This commit is contained in:
2023-07-06 10:07:44 +08:00
8 changed files with 66 additions and 21 deletions

View File

@@ -1,208 +0,0 @@
/*
* 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.util;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.annotation.Nullable;
import com.google.common.annotations.Beta;
@Beta
public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V, T>> {
private final Map<K, V> map;
private final Consumer<K> keyChecker;
private final Consumer<V> valueChecker;
protected AbstractMapWrapper(Map<K, V> map, @Nullable Consumer<K> keyChecker, @Nullable Consumer<V> valueChecker) {
this.map = map;
this.keyChecker = keyChecker;
this.valueChecker = valueChecker;
}
public final T put(K key, V value) {
if (this.keyChecker != null) {
this.keyChecker.accept(key);
}
if (this.valueChecker != null) {
this.valueChecker.accept(value);
}
this.map.put(key, value);
return getSelf();
}
public final T putAll(Map<? extends K, ? extends V> m) {
for (Entry<? extends K, ? extends V> entry : m.entrySet()) {
put(entry.getKey(), entry.getValue());
}
return getSelf();
}
/**
* 获取 {@code map} 中的值。如果 {@code key} 不存在,则抛出异常。
* 将 {@code value}(可为 {@code null})装进 {@link Optional} 中后返回。
* <i>为了这碟醋包的这盘饺子。</i>
*
* @param key 键
* @return 可缺失的值
* @throws IllegalArgumentException key 不存在时抛出。
*/
public Optional<V> get(K key) {
if (!this.map.containsKey(key)) {
throw new IllegalArgumentException("Key does not exist");
}
return Optional.ofNullable(this.map.get(key));
}
/**
* 获取 {@code map} 中的值。如果 {@code key} 不存在,则抛出异常。
*
* @param key 键
* @return 值
* @throws IllegalArgumentException key 不存在时抛出。
*/
@Nullable
public V getOrNull(K key) {
if (!this.map.containsKey(key)) {
throw new IllegalArgumentException("Key does not exist");
}
return this.map.get(key);
}
@SuppressWarnings("unchecked")
public final <R> Optional<R> getAndConvert(K key) {
return get(key).map(v -> (R) v);
}
public final <R> Optional<R> getAndConvert(K key, Function<V, R> mapper) {
return get(key).map(mapper);
}
public final boolean containsKey(Object key) {
return this.map.containsKey(key);
}
public final int size() {
return this.map.size();
}
public final Set<K> keySet() {
return this.map.keySet();
}
public final Collection<V> values() {
return this.map.values();
}
public final Set<Entry<K, V>> entrySet() {
return this.map.entrySet();
}
public final void clear() {
this.map.clear();
}
public final boolean containsValue(Object value) {
return this.map.containsValue(value);
}
public final boolean isEmpty() {
return this.map.isEmpty();
}
public final V remove(Object key) {
return this.map.remove(key);
}
public final V putIfAbsent(K key, V value) {
return this.map.putIfAbsent(key, value);
}
public final V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
V v = this.map.get(key);
if (null == v) {
this.map.putIfAbsent(key, mappingFunction.apply(key));
v = this.map.get(key);
}
return v;
}
public final Map<K, V> exportMap() {
return this.map;
}
public final Map<K, V> exportUnmodifiableMapMap() {
return Collections.unmodifiableMap(this.map);
}
protected abstract T getSelf();
@Override
public String toString() {
return this.map.toString();
}
protected 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;
protected Builder(Map<K, V> map) {
this.map = map;
}
public Builder<K, V, T> keyChecker(@Nullable Consumer<K> keyChecker) {
this.keyChecker = keyChecker;
return this;
}
public Builder<K, V, T> valueChecker(@Nullable Consumer<V> valueChecker) {
this.valueChecker = valueChecker;
return this;
}
public Builder<K, V, T> put(K key, V value) {
if (this.keyChecker != null) {
this.keyChecker.accept(key);
}
if (this.valueChecker != null) {
this.valueChecker.accept(value);
}
this.map.put(key, value);
return this;
}
public Builder<K, V, T> putAll(Map<? extends K, ? extends V> m) {
for (Entry<? extends K, ? extends V> entry : m.entrySet()) {
put(entry.getKey(), entry.getValue());
}
return this;
}
public abstract T build();
public abstract T buildUnmodifiableMap();
}
}

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
@@ -114,7 +118,7 @@ public class DateTimeUtil {
* </p>
*
* @param timeMillis 时间戳
* @param zone 时区
* @param zone 时区
* @return 带时区信息的地区时间
*/
public static ZonedDateTime toZonedDateTime(long timeMillis, ZoneId zone) {
@@ -179,7 +183,7 @@ public class DateTimeUtil {
* 获取时间戳在指定时区的地区时间。
*
* @param timeMillis 时间戳
* @param zone 时区
* @param zone 时区
* @return 地区时间
*/
public static LocalDateTime toLocalDateTime(long timeMillis, ZoneId zone) {
@@ -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() {

View File

@@ -1,85 +0,0 @@
/*
* 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.util;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.function.Consumer;
import com.google.common.annotations.Beta;
@Beta
public final class MapWrapper<K, V> extends AbstractMapWrapper<K, V, MapWrapper<K, V>> {
private MapWrapper(Map<K, V> map, Consumer<K> keyChecker, Consumer<V> valueChecker) {
super(map, keyChecker, valueChecker);
}
public static <K, V> Builder<K, V> wrap(Map<K, V> map) {
return new Builder<>(map);
}
public static <K, V> Builder<K, V> wrapHashMap() {
return new Builder<>(new HashMap<>());
}
public static <K, V> Builder<K, V> wrapHashMap(int initialCapacity) {
return new Builder<>(new HashMap<>(initialCapacity));
}
public static <K, V> Builder<K, V> wrapHashMap(int initialCapacity, float loadFactor) {
return new Builder<>(new HashMap<>(initialCapacity, loadFactor));
}
public static <K extends Comparable<? super K>, V> Builder<K, V> wrapTreeMap() {
return new Builder<>(new TreeMap<>());
}
public static <K, V> Builder<K, V> wrapTreeMap(SortedMap<K, ? extends V> m) {
return new Builder<>(new TreeMap<>(m));
}
public static <K, V> Builder<K, V> wrapTreeMap(Comparator<? super K> comparator) {
return new Builder<>(new TreeMap<>(comparator));
}
public static final class Builder<K, V> extends AbstractMapWrapper.Builder<K, V, MapWrapper<K, V>> {
private Builder(Map<K, V> map) {
super(map);
}
@Override
public MapWrapper<K, V> build() {
return new MapWrapper<>(map, keyChecker, valueChecker);
}
@Override
public MapWrapper<K, V> buildUnmodifiableMap() {
return new MapWrapper<>(Collections.unmodifiableMap(map), keyChecker, valueChecker);
}
}
@Override
protected MapWrapper<K, V> getSelf() {
return this;
}
}