refactor: Optimised codes.

Move all private or protected to the last of files.
This commit is contained in:
Zihlu Wang
2024-03-31 17:47:42 +08:00
parent d593c83cc8
commit bb79c126fa
10 changed files with 296 additions and 283 deletions
@@ -44,13 +44,6 @@ import java.util.UUID;
@Slf4j
public final class AesUtil {
private AesUtil() {
}
private static final String AES = "AES";
private static final String AES_CBC_CIPHER = "AES/CBC/PKCS5Padding";
/**
* Encrypts the data using the AES algorithm with the given secret.
*
@@ -133,4 +126,20 @@ public final class AesUtil {
return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16);
}
/**
* Private constructor will protect this class from being instantiated.
*/
private AesUtil() {
}
/**
* The algorithm AES.
*/
private static final String AES = "AES";
/**
* The algorithm AES/CBC/PKCS5Padding.
*/
private static final String AES_CBC_CIPHER = "AES/CBC/PKCS5Padding";
}
@@ -56,14 +56,6 @@ import java.util.Objects;
*/
public final class Base64Util {
private static Base64.Encoder encoder;
private static Base64.Decoder decoder;
private static Base64.Encoder urlEncoder;
private static Base64.Decoder urlDecoder;
/**
* Ensure that there is only one Base64 Encoder.
*
@@ -210,4 +202,12 @@ public final class Base64Util {
return decodeUrlComponents(value, StandardCharsets.UTF_8);
}
private static Base64.Encoder encoder;
private static Base64.Decoder decoder;
private static Base64.Encoder urlEncoder;
private static Base64.Decoder urlDecoder;
}
@@ -77,11 +77,6 @@ import java.util.function.Supplier;
*/
public final class BranchUtil<T> {
/**
* The final result of the boolean expression.
*/
private final boolean result;
/**
* Create a {@code BranchUtil} instance.
*
@@ -239,4 +234,9 @@ public final class BranchUtil<T> {
handle(ifHandler, null);
}
/**
* The final result of the boolean expression.
*/
private final boolean result;
}
@@ -95,12 +95,6 @@ import java.util.function.Function;
@Getter
public final class ChainedCalcUtil {
/**
* -- GETTER --
* Returns the current value as a BigDecimal.
*/
private BigDecimal value;
/**
* Creates a {@code ChainedCalcUtil} instance with the specified initial
* value.
@@ -359,4 +353,10 @@ public final class ChainedCalcUtil {
return res;
}
/**
* -- GETTER --
* Returns the current value as a BigDecimal.
*/
private BigDecimal value;
}
@@ -69,48 +69,6 @@ import java.util.Optional;
*/
public final class HashUtil {
/**
* Private constructor to prevent instantiation
*/
private HashUtil() {
}
/**
* Calculates the hash value of the specified string using the specified
* algorithm and charset.
*
* @param method the hash algorithm to use
* @param value the string to calculate the hash value for
* @param charset the charset to use for encoding the string (default is
* UTF-8 if null)
* @return the hash value as a hexadecimal string, or an empty string if
* the algorithm is not available
* @throws RuntimeException if an unknown algorithm name is provided
* (should not occur under controlled usage)
*/
private static String hash(String method, String value, Charset charset) {
try {
var messageDigest = MessageDigest.getInstance(method);
messageDigest.update(value.getBytes(charset));
var bytes = messageDigest.digest();
var builder = new StringBuilder();
for (var b : bytes) {
var str = Integer.toHexString(b & 0xff);
if (str.length() == 1) {
builder.append(0);
}
builder.append(str);
}
return builder.toString();
} catch (NoSuchAlgorithmException ignored) {
// This should not occur under controlled usage
// Only trusted algorithms are allowed
return "";
}
}
/**
* Calculates the MD2 hash value of the specified string using the given
* charset.
@@ -286,4 +244,46 @@ public final class HashUtil {
return hash("SHA-512", value, StandardCharsets.UTF_8);
}
/**
* Private constructor will protect this class from being instantiated.
*/
private HashUtil() {
}
/**
* Calculates the hash value of the specified string using the specified
* algorithm and charset.
*
* @param method the hash algorithm to use
* @param value the string to calculate the hash value for
* @param charset the charset to use for encoding the string (default is
* UTF-8 if null)
* @return the hash value as a hexadecimal string, or an empty string if
* the algorithm is not available
* @throws RuntimeException if an unknown algorithm name is provided
* (should not occur under controlled usage)
*/
private static String hash(String method, String value, Charset charset) {
try {
var messageDigest = MessageDigest.getInstance(method);
messageDigest.update(value.getBytes(charset));
var bytes = messageDigest.digest();
var builder = new StringBuilder();
for (var b : bytes) {
var str = Integer.toHexString(b & 0xff);
if (str.length() == 1) {
builder.append(0);
}
builder.append(str);
}
return builder.toString();
} catch (NoSuchAlgorithmException ignored) {
// This should not occur under controlled usage
// Only trusted algorithms are allowed
return "";
}
}
}
@@ -37,12 +37,6 @@ import java.util.Map;
@Slf4j
public final class MapUtil {
/**
* Private constructor to prevent instantiation of MapUtil.
*/
private MapUtil() {
}
/**
* Converts an object to a map by mapping the field names to their
* corresponding values.
@@ -219,4 +213,10 @@ public final class MapUtil {
return String.valueOf(obj);
}
}
/**
* Private constructor will protect this class from being instantiated.
*/
private MapUtil() {
}
}