This commit is contained in:
2023-06-29 01:20:24 +08:00
13 changed files with 54 additions and 52 deletions

View File

@@ -9,33 +9,22 @@ import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.util.TimeZone;
import org.apache.commons.lang3.StringUtils;
import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap;
import xyz.zhouxy.plusone.commons.collection.MapWrapper;
public class DateTimeUtil {
private static final MapWrapper<String, DateTimeFormatter> DATE_TIME_FORMATTER_CHCHE = MapWrapper
.<String, DateTimeFormatter>wrapHashMap()
.keyChecker(StringUtils::isNotBlank)
.valueChecker(Objects::nonNull)
private static final MapWrapper<String, DateTimeFormatter> DATE_TIME_FORMATTER_CACHE = MapWrapper
.<String, DateTimeFormatter>wrap(new SafeConcurrentHashMap<>())
.keyChecker(pattern -> Assert.isNotBlank(pattern, "The pattern could not be blank."))
.valueChecker(formatter -> Assert.notNull(formatter, "The formatter could not be null."))
.build();
public static DateTimeFormatter getDateTimeFormatter(String pattern) {
if (!DATE_TIME_FORMATTER_CHCHE.containsKey(pattern)) {
synchronized (DateTimeUtil.class) {
if (!DATE_TIME_FORMATTER_CHCHE.containsKey(pattern)) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
DATE_TIME_FORMATTER_CHCHE.put(pattern, formatter);
return formatter;
}
}
}
return DATE_TIME_FORMATTER_CHCHE.get(pattern)
.orElseThrow(() -> new IllegalStateException("Formatter does not exist."));
return DATE_TIME_FORMATTER_CACHE.computeIfAbsent(pattern, DateTimeFormatter::ofPattern);
}
public static String toString(String pattern, ZonedDateTime dateTime) {
@@ -126,7 +115,7 @@ public class DateTimeUtil {
* 只是不同时区的表示。
* </p>
*
* @param dateTime {@link Date} 对象
* @param timeMillis 时间戳
* @param zone 时区
* @return 带时区信息的地区时间
*/
@@ -191,7 +180,7 @@ public class DateTimeUtil {
/**
* 获取时间戳在指定时区的地区时间。
*
* @param dateTime {@link Date} 对象
* @param timeMillis 时间戳
* @param zone 时区
* @return 地区时间
*/

View File

@@ -60,7 +60,7 @@ public class MoreCollections {
return toConcurrentHashMap(c, keyGenerator, c.size());
}
public static <K, V> TreeMap<K, V> toTreeMap(Iterable<V> c, Function<? super V, K> keyGenerator) {
public static <K extends Comparable<? super K>, V> TreeMap<K, V> toTreeMap(Iterable<V> c, Function<? super V, K> keyGenerator) {
TreeMap<K, V> map = new TreeMap<>();
fillIntoEmptyMap(map, c, keyGenerator);
return map;
@@ -106,7 +106,7 @@ public class MoreCollections {
return toConcurrentHashMap(c, keyGenerator, c.length);
}
public static <K, V> TreeMap<K, V> toTreeMap(V[] c, Function<? super V, K> keyGenerator) {
public static <K extends Comparable<? super K>, V> TreeMap<K, V> toTreeMap(V[] c, Function<? super V, K> keyGenerator) {
TreeMap<K, V> map = new TreeMap<>();
fillIntoEmptyMap(map, c, keyGenerator);
return map;

View File

@@ -55,16 +55,16 @@ public class OptionalUtil {
}
/**
* 将 {@code Optional<Integer>} 转为 {@link OptionalInt}。
* 将 {@code Optional<Integer>} 对象转为 {@link OptionalInt} 对象
* <p>
* {@code Optional<Integer>} 将整数包装了两次,改为使用 {@link OptionalInt} 包装其中的整数数据。
* </p>
*
* @param value 包装对象
* @param optionalObj {@code Optional<Integer>} 对象
* @return {@link OptionalInt} 实例
*/
public static OptionalInt toOptionalInt(Optional<Integer> objectOptional) {
return optionalOf(objectOptional.orElse(null));
public static OptionalInt toOptionalInt(Optional<Integer> optionalObj) {
return optionalOf(optionalObj.orElse(null));
}
/**
@@ -87,11 +87,11 @@ public class OptionalUtil {
* {@code Optional<Long>} 将整数包装了两次,改为使用 {@link OptionalLong} 包装其中的整数数据。
* </p>
*
* @param value 包装对象
* @param optionalObj 包装对象
* @return {@link OptionalLong} 实例
*/
public static OptionalLong toOptionalLong(Optional<Long> objectOptional) {
return optionalOf(objectOptional.orElse(null));
public static OptionalLong toOptionalLong(Optional<Long> optionalObj) {
return optionalOf(optionalObj.orElse(null));
}
/**
@@ -114,11 +114,11 @@ public class OptionalUtil {
* {@code Optional<Double>} 将整数包装了两次,改为使用 {@link OptionalDouble} 包装其中的整数数据。
* </p>
*
* @param value 包装对象
* @param optionalObj 包装对象
* @return {@link OptionalDouble} 实例
*/
public static OptionalDouble toOptionalDouble(Optional<Double> objectOptional) {
return optionalOf(objectOptional.orElse(null));
public static OptionalDouble toOptionalDouble(Optional<Double> optionalObj) {
return optionalOf(optionalObj.orElse(null));
}
/**

View File

@@ -51,10 +51,11 @@ public class PagingAndSortingQueryParams {
}
public PagingAndSortingQueryParams(String... sortableColNames) {
Set<String> sortableColNameSet = new HashSet<>(sortableColNames.length);
for (String colName : sortableColNames) {
Assert.isNotBlank(colName, "Column name must has text.");
sortableColNameSet.add(colName);
}
Set<String> sortableColNameSet = new HashSet<>(sortableColNames.length);
this.sortableColNames = Collections.unmodifiableSet(sortableColNameSet);
}

View File

@@ -42,10 +42,10 @@ public class SnowflakeIdGenerator {
private static final long SEQUENCE_MASK = -1L ^ (-1L << SEQUENCE_BITS);
/** 工作机器 ID (0~31) */
private long workerId;
private final long workerId;
/** 数据中心 ID (0~31) */
private long datacenterId;
private final long datacenterId;
/** 毫秒内序列 (0~4095) */
private long sequence = 0L;
@@ -78,7 +78,6 @@ public class SnowflakeIdGenerator {
* 获得下一个ID (该方法是线程安全的)
*
* @return SnowflakeId
* @throws InterruptedException
*/
public synchronized long nextId() {
long timestamp = timeGen();