Commit 56179770 authored by yanzg's avatar yanzg

修复等待时间

parent 2a0ecb83
package com.yanzuoguang.util.base;
import com.yanzuoguang.util.helper.StringHelper;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* 集合转换为表格字符串
*
* @author 颜佐光
*/
public class CollectionString {
private static final String SPLIT = "|";
private static final String EMPTY = " ";
private static final String ROW = "-";
private static final int EMPTY_SIZE = 2;
/**
* 集合转换为表格字符串
*
* @param rowList 需要转换的集合
* @param <T> 集合类型
* @return 表格字符串
*/
public static <T> String getCollectionString(String tag, Collection<T> rowList) {
// 每个字段的长度
Map<String, Integer> mapFieldCount = new HashMap<>(15);
// 每个值的长度
rowList.forEach(k -> ObjectEach.each(k, (name, value, field) -> {
// 获取字符串
String val = StringHelper.getFirst(StringHelper.toString(value));
int len = Math.max(length(name), length(val));
Integer hisLen = mapFieldCount.getOrDefault(name, len);
int maxLen = Math.max(len, hisLen);
mapFieldCount.put(name, maxLen);
}));
// 生成结果
StringBuilder sbResult = new StringBuilder();
sbResult.append(tag);
appendEmptyRow(sbResult, mapFieldCount);
sbResult.append(System.lineSeparator());
sbResult.append(SPLIT);
mapFieldCount.forEach((name, len) -> {
int nameLen = length(name);
appendChar(sbResult, EMPTY_SIZE, EMPTY);
sbResult.append(name);
appendChar(sbResult, len - nameLen + EMPTY_SIZE, EMPTY);
sbResult.append(SPLIT);
});
appendEmptyRow(sbResult, mapFieldCount);
// 遍历每一行
rowList.forEach(row -> {
sbResult.append(System.lineSeparator());
sbResult.append(SPLIT);
mapFieldCount.forEach((name, len) -> {
appendChar(sbResult, EMPTY_SIZE, EMPTY);
// 获取字符串
String value = StringHelper.getFirst(ObjectHelper.getString(row, name));
sbResult.append(value);
appendChar(sbResult, len - length(value) + EMPTY_SIZE, EMPTY);
sbResult.append(SPLIT);
});
});
appendEmptyRow(sbResult, mapFieldCount);
return sbResult.toString();
}
/**
* 添加空行
*
* @param sbResult
* @param mapFieldCount
*/
private static void appendEmptyRow(StringBuilder sbResult, Map<String, Integer> mapFieldCount) {
if (sbResult.length() > 0) {
sbResult.append(System.lineSeparator());
}
sbResult.append(SPLIT);
mapFieldCount.forEach((name, len) -> {
appendChar(sbResult, len + EMPTY_SIZE + EMPTY_SIZE, ROW);
sbResult.append(SPLIT);
});
}
/**
* 添加字符
*
* @param sb 字符串
* @param len 长度
* @param ch 字符
*/
private static void appendChar(StringBuilder sb, int len, String ch) {
for (int i = 0; i < len; i++) {
sb.append(ch);
}
}
/**
* 获取字符串的长度,如果有中⽂,则每个中⽂字符计为2位
*
* @param value 指定的字符串
* @return 字符串的长度
*/
public static int length(String value) {
int valueLength = 0;
String chinese = "[\u0391-\uFFE5]";
/* 获取字段值的长度,如果含中⽂字符,则每个中⽂字符长度为2,否则为1 */
for (int i = 0; i < value.length(); i++) {
/* 获取⼀个字符 */
String temp = value.substring(i, i + 1);
/* 判断是否为中⽂字符 */
if (temp.matches(chinese)) {
/* 中⽂字符长度为2 */
valueLength += 2;
} else {
/* 其他字符长度为1 */
valueLength += 1;
}
}
return valueLength;
}
}
package com.yanzuoguang.util.base;
import com.yanzuoguang.util.helper.StringHelper;
import java.util.HashMap;
import java.util.Map;
/**
* 遍历对象和值
*
* @author 颜佐光
*/
public class ObjectEach {
/**
* 遍历对象
*
* @param target 对象
* @param consumer 消费者,其中包含名称,值,方法(map时为空)
*/
public static void each(Object target, ObjectEachItem<String, Object, MethodField> consumer) {
if (target instanceof Map) {
eachMap((Map) target, consumer);
} else {
eachObject(target, consumer);
}
}
/**
* 遍历map对象
*
* @param target 目标对象
* @param consumer 遍历的字段,值,方法
*/
private static void eachMap(Map target, ObjectEachItem<String, Object, MethodField> consumer) {
for (Object key : target.keySet()) {
Object fromValue = target.get(key);
consumer.accept(StringHelper.toString(key), fromValue, null);
}
}
/**
* 遍历对象
*
* @param target 目标对象
* @param consumer 遍历的字段,值,方法
*/
private static void eachObject(Object target, ObjectEachItem<String, Object, MethodField> consumer) {
HashMap<String, MethodField> mapField = ObjectHelper.getTypeField(target.getClass());
for (Map.Entry<String, MethodField> field : mapField.entrySet()) {
String name = field.getValue().getName();
Object fromValue = ObjectHelper.get(target, name);
consumer.accept(name, fromValue, field.getValue());
}
}
}
package com.yanzuoguang.util.base;
import java.util.Objects;
/**
* 处理3个参数的函数接口
*
* @param <H> the input argument
* @param <I> the input argument
* @param <J> the input argument
* @author 颜佐光
*/
@FunctionalInterface
public interface ObjectEachItem<H, I, J> {
/**
* Performs this operation on the given argument.
*
* @param h the input argument
* @param i the input argument
* @param j the input argument
*/
void accept(H h, I i, J j);
/**
* Returns a composed {@code Consumer} that performs, in sequence, this
* operation followed by the {@code after} operation. If performing either
* operation throws an exception, it is relayed to the caller of the
* composed operation. If performing this operation throws an exception,
* the {@code after} operation will not be performed.
*
* @param after the operation to perform after this operation
* @return a composed {@code Consumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default ObjectEachItem<H, I, J> andThen(ObjectEachItem<? super H, ? super I, ? super J> after) {
Objects.requireNonNull(after);
return (H h, I i, J j) -> {
accept(h, i, j);
after.accept(h, i, j);
};
}
}
......@@ -470,7 +470,7 @@ public class ObjectHelper {
if (from == null || to == null) {
return to;
}
HashMap<String, MethodField> mapField = getInitTypeField(to.getClass());
HashMap<String, MethodField> mapField = getTypeField(to.getClass());
return writeWithClass(emptyWrite, to, from, mapField);
}
......@@ -568,7 +568,7 @@ public class ObjectHelper {
if (from == null || to == null) {
return to;
}
HashMap<String, MethodField> mapField = getInitTypeField(from.getClass());
HashMap<String, MethodField> mapField = getTypeField(from.getClass());
return writeWithClass(emptyWrite, to, from, mapField);
}
......
......@@ -11,6 +11,14 @@ public class DemoVo {
*/
private String name;
public DemoVo() {
}
public DemoVo(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
......
package helper;
import base.DemoVo;
import com.yanzuoguang.util.base.CollectionString;
import com.yanzuoguang.util.helper.StringHelper;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TestCollectionString {
@Test
public void testCh() {
List<DemoVo> mapList = new ArrayList<>(Arrays.asList(
new DemoVo("1", "颜佐光"),
new DemoVo("10000", "成吉思汗"),
new DemoVo("10001", "毛泽东")
));
System.out.println(CollectionString.getCollectionString("人名:", mapList));
System.out.println(CollectionString.getCollectionString(StringHelper.EMPTY, mapList));
}
@Test
public void testEn() {
List<DemoVo> mapList = new ArrayList<>(Arrays.asList(
new DemoVo("1", "yan zuo guang"),
new DemoVo("10000", "chen jie si han"),
new DemoVo("10001", "mao ze dong")
));
System.out.println(CollectionString.getCollectionString(StringHelper.EMPTY, mapList));
System.out.println(CollectionString.getCollectionString("人名:", mapList));
}
}
......@@ -4,7 +4,6 @@ import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.aop.log.LogLocal;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.LogVo;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
......
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.util.base.CollectionString;
import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.thread.ThreadNext;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* 日志时间
*
* @author 颜佐光
*/
@Component
public class AspectLogTime implements ThreadNext.Next, InitializingBean {
private MemoryCache<AspectUrlCountVo> memoryCache = new MemoryCache<>();
@Override
public void afterPropertiesSet() {
ThreadNext.start(this, "aspectLogTime");
}
@Override
public boolean next() {
List<AspectUrlCountVo> rowList = new ArrayList<>(memoryCache.getValues());
System.out.println(CollectionString.getCollectionString("接口执行次数:", rowList));
return true;
}
@Override
public int getNextTime() {
return 60 * 1000;
}
}
package com.yanzuoguang.cloud.aop;
/**
* 次数统计
*
* @author 颜佐光
*/
public class AspectUrlCountVo {
private volatile String url;
private volatile long startCount;
private volatile long endCount;
private volatile long totalTime;
private volatile long minTime;
private volatile long maxTime;
public AspectUrlCountVo(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public long getStartCount() {
return startCount;
}
public void setStartCount(long startCount) {
this.startCount = startCount;
}
public long getEndCount() {
return endCount;
}
public void setEndCount(long endCount) {
this.endCount = endCount;
}
public long getTotalTime() {
return totalTime;
}
public void setTotalTime(long totalTime) {
this.totalTime = totalTime;
}
public long getMinTime() {
return minTime;
}
public void setMinTime(long minTime) {
this.minTime = minTime;
}
public long getMaxTime() {
return maxTime;
}
public void setMaxTime(long maxTime) {
this.maxTime = maxTime;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment