feature/net-util
ZhouXY108 2023-07-06 10:07:44 +08:00
commit 8cc55fbe7e
8 changed files with 66 additions and 21 deletions

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package xyz.zhouxy.plusone.commons.util;
package xyz.zhouxy.plusone.commons.collection;
import java.util.Collection;
import java.util.Collections;
@ -164,7 +164,7 @@ public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V
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 Consumer<K> keyChecker;
protected Consumer<V> valueChecker;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package xyz.zhouxy.plusone.commons.util;
package xyz.zhouxy.plusone.commons.collection;
import java.util.Collections;
import java.util.Comparator;

View File

@ -21,6 +21,7 @@ import java.util.regex.Pattern;
import com.fasterxml.jackson.annotation.JsonValue;
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) {
Assert.notNull(pattern, "The pattern must not be null.");
Assert.isNotBlank(value, "The value must be has text.");
Assert.isTrue(pattern.matcher(value).matches());
Assert.isTrue(RegexUtil.matches(value, pattern));
this.value = value;
}

View File

@ -21,7 +21,7 @@ package xyz.zhouxy.plusone.commons.exception;
*
* @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;

View File

@ -16,14 +16,16 @@
package xyz.zhouxy.plusone.commons.exception;
import javax.annotation.Nonnull;
/**
* {@code getCode}
* {@code code}
* 便 {@code code}
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
* @see BaseException
*/
public interface IWithCode {
int getCode();
public interface IWithCode<T> {
@Nonnull
T getCode();
}

View File

@ -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();
}

View File

@ -31,7 +31,7 @@ import java.util.Set;
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.OptionalUtil;

View File

@ -11,8 +11,12 @@ import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import org.joda.time.DateTimeZone;
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_CACHE = MapWrapper
@ -221,6 +225,7 @@ public class DateTimeUtil {
}
// ====================
// toJodaInstant
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(
java.time.LocalDateTime localDateTime,
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);
}
public static org.joda.time.DateTime toJodaDateTime(
java.time.Instant instant,
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);
}
@ -282,31 +287,39 @@ public class DateTimeUtil {
public static java.time.ZonedDateTime toZonedDateTime(
org.joda.time.LocalDateTime localDateTime,
org.joda.time.DateTimeZone dateTimeZone) {
java.time.ZoneId zone = dateTimeZone.toTimeZone().toZoneId();
java.time.ZoneId zone = toJavaZone(dateTimeZone);
return toJavaInstant(localDateTime, dateTimeZone).atZone(zone);
}
public static java.time.ZonedDateTime toZonedDateTime(
org.joda.time.Instant instant,
org.joda.time.DateTimeZone dateTimeZone) {
java.time.ZoneId zone = dateTimeZone.toTimeZone().toZoneId();
java.time.ZoneId zone = toJavaZone(dateTimeZone);
return toJavaInstant(instant).atZone(zone);
}
// toJodaLocalDateTime
public static org.joda.time.LocalDateTime toJodaLocalDateTime(java.time.LocalDateTime localDateTime) {
return toJodaInstant(localDateTime, ZoneId.systemDefault())
.toDateTime(org.joda.time.DateTimeZone.getDefault())
.toLocalDateTime();
java.time.ZoneId javaZone = java.time.ZoneId.systemDefault();
org.joda.time.DateTimeZone jodaZone = toJodaTime(javaZone);
return toJodaInstant(localDateTime, javaZone).toDateTime(jodaZone).toLocalDateTime();
}
// toJavaLocalDateTime
public static java.time.LocalDateTime toJavaLocalDateTime(org.joda.time.LocalDateTime localDateTime) {
return toJavaInstant(localDateTime, org.joda.time.DateTimeZone.getDefault())
.atZone(java.time.ZoneId.systemDefault())
.toLocalDateTime();
org.joda.time.DateTimeZone jodaZone = org.joda.time.DateTimeZone.getDefault();
java.time.ZoneId javaZone = toJavaZone(jodaZone);
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() {