Skip to content
This repository was archived by the owner on Nov 21, 2020. It is now read-only.

Commit 93b46f2

Browse files
committed
feat: enhance codes
1 parent f051d2b commit 93b46f2

File tree

9 files changed

+165
-38
lines changed

9 files changed

+165
-38
lines changed

pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2323
<maven.compiler.source>1.8</maven.compiler.source>
2424
<maven.compiler.target>1.8</maven.compiler.target>
25-
<junit.version>4.12</junit.version>
25+
<junit.version>4.13-beta-3</junit.version>
2626
<!--Required Dependency-->
27-
<fastjson.version>1.2.56</fastjson.version>
28-
<hutool.version>4.5.3</hutool.version>
29-
<guava.version>27.1-jre</guava.version>
30-
<slf4j.version>1.7.26</slf4j.version>
27+
<fastjson.version>1.2.58</fastjson.version>
28+
<hutool.version>4.5.16</hutool.version>
29+
<guava.version>28.0-jre</guava.version>
30+
<slf4j.version>2.0.0-alpha0</slf4j.version>
3131
<!--Optional Dependency-->
3232
<javax.servlet.version>4.0.1</javax.servlet.version>
3333
<spring.version>5.1.6.RELEASE</spring.version>

src/main/java/org/code4everything/boot/base/DateUtils.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ public final class DateUtils {
2222

2323
private static Date endOfTodayCopier = new Date(0);
2424

25+
// -----------------------本周----------------------------------------------
26+
27+
private static long startOfThisWeek = 0;
28+
29+
private static Date startOfThisWeekCopier = new Date(0);
30+
31+
private static long endOfThisWeek = 0;
32+
33+
private static Date endOfThisWeekCopier = new Date(0);
34+
2535
// ------------------当月-------------------------------------------
2636

2737
private static long startOfThisMonth = 0;
@@ -32,8 +42,62 @@ public final class DateUtils {
3242

3343
private static Date endOfThisMonthCopier = new Date(0);
3444

45+
// ----------------------------当年-----------------------------------------------
46+
47+
private static long startOfThisYear = 0;
48+
49+
private static Date startOfThisYearCopier = new Date(0);
50+
51+
private static long endOfThisYear = 0;
52+
53+
private static Date endOfThisYearCopier = new Date(0);
54+
3555
private DateUtils() {}
3656

57+
/**
58+
* 获取本年的开始时间点
59+
*
60+
* @return 本年的开始
61+
*
62+
* @since 1.1.5
63+
*/
64+
public static Date getStartOfThisYear() {
65+
return checkThisYear(true, startOfThisYearCopier);
66+
}
67+
68+
/**
69+
* 获取本年的结束时间点
70+
*
71+
* @return 本年的结束
72+
*
73+
* @since 1.1.5
74+
*/
75+
public static Date getEndOfThisYear() {
76+
return checkThisYear(false, endOfThisYearCopier);
77+
}
78+
79+
/**
80+
* 获取本周的开始时间点
81+
*
82+
* @return 本周的开始
83+
*
84+
* @since 1.1.3
85+
*/
86+
public static Date getStartOfThisWeek() {
87+
return checkThisWeek(true, startOfThisWeekCopier);
88+
}
89+
90+
/**
91+
* 获取本周的结束时间点
92+
*
93+
* @return 本周的结束
94+
*
95+
* @since 1.1.5
96+
*/
97+
public static Date getEndOfThisWeek() {
98+
return checkThisWeek(false, endOfThisWeekCopier);
99+
}
100+
37101
/**
38102
* 获取本月的开始时间点
39103
*
@@ -78,6 +142,35 @@ public static Date getEndOfToday() {
78142
return checkToday(false, endOfTodayCopier);
79143
}
80144

145+
/**
146+
* 使用副本来防止原引用被篡改
147+
*/
148+
private static Date checkThisYear(boolean isStart, Date copier) {
149+
long curr = System.currentTimeMillis();
150+
if (curr > endOfThisYear) {
151+
// 如果当前的时间戳超过了endOfThisYear的时间戳,说明ThisMonth的时间戳已经过期,需重新设置
152+
Date date = new Date();
153+
startOfThisYear = DateUtil.beginOfYear(date).getTime();
154+
endOfThisYear = DateUtil.endOfYear(date).getTime();
155+
}
156+
return check(isStart ? startOfThisYear : endOfThisYear, copier);
157+
}
158+
159+
160+
/**
161+
* 使用副本来防止原引用被篡改
162+
*/
163+
private static Date checkThisWeek(boolean isStart, Date copier) {
164+
long curr = System.currentTimeMillis();
165+
if (curr > endOfThisWeek) {
166+
// 如果当前的时间戳超过了endOfThisWeek的时间戳,说明ThisMonth的时间戳已经过期,需重新设置
167+
Date date = new Date();
168+
startOfThisWeek = DateUtil.beginOfWeek(date).getTime();
169+
endOfThisWeek = DateUtil.endOfWeek(date).getTime();
170+
}
171+
return check(isStart ? startOfThisWeek : endOfThisWeek, copier);
172+
}
173+
81174
/**
82175
* 使用副本来防止原引用被篡改
83176
*/

src/main/java/org/code4everything/boot/base/FileUtils.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import cn.hutool.core.io.watch.Watcher;
88
import cn.hutool.core.io.watch.watchers.DelayWatcher;
99
import cn.hutool.core.util.CharsetUtil;
10-
import cn.hutool.core.util.StrUtil;
1110
import com.alibaba.fastjson.JSONObject;
1211
import org.code4everything.boot.base.constant.StringConsts;
1312
import org.code4everything.boot.config.BootConfig;
@@ -37,30 +36,6 @@ public final class FileUtils {
3736

3837
private FileUtils() {}
3938

40-
/**
41-
* 格式化文件大小,如:23.3 MB
42-
*
43-
* @param size 大小
44-
* @param scale 保留小数位数:0,1,2,3
45-
*
46-
* @return 文件大小
47-
*
48-
* @since 1.1.2
49-
*/
50-
public static String formatSize(long size, int scale) {
51-
if (size < 0) {
52-
throw new IllegalArgumentException("size must not be negative");
53-
}
54-
int idx = 0;
55-
int last = 0;
56-
for (; size > KB; idx++, size /= KB) {
57-
last = (int) size;
58-
}
59-
// 求出余数:1^3=2, 2^3=1, 3^3=0
60-
last = scale == 0 ? 0 : (last % KB) / (int) Math.pow(10, scale ^ 3);
61-
return size + (last > 0 ? "." + StrUtil.padPre(String.valueOf(last), scale, "0") : "") + SIZE[idx];
62-
}
63-
6439
/**
6540
* 监听文件变化,并自动注入Bean类
6641
*

src/main/java/org/code4everything/boot/base/ObjectUtils.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.code4everything.boot.base;
22

33
import cn.hutool.core.util.ArrayUtil;
4+
import cn.hutool.core.util.ObjectUtil;
45

56
import java.util.Objects;
67

@@ -15,7 +16,7 @@ public final class ObjectUtils {
1516
private ObjectUtils() {}
1617

1718
/**
18-
* 所有对象都不为NULL
19+
* 是否所有对象都不为NULL
1920
*
2021
* @param objects 对象数组
2122
*
@@ -33,4 +34,44 @@ public static boolean isNotNull(Object... objects) {
3334
}
3435
return true;
3536
}
37+
38+
/**
39+
* 是否所有对象均为NULL
40+
*
41+
* @param objects 对象数组
42+
*
43+
* @return 是否所有对象均为NULL
44+
*
45+
* @since 1.1.5
46+
*/
47+
public static boolean isNull(Object... objects) {
48+
if (ArrayUtil.isNotEmpty(objects)) {
49+
for (Object object : objects) {
50+
if (ObjectUtil.isNotNull(object)) {
51+
return false;
52+
}
53+
}
54+
}
55+
return true;
56+
}
57+
58+
/**
59+
* 是否有一个为NULL的对象
60+
*
61+
* @param objects 对象数组
62+
*
63+
* @return 是否有一个为NULL的对象
64+
*
65+
* @since 1.1.5
66+
*/
67+
public static boolean isNullAny(Object... objects) {
68+
if (ArrayUtil.isNotEmpty(objects)) {
69+
for (Object object : objects) {
70+
if (Objects.isNull(object)) {
71+
return true;
72+
}
73+
}
74+
}
75+
return false;
76+
}
3677
}

src/main/java/org/code4everything/boot/base/bean/BaseBean.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import cn.hutool.core.bean.BeanUtil;
44
import cn.hutool.core.util.ObjectUtil;
55
import cn.hutool.core.util.ReflectUtil;
6-
import com.alibaba.fastjson.JSONObject;
6+
import com.alibaba.fastjson.JSON;
77

88
import java.lang.reflect.Field;
99
import java.util.Objects;
@@ -53,7 +53,7 @@ default boolean hasValue() {
5353
* @since 1.1.2
5454
*/
5555
default String toJsonString(boolean pretty) {
56-
return JSONObject.toJSONString(this, pretty);
56+
return JSON.toJSONString(this, pretty);
5757
}
5858

5959
/**

src/main/java/org/code4everything/boot/base/collection/SortedList.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.code4everything.boot.base.collection;
22

3+
import cn.hutool.core.collection.CollUtil;
34
import cn.hutool.core.comparator.ComparatorException;
45
import cn.hutool.core.util.ObjectUtil;
6+
import com.alibaba.fastjson.JSON;
7+
import org.code4everything.boot.base.bean.BaseBean;
58

69
import javax.annotation.concurrent.NotThreadSafe;
710
import java.util.*;
@@ -14,7 +17,7 @@
1417
* @since 2019/1/17
1518
**/
1619
@NotThreadSafe
17-
public class SortedList<E, T extends List<E>> {
20+
public class SortedList<E, T extends List<E>> implements BaseBean {
1821

1922
/**
2023
* 数据源
@@ -357,4 +360,14 @@ private void checkComparator() {
357360
throw new ComparatorException("comparator must not be null");
358361
}
359362
}
363+
364+
@Override
365+
public boolean hasValue() {
366+
return CollUtil.isNotEmpty(list);
367+
}
368+
369+
@Override
370+
public String toJsonString(boolean pretty) {
371+
return JSON.toJSONString(list);
372+
}
360373
}

src/main/java/org/code4everything/boot/base/encoder/DefaultFieldEncoder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.code4everything.boot.base.encoder;
22

3+
import cn.hutool.core.codec.Base64;
34
import cn.hutool.core.util.ObjectUtil;
45
import cn.hutool.crypto.digest.DigestUtil;
56
import org.slf4j.Logger;
@@ -53,6 +54,9 @@ protected boolean encodeField(Field field, Object data, Sealed sealed) {
5354
case "sha256":
5455
field.set(data, DigestUtil.sha256Hex(value));
5556
break;
57+
case "base64":
58+
field.set(data, Base64.encode(value));
59+
break;
5660
default:
5761
field.set(data, sealed.value());
5862
break;

src/main/java/org/code4everything/boot/base/encoder/Sealed.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
public @interface Sealed {
1818

1919
/**
20-
* 加密方法,默认仅支持:md5 sha1 sha256,不支持的加密方法将用方法名覆盖字段值。 其他加密方法需要继承 {@link DefaultFieldEncoder} 后重写 {@link
20+
* 加密方法,默认仅支持:md5 sha1 sha256 base64,不支持的加密方法将用方法名覆盖字段值。 其他加密方法需要继承 {@link DefaultFieldEncoder} 后重写 {@link
2121
* DefaultFieldEncoder#encodeField(Field, Object, Sealed)} 方法或你自己实现 {@link FieldEncoder}接口
2222
*
2323
* @return 加密方法

src/main/java/org/code4everything/boot/cache/guava/GuavaCache.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.code4everything.boot.cache.guava;
22

3+
import com.google.common.cache.Cache;
34
import com.google.common.cache.CacheBuilder;
45
import org.code4everything.boot.cache.AbstractCache;
56

@@ -16,17 +17,17 @@ public class GuavaCache extends AbstractCache {
1617
/**
1718
* @since 1.1.3
1819
*/
19-
private final com.google.common.cache.Cache<Object, Object> cache;
20+
private final Cache<Object, Object> cache;
2021

2122
/**
2223
* 构造函数
2324
*
2425
* @param name 缓存名
25-
* @param cache {@link com.google.common.cache.Cache}
26+
* @param cache {@link Cache}
2627
*
2728
* @since 1.1.3
2829
*/
29-
public GuavaCache(String name, com.google.common.cache.Cache<Object, Object> cache) {
30+
public GuavaCache(String name, Cache<Object, Object> cache) {
3031
super(name, cache);
3132
this.cache = cache;
3233
}

0 commit comments

Comments
 (0)