添加 UnifiedResponse 以替代 RestfulResult;重构 RegexUtil。 (#4)

Reviewed-on: http://zhouxy.xyz:3000/plusone/plusone-commons/pulls/4
This commit is contained in:
2023-08-09 20:23:31 +08:00
parent f2aba52c4c
commit ef43b4dd87
12 changed files with 611 additions and 100 deletions

View File

@@ -22,6 +22,7 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Function;
@@ -29,6 +30,8 @@ import javax.annotation.Nullable;
import com.google.common.annotations.Beta;
import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapUtil;
@Beta
public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V, T>> {
@@ -141,12 +144,12 @@ public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V
}
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);
if (this.map instanceof ConcurrentHashMap) {
return ConcurrentHashMapUtil.computIfAbsent(
(ConcurrentHashMap<K, V>) this.map, key, mappingFunction);
} else {
return this.map.computeIfAbsent(key, mappingFunction);
}
return v;
}
public final Map<K, V> exportMap() {

View File

@@ -1,14 +1,30 @@
/*
* 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.collection;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import javax.annotation.concurrent.ThreadSafe;
import xyz.zhouxy.plusone.commons.base.JRE;
import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapUtil;
// TODO 添加文档注释
@ThreadSafe
public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
@@ -86,27 +102,6 @@ public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
/** {@inheritDoc} */
@Override
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
Objects.requireNonNull(mappingFunction);
if (JRE.isJava8()) {
V v = get(key);
if (null == v) {
// this bug fix methods maybe cause `mappingFunction.apply` multiple calls.
v = mappingFunction.apply(key);
if (null == v) {
return null;
}
final V res = putIfAbsent(key, v);
if (null != res) {
// if pre value present, means other thread put value already,
// and putIfAbsent not effect
// return exist value
return res;
}
// if pre value is null, means putIfAbsent effected, return current value
}
return v;
} else {
return computeIfAbsent(key, mappingFunction);
}
return ConcurrentHashMapUtil.computIfAbsent(this, key, mappingFunction);
}
}