| | |
| | | package com.zy.acs.common.utils; |
| | | |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.slf4j.helpers.FormattingTuple; |
| | | import org.slf4j.helpers.MessageFormatter; |
| | | |
| | | import java.lang.reflect.Array; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * news stories for vincent |
| | |
| | | @Slf4j |
| | | public class News { |
| | | |
| | | private static final int DEFAULT_CAPACITY = 1024; |
| | | private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | private static final NewsQueue<NewsDomain> NEWS_QUEUE = new NewsQueue<>(DEFAULT_CAPACITY); |
| | | |
| | | public static void main(String[] args) { |
| | | News.info("info{}", 1); |
| | | News.warn("warn{}", 2); |
| | | News.error("error{}", 3); |
| | | System.out.println(News.print()); |
| | | } |
| | | |
| | | 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; |
| | | this.arr = (T[]) Array.newInstance(cls, capacity); |
| | | this.capacity = capacity; |
| | | } |
| | | |
| | | public synchronized boolean offer(T t) { |
| | | if (this.tail == this.capacity) { |
| | | this.peek(); |
| | | } |
| | | this.reform(); |
| | | this.arr[this.tail] = t; |
| | | this.tail ++; |
| | | return true; |
| | | } |
| | | |
| | | public synchronized boolean put(T t) { |
| | | if (this.tail == this.capacity) { |
| | | return false; |
| | | } else { |
| | | this.reform(); |
| | | } |
| | | 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 ++; |
| | | this.reform(); |
| | | return t; |
| | | } |
| | | |
| | | private void reform() { |
| | | for (int i = this.head; i < this.tail; i++) { |
| | | this.arr[i-this.head] = this.arr[i]; |
| | | } |
| | | this.tail -= this.head; |
| | | this.head = 0; |
| | | } |
| | | |
| | | public synchronized int size() { |
| | | return this.tail - this.head; |
| | | } |
| | | |
| | | public synchronized List<T> data() { |
| | | 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, Object... arguments) { |
| | |
| | | for (int i = 0; i < domains.size(); i++) { |
| | | NewsDomain domain = domains.get(i); |
| | | sb.append("{"); |
| | | sb.append("\"l\":").append(domain.level.idx).append(","); |
| | | sb.append("\"v\":\"").append(domain.content).append("\"").append(","); |
| | | sb.append("\"t\":\"").append(domain.date).append("\""); |
| | | sb.append("\"l\":").append(domain.getLevel().idx).append(","); |
| | | sb.append("\"v\":\"").append(escapeJson(domain.getContent())).append("\"").append(","); |
| | | sb.append("\"t\":\"").append(escapeJson(domain.getDate())).append("\""); |
| | | sb.append("}"); |
| | | if (i < domains.size() - 1) { |
| | | sb.append(","); |
| | |
| | | List<Map<String, Object>> res = new ArrayList<>(); |
| | | for (NewsDomain datum : NEWS_QUEUE.data()) { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("l", datum.level.idx); |
| | | map.put("v", datum.content); |
| | | map.put("t", datum.date); |
| | | map.put("l", datum.getLevel().idx); |
| | | map.put("v", datum.getContent()); |
| | | map.put("t", datum.getDate()); |
| | | res.add(map); |
| | | } |
| | | return res; |
| | | } |
| | | |
| | | private static boolean offer(NewsLevel level, String msg, Object[] args) { |
| | | return NEWS_QUEUE.offer(new NewsDomain(level, replace(msg, args), (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()))); |
| | | String template = msg == null ? "" : msg; |
| | | FormattingTuple tuple = MessageFormatter.arrayFormat(template, args); |
| | | String formatted = tuple.getMessage(); |
| | | return NEWS_QUEUE.offer(new NewsDomain(level, formatted, |
| | | LocalDateTime.now().format(DATE_FORMATTER))); |
| | | } |
| | | |
| | | private static String replace(String str, Object[] objs){ |
| | | if (null == objs || objs.length == 0 || null == str || "".equals(str.trim())) { |
| | | return str; |
| | | } else { |
| | | StringBuilder sb = new StringBuilder(str); |
| | | for (Object obj : objs) { |
| | | int idx = sb.indexOf("{}"); |
| | | if (idx == -1) { break; } |
| | | sb.replace(idx, idx + 2, String.valueOf(obj)); |
| | | private static String escapeJson(String value) { |
| | | if (value == null) { |
| | | return ""; |
| | | } |
| | | StringBuilder sb = new StringBuilder(value.length()); |
| | | for (char c : value.toCharArray()) { |
| | | switch (c) { |
| | | case '"': |
| | | sb.append("\\\""); |
| | | break; |
| | | case '\\': |
| | | sb.append("\\\\"); |
| | | break; |
| | | case '\n': |
| | | sb.append("\\n"); |
| | | break; |
| | | case '\r': |
| | | sb.append("\\r"); |
| | | break; |
| | | case '\t': |
| | | sb.append("\\t"); |
| | | break; |
| | | default: |
| | | if (c < 0x20) { |
| | | sb.append(String.format("\\u%04x", (int) c)); |
| | | } else { |
| | | sb.append(c); |
| | | } |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | private static final class NewsQueue<T> { |
| | | |
| | | private final Object[] arr; |
| | | private final int capacity; |
| | | private int head; |
| | | private int size; |
| | | |
| | | private NewsQueue(int capacity) { |
| | | if (capacity <= 0) { |
| | | throw new IllegalArgumentException("capacity must be > 0"); |
| | | } |
| | | this.arr = new Object[capacity]; |
| | | this.capacity = capacity; |
| | | this.head = 0; |
| | | this.size = 0; |
| | | } |
| | | |
| | | public synchronized boolean offer(T t) { |
| | | int writeIndex = (head + size) % capacity; |
| | | arr[writeIndex] = t; |
| | | if (size == capacity) { |
| | | head = (head + 1) % capacity; |
| | | } else { |
| | | size++; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | public synchronized List<T> data() { |
| | | List<T> copy = new ArrayList<>(size); |
| | | for (int i = 0; i < size; i++) { |
| | | int idx = (head + i) % capacity; |
| | | @SuppressWarnings("unchecked") |
| | | T element = (T) arr[idx]; |
| | | copy.add(element); |
| | | } |
| | | return copy; |
| | | } |
| | | } |
| | | |
| | | @Data |
| | | static class NewsDomain { |
| | | public NewsLevel level; |
| | | public String content; |
| | | public String date; |
| | | private final NewsLevel level; |
| | | private final String content; |
| | | private final String date; |
| | | |
| | | public NewsDomain(NewsLevel level, String content, String date) { |
| | | this.level = level; |
| | | this.content = content; |
| | | this.date = date; |
| | | } |
| | | |
| | | } |
| | | |
| | | enum NewsLevel { |
| | |
| | | WARN(2), |
| | | ERROR(3), |
| | | ; |
| | | public int idx; |
| | | public final int idx; |
| | | NewsLevel(int idx) { |
| | | this.idx = idx; |
| | | } |