forked from plusone/plusone-commons
Merge branch 'dev' of http://zhouxy.xyz:3000/ZhouXY108/plusone-commons into dev
commit
8cc55fbe7e
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package xyz.zhouxy.plusone.commons.util;
|
package xyz.zhouxy.plusone.commons.collection;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -164,7 +164,7 @@ public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V
|
||||||
return this.map.toString();
|
return this.map.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract static class Builder<K, V, T extends AbstractMapWrapper<K, V, T>> {
|
public abstract static class Builder<K, V, T extends AbstractMapWrapper<K, V, T>> {
|
||||||
protected final Map<K, V> map;
|
protected final Map<K, V> map;
|
||||||
protected Consumer<K> keyChecker;
|
protected Consumer<K> keyChecker;
|
||||||
protected Consumer<V> valueChecker;
|
protected Consumer<V> valueChecker;
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package xyz.zhouxy.plusone.commons.util;
|
package xyz.zhouxy.plusone.commons.collection;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
|
@ -21,6 +21,7 @@ import java.util.regex.Pattern;
|
||||||
import com.fasterxml.jackson.annotation.JsonValue;
|
import com.fasterxml.jackson.annotation.JsonValue;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.util.Assert;
|
import xyz.zhouxy.plusone.commons.util.Assert;
|
||||||
|
import xyz.zhouxy.plusone.commons.util.RegexUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 带校验的字符串值对象
|
* 带校验的字符串值对象
|
||||||
|
@ -34,7 +35,7 @@ public abstract class ValidatableStringRecord {
|
||||||
protected ValidatableStringRecord(String value, Pattern pattern) {
|
protected ValidatableStringRecord(String value, Pattern pattern) {
|
||||||
Assert.notNull(pattern, "The pattern must not be null.");
|
Assert.notNull(pattern, "The pattern must not be null.");
|
||||||
Assert.isNotBlank(value, "The value must be has text.");
|
Assert.isNotBlank(value, "The value must be has text.");
|
||||||
Assert.isTrue(pattern.matcher(value).matches());
|
Assert.isTrue(RegexUtil.matches(value, pattern));
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ package xyz.zhouxy.plusone.commons.exception;
|
||||||
*
|
*
|
||||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||||
*/
|
*/
|
||||||
public abstract class BaseException extends RuntimeException implements IWithCode {
|
public abstract class BaseException extends RuntimeException implements IWithIntCode {
|
||||||
|
|
||||||
private static final long serialVersionUID = -2546365325001947203L;
|
private static final long serialVersionUID = -2546365325001947203L;
|
||||||
|
|
||||||
|
|
|
@ -16,14 +16,16 @@
|
||||||
|
|
||||||
package xyz.zhouxy.plusone.commons.exception;
|
package xyz.zhouxy.plusone.commons.exception;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 规定实现类带有 {@code getCode} 方法。
|
* 规定实现类带有 {@code getCode} 方法。
|
||||||
* 用于像自定义异常等需要带有 {@code code} 字段的类,
|
* 用于像自定义异常等需要带有 {@code code} 字段的类,
|
||||||
* 方便其它地方的程序判断该类的是否实现了此接口,以此获取其实例的 {@code code} 字段的值。
|
* 方便其它地方的程序判断该类的是否实现了此接口,以此获取其实例的 {@code code} 字段的值。
|
||||||
*
|
*
|
||||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||||
* @see BaseException
|
|
||||||
*/
|
*/
|
||||||
public interface IWithCode {
|
public interface IWithCode<T> {
|
||||||
int getCode();
|
@Nonnull
|
||||||
|
T getCode();
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2022-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package xyz.zhouxy.plusone.commons.exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 规定实现类带有 {@code getCode} 方法。
|
||||||
|
* 用于像自定义异常等需要带有 {@code code} 字段的类,
|
||||||
|
* 方便其它地方的程序判断该类的是否实现了此接口,以此获取其实例的 {@code code} 字段的值。
|
||||||
|
*
|
||||||
|
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||||
|
* @see BaseException
|
||||||
|
*/
|
||||||
|
public interface IWithIntCode {
|
||||||
|
int getCode();
|
||||||
|
}
|
|
@ -31,7 +31,7 @@ import java.util.Set;
|
||||||
|
|
||||||
import com.google.common.annotations.Beta;
|
import com.google.common.annotations.Beta;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.util.AbstractMapWrapper;
|
import xyz.zhouxy.plusone.commons.collection.AbstractMapWrapper;
|
||||||
import xyz.zhouxy.plusone.commons.util.Assert;
|
import xyz.zhouxy.plusone.commons.util.Assert;
|
||||||
import xyz.zhouxy.plusone.commons.util.OptionalUtil;
|
import xyz.zhouxy.plusone.commons.util.OptionalUtil;
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,12 @@ import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
import org.joda.time.DateTimeZone;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap;
|
import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.commons.collection.MapWrapper;
|
||||||
|
|
||||||
public class DateTimeUtil {
|
public class DateTimeUtil {
|
||||||
|
|
||||||
private static final MapWrapper<String, DateTimeFormatter> DATE_TIME_FORMATTER_CACHE = MapWrapper
|
private static final MapWrapper<String, DateTimeFormatter> DATE_TIME_FORMATTER_CACHE = MapWrapper
|
||||||
|
@ -221,6 +225,7 @@ public class DateTimeUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====================
|
// ====================
|
||||||
|
|
||||||
// toJodaInstant
|
// toJodaInstant
|
||||||
|
|
||||||
public static org.joda.time.Instant toJodaInstant(java.time.Instant instant) {
|
public static org.joda.time.Instant toJodaInstant(java.time.Instant instant) {
|
||||||
|
@ -261,14 +266,14 @@ public class DateTimeUtil {
|
||||||
public static org.joda.time.DateTime toJodaDateTime(
|
public static org.joda.time.DateTime toJodaDateTime(
|
||||||
java.time.LocalDateTime localDateTime,
|
java.time.LocalDateTime localDateTime,
|
||||||
java.time.ZoneId zone) {
|
java.time.ZoneId zone) {
|
||||||
org.joda.time.DateTimeZone dateTimeZone = org.joda.time.DateTimeZone.forID(zone.getId());
|
org.joda.time.DateTimeZone dateTimeZone = toJodaTime(zone);
|
||||||
return toJodaInstant(ZonedDateTime.of(localDateTime, zone).toInstant()).toDateTime(dateTimeZone);
|
return toJodaInstant(ZonedDateTime.of(localDateTime, zone).toInstant()).toDateTime(dateTimeZone);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static org.joda.time.DateTime toJodaDateTime(
|
public static org.joda.time.DateTime toJodaDateTime(
|
||||||
java.time.Instant instant,
|
java.time.Instant instant,
|
||||||
java.time.ZoneId zone) {
|
java.time.ZoneId zone) {
|
||||||
org.joda.time.DateTimeZone dateTimeZone = org.joda.time.DateTimeZone.forID(zone.getId());
|
org.joda.time.DateTimeZone dateTimeZone = toJodaTime(zone);
|
||||||
return toJodaInstant(instant).toDateTime(dateTimeZone);
|
return toJodaInstant(instant).toDateTime(dateTimeZone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -282,31 +287,39 @@ public class DateTimeUtil {
|
||||||
public static java.time.ZonedDateTime toZonedDateTime(
|
public static java.time.ZonedDateTime toZonedDateTime(
|
||||||
org.joda.time.LocalDateTime localDateTime,
|
org.joda.time.LocalDateTime localDateTime,
|
||||||
org.joda.time.DateTimeZone dateTimeZone) {
|
org.joda.time.DateTimeZone dateTimeZone) {
|
||||||
java.time.ZoneId zone = dateTimeZone.toTimeZone().toZoneId();
|
java.time.ZoneId zone = toJavaZone(dateTimeZone);
|
||||||
return toJavaInstant(localDateTime, dateTimeZone).atZone(zone);
|
return toJavaInstant(localDateTime, dateTimeZone).atZone(zone);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static java.time.ZonedDateTime toZonedDateTime(
|
public static java.time.ZonedDateTime toZonedDateTime(
|
||||||
org.joda.time.Instant instant,
|
org.joda.time.Instant instant,
|
||||||
org.joda.time.DateTimeZone dateTimeZone) {
|
org.joda.time.DateTimeZone dateTimeZone) {
|
||||||
java.time.ZoneId zone = dateTimeZone.toTimeZone().toZoneId();
|
java.time.ZoneId zone = toJavaZone(dateTimeZone);
|
||||||
return toJavaInstant(instant).atZone(zone);
|
return toJavaInstant(instant).atZone(zone);
|
||||||
}
|
}
|
||||||
|
|
||||||
// toJodaLocalDateTime
|
// toJodaLocalDateTime
|
||||||
|
|
||||||
public static org.joda.time.LocalDateTime toJodaLocalDateTime(java.time.LocalDateTime localDateTime) {
|
public static org.joda.time.LocalDateTime toJodaLocalDateTime(java.time.LocalDateTime localDateTime) {
|
||||||
return toJodaInstant(localDateTime, ZoneId.systemDefault())
|
java.time.ZoneId javaZone = java.time.ZoneId.systemDefault();
|
||||||
.toDateTime(org.joda.time.DateTimeZone.getDefault())
|
org.joda.time.DateTimeZone jodaZone = toJodaTime(javaZone);
|
||||||
.toLocalDateTime();
|
return toJodaInstant(localDateTime, javaZone).toDateTime(jodaZone).toLocalDateTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
// toJavaLocalDateTime
|
// toJavaLocalDateTime
|
||||||
|
|
||||||
public static java.time.LocalDateTime toJavaLocalDateTime(org.joda.time.LocalDateTime localDateTime) {
|
public static java.time.LocalDateTime toJavaLocalDateTime(org.joda.time.LocalDateTime localDateTime) {
|
||||||
return toJavaInstant(localDateTime, org.joda.time.DateTimeZone.getDefault())
|
org.joda.time.DateTimeZone jodaZone = org.joda.time.DateTimeZone.getDefault();
|
||||||
.atZone(java.time.ZoneId.systemDefault())
|
java.time.ZoneId javaZone = toJavaZone(jodaZone);
|
||||||
.toLocalDateTime();
|
return toJavaInstant(localDateTime, jodaZone).atZone(javaZone).toLocalDateTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static java.time.ZoneId toJavaZone(org.joda.time.DateTimeZone jodaZone) {
|
||||||
|
return jodaZone.toTimeZone().toZoneId();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DateTimeZone toJodaTime(java.time.ZoneId zone) {
|
||||||
|
return org.joda.time.DateTimeZone.forID(zone.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
private DateTimeUtil() {
|
private DateTimeUtil() {
|
||||||
|
|
Loading…
Reference in New Issue