#
luxiaotao1123
2022-12-23 05a274723ec0113346955e25c59a50ec4e7430e4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.zy.core;
 
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
 
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
/**
 * news stories for zoneyung
 * Created by vincent on 2022/12/22
 */
@Slf4j
public class News {
 
    public static void main(String[] args) {
        News.info("info{}", 1);
        News.warn("warn{}", 2);
        News.error("error{}", 3);
        System.out.println(JSON.toJSONString(NEWS_QUEUE.data()));
    }
 
    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> 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) {
        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, replace(msg, args), new Date()));
    }
 
    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));
            }
            return sb.toString();
        }
    }
 
    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,
        ;
    }
 
}