forked from plusone/plusone-commons
Compare commits
7 Commits
feature/se
...
feature/se
Author | SHA1 | Date | |
---|---|---|---|
53f31602d2 | |||
1c4b0631f9 | |||
5ba888a9f2 | |||
1976f7d828 | |||
cc10f11b37 | |||
ce62bdcdc6 | |||
94d34faffd |
12
pom.xml
12
pom.xml
@@ -1,7 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>xyz.zhouxy.plusone</groupId>
|
<groupId>xyz.zhouxy.plusone</groupId>
|
||||||
@@ -18,6 +17,7 @@
|
|||||||
<guava.version>32.0.1-jre</guava.version>
|
<guava.version>32.0.1-jre</guava.version>
|
||||||
<google-jsr305.version>3.0.2</google-jsr305.version>
|
<google-jsr305.version>3.0.2</google-jsr305.version>
|
||||||
<joda-time.version>2.12.5</joda-time.version>
|
<joda-time.version>2.12.5</joda-time.version>
|
||||||
|
<jbcrypt.version>0.4</jbcrypt.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@@ -52,6 +52,14 @@
|
|||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mindrot</groupId>
|
||||||
|
<artifactId>jbcrypt</artifactId>
|
||||||
|
<version>${jbcrypt.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- test use only -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>ch.qos.logback</groupId>
|
<groupId>ch.qos.logback</groupId>
|
||||||
<artifactId>logback-classic</artifactId>
|
<artifactId>logback-classic</artifactId>
|
||||||
|
@@ -5,7 +5,15 @@ import java.lang.annotation.Retention;
|
|||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
// TODO 添加 Javadoc
|
/**
|
||||||
|
* ReaderMethod
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 标识方法是读方法,如 getter。
|
||||||
|
*
|
||||||
|
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||||
|
* @since 1.0
|
||||||
|
*/
|
||||||
@Target(ElementType.METHOD)
|
@Target(ElementType.METHOD)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface ReaderMethod {
|
public @interface ReaderMethod {
|
||||||
|
@@ -5,7 +5,15 @@ import java.lang.annotation.Retention;
|
|||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
// TODO 添加 Javadoc
|
/**
|
||||||
|
* WriterMethod
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 标识方法是写方法,如 setter。
|
||||||
|
*
|
||||||
|
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||||
|
* @since 1.0
|
||||||
|
*/
|
||||||
@Target(ElementType.METHOD)
|
@Target(ElementType.METHOD)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface WriterMethod {
|
public @interface WriterMethod {
|
||||||
|
@@ -22,9 +22,21 @@ import java.util.function.Function;
|
|||||||
|
|
||||||
import javax.annotation.concurrent.ThreadSafe;
|
import javax.annotation.concurrent.ThreadSafe;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.commons.base.JRE;
|
||||||
import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapUtil;
|
import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapUtil;
|
||||||
|
|
||||||
// TODO 添加文档注释
|
/**
|
||||||
|
* SafeConcurrentHashMap
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Java 8 的 {@link ConcurrentHashMap#computeIfAbsent(Object, Function)} 方法有 bug,
|
||||||
|
* 使用 Java 8 时,可使用这个类进行替换。
|
||||||
|
*
|
||||||
|
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||||
|
* @since 1.0
|
||||||
|
* @see ConcurrentHashMap
|
||||||
|
* @see ConcurrentHashMapUtil#computeIfAbsentForJava8(ConcurrentHashMap, Object, Function)
|
||||||
|
*/
|
||||||
@ThreadSafe
|
@ThreadSafe
|
||||||
public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
|
public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
|
||||||
|
|
||||||
@@ -102,6 +114,8 @@ public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
|
|||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
|
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
|
||||||
return ConcurrentHashMapUtil.computeIfAbsentForJava8(this, key, mappingFunction);
|
return JRE.isJava8()
|
||||||
|
? ConcurrentHashMapUtil.computeIfAbsentForJava8(this, key, mappingFunction)
|
||||||
|
: super.computeIfAbsent(key, mappingFunction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -27,7 +27,7 @@ import java.util.Objects;
|
|||||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||||
*/
|
*/
|
||||||
public abstract class BaseException
|
public abstract class BaseException
|
||||||
extends RuntimeException
|
extends Exception
|
||||||
implements IWithCode<String> {
|
implements IWithCode<String> {
|
||||||
|
|
||||||
private static final long serialVersionUID = -2546365325001947203L;
|
private static final long serialVersionUID = -2546365325001947203L;
|
||||||
|
@@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 带错误码的异常。
|
||||||
|
*
|
||||||
|
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||||
|
*/
|
||||||
|
public abstract class BaseRuntimeException
|
||||||
|
extends RuntimeException
|
||||||
|
implements IWithCode<String> {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -6345888403567792664L;
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
private final String code;
|
||||||
|
|
||||||
|
protected BaseRuntimeException(String code, String msg) {
|
||||||
|
super(msg);
|
||||||
|
this.code = Objects.requireNonNull(code);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected BaseRuntimeException(String code, Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
this.code = Objects.requireNonNull(code);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected BaseRuntimeException(String code, String msg, Throwable cause) {
|
||||||
|
super(msg, cause);
|
||||||
|
this.code = Objects.requireNonNull(code);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public final String getCode() {
|
||||||
|
return this.code;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,60 @@
|
|||||||
|
package xyz.zhouxy.plusone.commons.security;
|
||||||
|
|
||||||
|
import com.google.common.hash.Hasher;
|
||||||
|
import com.google.common.hash.Hashing;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 使用摘要算法推荐使用 guava 的 hash 包。
|
||||||
|
* </p>
|
||||||
|
* <p>
|
||||||
|
* 密码加密推荐使用 {@link org.mindrot.jbcrypt.BCrypt}。
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
|
public class Hashers {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回 guava 的 {@link Hasher}
|
||||||
|
*/
|
||||||
|
public static Hasher sha256() {
|
||||||
|
return Hashing.sha256().newHasher();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回 guava 的 {@link Hasher}
|
||||||
|
*/
|
||||||
|
public static Hasher sha384() {
|
||||||
|
return Hashing.sha384().newHasher();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回 guava 的 {@link Hasher}
|
||||||
|
*/
|
||||||
|
public static Hasher sha512() {
|
||||||
|
return Hashing.sha512().newHasher();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回 guava 的 {@link Hasher}
|
||||||
|
*
|
||||||
|
* @deprecated 该算法已弃用
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public static Hasher sha1() {
|
||||||
|
return Hashing.sha1().newHasher();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回 guava 的 {@link Hasher}
|
||||||
|
*
|
||||||
|
* @deprecated 该算法已弃用
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public static Hasher md5() {
|
||||||
|
return Hashing.md5().newHasher();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Hashers() {
|
||||||
|
throw new IllegalStateException("Utility class");
|
||||||
|
}
|
||||||
|
}
|
93
src/main/java/xyz/zhouxy/plusone/commons/security/RSA.java
Normal file
93
src/main/java/xyz/zhouxy/plusone/commons/security/RSA.java
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
package xyz.zhouxy.plusone.commons.security;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.GeneralSecurityException;
|
||||||
|
import java.security.KeyPair;
|
||||||
|
import java.security.KeyPairGenerator;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.PrivateKey;
|
||||||
|
import java.security.PublicKey;
|
||||||
|
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
|
||||||
|
import com.google.common.annotations.Beta;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RSA 非对称加密。
|
||||||
|
*/
|
||||||
|
@Beta
|
||||||
|
public class RSA {
|
||||||
|
private static final int DEFAULT_KEY_SIZE = 2048;
|
||||||
|
public static final String NAME = "RSA";
|
||||||
|
private static final String TRANSFORMATION = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding";
|
||||||
|
|
||||||
|
private final PublicKey publicKey;
|
||||||
|
private final PrivateKey privateKey;
|
||||||
|
|
||||||
|
private RSA(final PublicKey publicKey, final PrivateKey privateKey) {
|
||||||
|
this.publicKey = publicKey;
|
||||||
|
this.privateKey = privateKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RSA of(final PublicKey publicKey, final PrivateKey privateKey) {
|
||||||
|
return new RSA(publicKey, privateKey);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RSA of(final KeyPair keyPair) {
|
||||||
|
return new RSA(keyPair.getPublic(), keyPair.getPrivate());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RSA withKeySize(int keySize) {
|
||||||
|
try {
|
||||||
|
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(NAME);
|
||||||
|
keyPairGenerator.initialize(keySize);
|
||||||
|
final KeyPair keyPair = keyPairGenerator.generateKeyPair();
|
||||||
|
return new RSA(keyPair.getPublic(), keyPair.getPrivate());
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
throw new SecurityException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RSA withDefaultKeySize() {
|
||||||
|
return withKeySize(DEFAULT_KEY_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PrivateKey getPrivateKey() {
|
||||||
|
return privateKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PublicKey getPublicKey() {
|
||||||
|
return publicKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] encrypt(byte[] input) throws GeneralSecurityException {
|
||||||
|
Cipher encryptModeCipher = Cipher.getInstance(TRANSFORMATION);
|
||||||
|
encryptModeCipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||||
|
return encryptModeCipher.doFinal(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] encrypt(String input) throws GeneralSecurityException {
|
||||||
|
return encrypt(input.getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
public String encryptToString(String input) throws GeneralSecurityException {
|
||||||
|
return new BigInteger(1, encrypt(input)).toString(16);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] decrypt(byte[] input) throws GeneralSecurityException {
|
||||||
|
Cipher decryptModeCipher = Cipher.getInstance(TRANSFORMATION);
|
||||||
|
decryptModeCipher.init(Cipher.DECRYPT_MODE, privateKey);
|
||||||
|
return decryptModeCipher.doFinal(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] decrypt(String input) throws GeneralSecurityException {
|
||||||
|
return decrypt(new BigInteger(input, 16).toByteArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
public String decryptToString(String input) throws GeneralSecurityException {
|
||||||
|
return new String(decrypt(input));
|
||||||
|
}
|
||||||
|
}
|
@@ -1,133 +0,0 @@
|
|||||||
package xyz.zhouxy.plusone.commons.seq;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.StringJoiner;
|
|
||||||
import java.util.function.BiFunction;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.function.Predicate;
|
|
||||||
|
|
||||||
import com.google.common.annotations.Beta;
|
|
||||||
import com.google.common.collect.ImmutableList;
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
|
||||||
|
|
||||||
@Beta
|
|
||||||
@FunctionalInterface
|
|
||||||
public interface Seq<T> extends Consumer<Consumer<T>> {
|
|
||||||
// 这个方法和 forEach 是完全等价的
|
|
||||||
void accept(Consumer<T> consumer);
|
|
||||||
|
|
||||||
static <T> Seq<T> from(Collection<T> collection) {
|
|
||||||
return collection::forEach;
|
|
||||||
}
|
|
||||||
|
|
||||||
static <T> Seq<T> unit(T t) {
|
|
||||||
return c -> c.accept(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
default <E> Seq<E> map(Function<T, E> func) {
|
|
||||||
Seq<T> srcSeq = this;
|
|
||||||
return c -> srcSeq.accept((T t) -> c.accept(func.apply(t)));
|
|
||||||
}
|
|
||||||
|
|
||||||
default <E> Seq<E> flatMap(Function<T, Seq<E>> function) {
|
|
||||||
Seq<T> srcSeq = this;
|
|
||||||
return c -> srcSeq.accept(t -> function.apply(t).accept(c));
|
|
||||||
}
|
|
||||||
|
|
||||||
default Seq<T> filter(Predicate<T> predicate) {
|
|
||||||
Seq<T> srcSeq = this;
|
|
||||||
return (Consumer<T> c) -> srcSeq.accept((T t) -> {
|
|
||||||
if (predicate.test(t)) {
|
|
||||||
c.accept(t);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
static void stop() {
|
|
||||||
throw StopException.INSTANCE;
|
|
||||||
}
|
|
||||||
|
|
||||||
default void consumeTillStop(Consumer<T> consumer) {
|
|
||||||
try {
|
|
||||||
accept(consumer);
|
|
||||||
} catch (StopException ignore) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
default Seq<T> take(int n) {
|
|
||||||
return c -> {
|
|
||||||
int[] i = { n };
|
|
||||||
consumeTillStop(t -> {
|
|
||||||
if (i[0]-- > 0) {
|
|
||||||
c.accept(t);
|
|
||||||
} else {
|
|
||||||
stop();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
default Seq<T> drop(int n) {
|
|
||||||
return c -> {
|
|
||||||
int[] a = { n - 1 };
|
|
||||||
accept(t -> {
|
|
||||||
if (a[0] < 0) {
|
|
||||||
c.accept(t);
|
|
||||||
} else {
|
|
||||||
a[0]--;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
default Seq<T> onEach(Consumer<T> consumer) {
|
|
||||||
Seq<T> srcSeq = this;
|
|
||||||
return c -> srcSeq.accept(consumer.andThen(c));
|
|
||||||
}
|
|
||||||
|
|
||||||
default <E, R> Seq<R> zip(Iterable<E> iterable, BiFunction<T, E, R> function) {
|
|
||||||
return c -> {
|
|
||||||
Iterator<E> iterator = iterable.iterator();
|
|
||||||
consumeTillStop(t -> {
|
|
||||||
if (iterator.hasNext()) {
|
|
||||||
c.accept(function.apply(t, iterator.next()));
|
|
||||||
} else {
|
|
||||||
stop();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
default String join(String sep) {
|
|
||||||
StringJoiner joiner = new StringJoiner(sep);
|
|
||||||
accept(t -> joiner.add(t.toString()));
|
|
||||||
return joiner.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
default List<T> toList() {
|
|
||||||
ImmutableList.Builder<T> builder = ImmutableList.builder();
|
|
||||||
accept(builder::add);
|
|
||||||
return builder.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
default Set<T> toSet() {
|
|
||||||
ImmutableSet.Builder<T> set = ImmutableSet.builder();
|
|
||||||
accept(set::add);
|
|
||||||
return set.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
static class StopException extends RuntimeException {
|
|
||||||
private static final long serialVersionUID = -8975705275260458012L;
|
|
||||||
private static final StopException INSTANCE = new StopException();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public synchronized Throwable fillInStackTrace() {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -21,12 +21,28 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.base.JRE;
|
import xyz.zhouxy.plusone.commons.base.JRE;
|
||||||
|
import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap;
|
||||||
|
|
||||||
public class ConcurrentHashMapUtil { // TODO 添加文档注释
|
/**
|
||||||
|
* ConcurrentHashMapUtil
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Java 8 的 {@link ConcurrentHashMap#computeIfAbsent(Object, Function)} 方法有 bug,
|
||||||
|
* 可使用这个工具类的 {@link computeIfAbsentForJava8} 进行替换。
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* <b>NOTE: 方法来自Dubbo,见:issues#2349</b>
|
||||||
|
*
|
||||||
|
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||||
|
* @since 1.0
|
||||||
|
* @see ConcurrentHashMap
|
||||||
|
* @see SafeConcurrentHashMap
|
||||||
|
*/
|
||||||
|
public class ConcurrentHashMapUtil {
|
||||||
|
|
||||||
public static <K, V> V computeIfAbsent(ConcurrentHashMap<K, V> map, final K key,
|
public static <K, V> V computeIfAbsent(ConcurrentHashMap<K, V> map, final K key,
|
||||||
final Function<? super K, ? extends V> mappingFunction) {
|
final Function<? super K, ? extends V> mappingFunction) {
|
||||||
|
Objects.requireNonNull(map, "map");
|
||||||
return JRE.isJava8()
|
return JRE.isJava8()
|
||||||
? computeIfAbsentForJava8(map, key, mappingFunction)
|
? computeIfAbsentForJava8(map, key, mappingFunction)
|
||||||
: map.computeIfAbsent(key, mappingFunction);
|
: map.computeIfAbsent(key, mappingFunction);
|
||||||
@@ -34,6 +50,7 @@ public class ConcurrentHashMapUtil { // TODO 添加文档注释
|
|||||||
|
|
||||||
public static <K, V> V computeIfAbsentForJava8(ConcurrentHashMap<K, V> map, final K key,
|
public static <K, V> V computeIfAbsentForJava8(ConcurrentHashMap<K, V> map, final K key,
|
||||||
final Function<? super K, ? extends V> mappingFunction) {
|
final Function<? super K, ? extends V> mappingFunction) {
|
||||||
|
Objects.requireNonNull(key);
|
||||||
Objects.requireNonNull(mappingFunction);
|
Objects.requireNonNull(mappingFunction);
|
||||||
V v = map.get(key);
|
V v = map.get(key);
|
||||||
if (null == v) {
|
if (null == v) {
|
||||||
|
23
src/test/java/xyz/zhouxy/plusone/commons/SerialTests.java
Normal file
23
src/test/java/xyz/zhouxy/plusone/commons/SerialTests.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package xyz.zhouxy.plusone.commons;
|
||||||
|
|
||||||
|
import java.io.ObjectStreamClass;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import xyz.zhouxy.plusone.commons.exception.BaseRuntimeException;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
class SerialTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSerialVersionUID() {
|
||||||
|
long uid = getSerialVersionUID(BaseRuntimeException.class);
|
||||||
|
log.info("\n private static final long serialVersionUID = {}L;", uid);
|
||||||
|
}
|
||||||
|
|
||||||
|
private long getSerialVersionUID(Class<?> cl) {
|
||||||
|
ObjectStreamClass c = ObjectStreamClass.lookup(cl);
|
||||||
|
return c.getSerialVersionUID();
|
||||||
|
}
|
||||||
|
}
|
@@ -1,56 +0,0 @@
|
|||||||
package xyz.zhouxy.plusone.commons.seq;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.TreeSet;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
class SeqTests {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testSeq() {
|
|
||||||
List<Integer> list = ImmutableList.of(108, 2333, 666, 555, 1, 2, 3, 23, 78, 65, 77, 22, 108);
|
|
||||||
List<String> result = new ArrayList<>();
|
|
||||||
Seq<String> targetSeq = new Seq<String>() {
|
|
||||||
@Override
|
|
||||||
public void accept(Consumer<String> c) {
|
|
||||||
list.forEach(t -> {
|
|
||||||
c.accept(String.valueOf(t + 250));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
targetSeq.accept(result::add);
|
|
||||||
// list.forEach(t -> {
|
|
||||||
// result.add(t + 250 + "");
|
|
||||||
// });
|
|
||||||
log.info("{}", result);
|
|
||||||
|
|
||||||
for (Integer i : produceEvenNumbers(9)) {
|
|
||||||
System.out.print(i);
|
|
||||||
System.out.print(" ");
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
|
|
||||||
Set<String> set = new TreeSet<>();
|
|
||||||
Seq<String> s = result::forEach;
|
|
||||||
s.accept(set::add);
|
|
||||||
log.info("{}", set);
|
|
||||||
}
|
|
||||||
|
|
||||||
Iterable<Integer> produceEvenNumbers(int upto) {
|
|
||||||
Seq<Integer> seq = c -> {
|
|
||||||
for (int i = 0; i <= upto; i += 2) {
|
|
||||||
c.accept(i);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return seq.toList();
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user