| | |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.lang.reflect.Array; |
| | | import java.util.Arrays; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * news stories |
| | | * news stories for zoneyung |
| | | * Created by vincent on 2022/12/22 |
| | | */ |
| | | @Slf4j |
| | | @SuppressWarnings({"unchecked","rawtypes"}) |
| | | public class News { |
| | | |
| | | interface NewsSupport<T> { boolean execute(T t); } |
| | | |
| | | private static final NewsQueue<NewsDomain> NEWS_QUEUE = new NewsQueue<>(NewsDomain.class, 1024); |
| | | |
| | | @SuppressWarnings({"unchecked"}) |
| | | static class NewsQueue<T> { |
| | | |
| | | private final transient Class<T> cls; |
| | | private final T[] arr; |
| | | private final int capacity; |
| | | private int head; |
| | | private int tail; |
| | | |
| | | { this.head = 0; this.tail = 0; } |
| | | |
| | | public NewsQueue(Class<T> cls, int capacity) { |
| | | this.cls = cls; |
| | | arr = (T[]) Array.newInstance(cls, capacity); |
| | | this.capacity = capacity; |
| | | } |
| | | |
| | | public synchronized boolean offer(T t) { |
| | | if (this.tail == this.capacity) { |
| | | return false; |
| | | } else { |
| | | for (int i = this.head; i < this.tail; i++) { |
| | | this.arr[i-this.head] = this.arr[i]; |
| | | } |
| | | this.tail -= this.head; |
| | | this.head = 0; |
| | | } |
| | | this.arr[this.tail] = t; |
| | | this.tail ++; |
| | | return true; |
| | | } |
| | | |
| | | public synchronized T peek() { |
| | | if (this.head == this.tail) { |
| | | return null; |
| | | } |
| | | T t = this.arr[this.head]; |
| | | this.head ++; |
| | | return t; |
| | | } |
| | | |
| | | public synchronized int size() { |
| | | return this.tail - this.head; |
| | | } |
| | | |
| | | public synchronized List<T> all() { |
| | | T[] ts = (T[]) Array.newInstance(this.cls, size()); |
| | | if (this.tail - this.head >= 0) { |
| | | System.arraycopy(this.arr, this.head, ts, 0, this.tail - this.head); |
| | | } |
| | | return Arrays.asList(ts); |
| | | } |
| | | |
| | | } |
| | | |
| | | public static void info(String format) { |
| | | offer(NewsLevel.INFO, format, null); |
| | | } |
| | | |
| | | public static void info(String format, Object argument) { |
| | | offer(NewsLevel.INFO, format, new Object[]{argument}); |
| | | } |
| | | |
| | | public static void info(String format, Object... arguments) { |
| | | offer(NewsLevel.INFO, format, arguments); |
| | | } |
| | | |
| | | public static void warn(String format, Object... arguments) { |
| | | offer(NewsLevel.WARN, format, arguments); |
| | | } |
| | | |
| | | public static void error(String format, Object... arguments) { |
| | | offer(NewsLevel.ERROR, format, arguments); |
| | | } |
| | | |
| | | private static boolean offer(NewsLevel level, String msg, Object[] args) { |
| | | return NEWS_QUEUE.offer(new NewsDomain(level, msg, new Date())); |
| | | } |
| | | |
| | | static class NewsDomain { |
| | | public NewsLevel level; |
| | | public String content; |
| | | public Date date; |
| | | |
| | | public NewsDomain(NewsLevel level, String content, Date date) { |
| | | this.level = level; |
| | | this.content = content; |
| | | this.date = date; |
| | | } |
| | | } |
| | | |
| | | enum NewsLevel { |
| | | INFO, |
| | | WARN, |
| | | ERROR, |
| | | ; |
| | | } |
| | | |
| | | } |