From 7f2edc4bf3b3d2ac429abb9a06ac7f425b2a01fd Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@63.com>
Date: 星期三, 28 十二月 2022 09:00:42 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/News.java | 61 ++++++++++++++++++++++--------
1 files changed, 45 insertions(+), 16 deletions(-)
diff --git a/src/main/java/com/zy/core/News.java b/src/main/java/com/zy/core/News.java
index e757b17..47b3c24 100644
--- a/src/main/java/com/zy/core/News.java
+++ b/src/main/java/com/zy/core/News.java
@@ -1,5 +1,6 @@
package com.zy.core;
+import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Array;
@@ -13,6 +14,13 @@
*/
@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); }
@@ -31,19 +39,25 @@
public NewsQueue(Class<T> cls, int capacity) {
this.cls = cls;
- arr = (T[]) Array.newInstance(cls, capacity);
+ 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 {
- 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.reform();
}
this.arr[this.tail] = t;
this.tail ++;
@@ -56,14 +70,23 @@
}
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> all() {
+ 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);
@@ -71,14 +94,6 @@
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) {
@@ -94,7 +109,21 @@
}
private static boolean offer(NewsLevel level, String msg, Object[] args) {
- return NEWS_QUEUE.offer(new NewsDomain(level, msg, new Date()));
+ 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 {
--
Gitblit v1.9.1