docs(global): Optimised javadoc, changed the spelling to British English.
This commit is contained in:
@@ -34,12 +34,12 @@ import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* AES Util helps you encrypt and decrypt data with specified key and AES
|
||||
* algorithm.
|
||||
* {@link AesUtil} can help you encrypt and decrypt data with specified secret
|
||||
* by AES algorithm.
|
||||
*
|
||||
* @author hubin@baomidou
|
||||
* @since 1.1.0
|
||||
* @version 1.1.0
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Slf4j
|
||||
public final class AesUtil {
|
||||
@@ -52,17 +52,17 @@ public final class AesUtil {
|
||||
private static final String AES_CBC_CIPHER = "AES/CBC/PKCS5Padding";
|
||||
|
||||
/**
|
||||
* Encrypt the given data with given key with AES algorithm.
|
||||
* Encrypts the data using the AES algorithm with the given secret.
|
||||
*
|
||||
* @param data the data to be encrypted
|
||||
* @param key the key to encrypt the data
|
||||
* @param data the data to be encrypted
|
||||
* @param secret the secret to encrypt the data
|
||||
* @return the encryption result or {@code null} if encryption failed
|
||||
*/
|
||||
public static byte[] encrypt(byte[] data, byte[] key) {
|
||||
public static byte[] encrypt(byte[] data, byte[] secret) {
|
||||
try {
|
||||
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(key, AES).getEncoded(), AES);
|
||||
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES);
|
||||
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(key));
|
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(secret));
|
||||
return cipher.doFinal(data);
|
||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException | UnsupportedOperationException |
|
||||
InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException |
|
||||
@@ -76,17 +76,17 @@ public final class AesUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypt the given data with given key with AES algorithm.
|
||||
* Decrypts the data using the AES algorithm with the given secret.
|
||||
*
|
||||
* @param data the data to be decrypted
|
||||
* @param key the key to encrypt the data
|
||||
* @param secret the secret to encrypt the data
|
||||
* @return the decryption result or {@code null} if decryption failed
|
||||
*/
|
||||
public static byte[] decrypt(byte[] data, byte[] key) {
|
||||
public static byte[] decrypt(byte[] data, byte[] secret) {
|
||||
try {
|
||||
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(key, AES).getEncoded(), AES);
|
||||
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES);
|
||||
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(key));
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(secret));
|
||||
return cipher.doFinal(data);
|
||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException | UnsupportedOperationException |
|
||||
InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException |
|
||||
@@ -100,36 +100,36 @@ public final class AesUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt the given data with given key with AES algorithm.
|
||||
* Encrypts the data using the AES algorithm with the given secret.
|
||||
*
|
||||
* @param data the data to be encrypted
|
||||
* @param key the key to encrypt the data
|
||||
* @param secret the secret to encrypt the data
|
||||
* @return the encryption result or {@code null} if encryption failed
|
||||
*/
|
||||
public static String encrypt(String data, String key) {
|
||||
return Base64.getEncoder().encodeToString(encrypt(data.getBytes(StandardCharsets.UTF_8), key.getBytes(StandardCharsets.UTF_8)));
|
||||
public static String encrypt(String data, String secret) {
|
||||
return Base64.getEncoder().encodeToString(encrypt(data.getBytes(StandardCharsets.UTF_8), secret.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypt the given data with given key with AES algorithm.
|
||||
* Decrypts the data using the AES algorithm with the given secret.
|
||||
*
|
||||
* @param data the data to be decrypted
|
||||
* @param key the key to encrypt the data
|
||||
* @param secret the secret to encrypt the data
|
||||
* @return the decryption result or {@code null} if decryption failed
|
||||
*/
|
||||
public static String decrypt(String data, String key) {
|
||||
public static String decrypt(String data, String secret) {
|
||||
return new String(Objects.requireNonNull(
|
||||
decrypt(Base64.getDecoder().decode(data.getBytes()),
|
||||
key.getBytes(StandardCharsets.UTF_8)))
|
||||
secret.getBytes(StandardCharsets.UTF_8)))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates 16 characters-long random key.
|
||||
* Generates 16 characters-long random secret.
|
||||
*
|
||||
* @return the generated secure secret
|
||||
*/
|
||||
public static String generateRandomKey() {
|
||||
public static String generateRandomSecret() {
|
||||
return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16);
|
||||
}
|
||||
|
||||
|
||||
@@ -20,10 +20,11 @@ package cn.org.codecrafters.devkit.utils;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* The {@code Base64Util} class provides static methods to encode and decode
|
||||
* strings using Base64 encoding. It utilizes the {@link Base64} class from the
|
||||
* The {@link Base64Util} class provides static methods to encode and decode
|
||||
* strings with Base64 encoding. It utilizes the {@link Base64} class from the
|
||||
* Java standard library for performing the encoding and decoding operations.
|
||||
* This utility class offers convenient methods to encode and decode strings
|
||||
* with different character sets.
|
||||
@@ -55,6 +56,34 @@ import java.util.Base64;
|
||||
*/
|
||||
public final class Base64Util {
|
||||
|
||||
private static Base64.Encoder encoder;
|
||||
|
||||
private static Base64.Decoder decoder;
|
||||
|
||||
/**
|
||||
* Ensure that there is only one Base64 Encoder.
|
||||
*
|
||||
* @return the {@link Base64.Encoder} instance
|
||||
*/
|
||||
private static Base64.Encoder getEncoder() {
|
||||
if (Objects.isNull(encoder)) {
|
||||
encoder = Base64.getEncoder();
|
||||
}
|
||||
return encoder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that there is only one Base64 Encoder.
|
||||
*
|
||||
* @return the {@link Base64.Encoder} instance
|
||||
*/
|
||||
private static Base64.Decoder getDecoder() {
|
||||
if (Objects.isNull(decoder)) {
|
||||
decoder = Base64.getDecoder();
|
||||
}
|
||||
return decoder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Private constructor to prevent instantiation of the class.
|
||||
*/
|
||||
@@ -69,8 +98,7 @@ public final class Base64Util {
|
||||
* @return the Base64 encoded string
|
||||
*/
|
||||
public static String encode(String value, Charset charset) {
|
||||
var encoder = Base64.getEncoder();
|
||||
var encoded = encoder.encode(value.getBytes(charset));
|
||||
var encoded = getEncoder().encode(value.getBytes(charset));
|
||||
|
||||
return new String(encoded);
|
||||
}
|
||||
@@ -93,8 +121,7 @@ public final class Base64Util {
|
||||
* @return the decoded string
|
||||
*/
|
||||
public static String decode(String value, Charset charset) {
|
||||
var decoder = Base64.getDecoder();
|
||||
var decoded = decoder.decode(value.getBytes(charset));
|
||||
var decoded = getDecoder().decode(value.getBytes(charset));
|
||||
|
||||
return new String(decoded);
|
||||
}
|
||||
|
||||
@@ -23,24 +23,24 @@ import java.util.function.BooleanSupplier;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* The BranchUtil class provides static methods to simplify conditional logic
|
||||
* in Java development by leveraging lambda expressions. It offers convenient
|
||||
* methods to replace verbose if...else statements with more concise and
|
||||
* expressive functional constructs.
|
||||
* The {@link BranchUtil} class provides static methods to simplify conditional
|
||||
* logic in Java development by leveraging lambda expressions. It offers
|
||||
* convenient methods to replace verbose {@code if...else} statements with more
|
||||
* concise and expressive functional constructs.
|
||||
* <p>
|
||||
* Developers can use the methods in this utility class to streamline their
|
||||
* code, enhance readability, and promote a more functional style of
|
||||
* programming when dealing with branching logic and conditional statements.
|
||||
* Developers can use methods in this utility class to streamline their code,
|
||||
* enhance readability, and promote a more functional style of programming when
|
||||
* dealing with branching logic and conditional statements.
|
||||
* <p>
|
||||
* <b>Example:</b>
|
||||
* <pre>
|
||||
* // If you want to simplify an if (exp1 || exp2), you can use the
|
||||
* // following code:
|
||||
* var r1 = BranchUtil.or(1 == 1, 2 == 1)
|
||||
* String r1 = BranchUtil.or(1 == 1, 2 == 1)
|
||||
* .handle(() -> "1 is equal to 1 or 2 is equal to 1.");
|
||||
*
|
||||
* // If you have an else branch, you can use the following code:
|
||||
* var r2 = BranchUtil.or(1 == 1, 2 == 1)
|
||||
* String r2 = BranchUtil.or(1 == 1, 2 == 1)
|
||||
* .handle(() -> "1 is equal to 1 or 2 is equal to 1.",
|
||||
* () -> "1 is not equal to 1 and 2 is not equal to 1.");
|
||||
*
|
||||
|
||||
@@ -26,14 +26,12 @@ import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Utility class for chained high-precision calculations using BigDecimal.
|
||||
* <p>
|
||||
* The ChainedCalcUtil class provides a convenient way to perform chained
|
||||
* high-precision calculations using BigDecimal. It allows users to perform
|
||||
* mathematical operations such as addition, subtraction, multiplication,
|
||||
* and division with customisable precision and scale. By using this utility
|
||||
* class, developers can achieve accurate results and avoid precision loss
|
||||
* in their calculations.
|
||||
* The {@code ChainedCalcUtil} class provides a convenient way to perform
|
||||
* chained high-precision calculations using {@link BigDecimal}. It allows
|
||||
* users to perform mathematical operations such as addition, subtraction,
|
||||
* multiplication, and division with customisable precision and scale. By using
|
||||
* this utility class, developers can achieve accurate results and avoid
|
||||
* precision loss in their calculations.
|
||||
* <p>
|
||||
* <b>Usage:</b>
|
||||
* <pre>
|
||||
@@ -81,13 +79,13 @@ import java.util.function.Function;
|
||||
* .getValue(2);
|
||||
* </pre>
|
||||
* The above expressions perform various mathematical calculations using the
|
||||
* ChainedCalcUtil class.
|
||||
* {@code ChainedCalcUtil} class.
|
||||
* <p>
|
||||
* <b>Note:</b>
|
||||
* The ChainedCalcUtil class internally uses BigDecimal to handle
|
||||
* high-precision calculations. It is important to note that BigDecimal
|
||||
* operations can be memory-intensive and may have performance implications
|
||||
* for extremely large numbers or complex calculations.
|
||||
* The {@code ChainedCalcUtil} class internally uses {@link BigDecimal} to
|
||||
* handle high-precision calculations. It is important to note that {@link
|
||||
* BigDecimal} operations can be memory-intensive and may have performance
|
||||
* implications for extremely large numbers or complex calculations.
|
||||
*
|
||||
* @author sunzsh
|
||||
* @version 1.1.0
|
||||
@@ -104,7 +102,8 @@ public final class ChainedCalcUtil {
|
||||
private BigDecimal value;
|
||||
|
||||
/**
|
||||
* Creates a ChainedCalcUtil instance with the specified initial value.
|
||||
* Creates a {@code ChainedCalcUtil} instance with the specified initial
|
||||
* value.
|
||||
*
|
||||
* @param value the initial value for the calculation
|
||||
*/
|
||||
@@ -116,7 +115,7 @@ public final class ChainedCalcUtil {
|
||||
* Starts a chained calculation with the specified initial value.
|
||||
*
|
||||
* @param value the initial value for the calculation
|
||||
* @return a ChainedCalcUtil instance for performing chained calculations
|
||||
* @return a {@code ChainedCalcUtil} instance for performing chained calculations
|
||||
*/
|
||||
public static ChainedCalcUtil startWith(Number value) {
|
||||
return new ChainedCalcUtil(value);
|
||||
@@ -126,7 +125,7 @@ public final class ChainedCalcUtil {
|
||||
* Adds the specified value to the current value.
|
||||
*
|
||||
* @param other the value to be added
|
||||
* @return a ChainedCalcUtil instance with the updated value
|
||||
* @return a {@code ChainedCalcUtil} instance with the updated value
|
||||
*/
|
||||
public ChainedCalcUtil add(Number other) {
|
||||
return operator(BigDecimal::add, other);
|
||||
@@ -138,7 +137,7 @@ public final class ChainedCalcUtil {
|
||||
*
|
||||
* @param other the value to be added
|
||||
* @param beforeOperateScale the scale to be applied before the operation
|
||||
* @return a ChainedCalcUtil instance with the updated value
|
||||
* @return a {@code ChainedCalcUtil} instance with the updated value
|
||||
*/
|
||||
public ChainedCalcUtil add(Number other, Integer beforeOperateScale) {
|
||||
return operator(BigDecimal::add, other, beforeOperateScale);
|
||||
@@ -148,7 +147,7 @@ public final class ChainedCalcUtil {
|
||||
* Subtracts the specified value from the current value.
|
||||
*
|
||||
* @param other the value to be subtracted
|
||||
* @return a ChainedCalcUtil instance with the updated value
|
||||
* @return a {@code ChainedCalcUtil} instance with the updated value
|
||||
*/
|
||||
public ChainedCalcUtil subtract(Number other) {
|
||||
return operator(BigDecimal::subtract, other);
|
||||
@@ -160,7 +159,7 @@ public final class ChainedCalcUtil {
|
||||
*
|
||||
* @param other the value to be subtracted
|
||||
* @param beforeOperateScale the scale to be applied before the operation
|
||||
* @return a ChainedCalcUtil instance with the updated value
|
||||
* @return a {@code ChainedCalcUtil} instance with the updated value
|
||||
*/
|
||||
public ChainedCalcUtil subtract(Number other, Integer beforeOperateScale) {
|
||||
return operator(BigDecimal::subtract, other, beforeOperateScale);
|
||||
@@ -170,7 +169,7 @@ public final class ChainedCalcUtil {
|
||||
* Multiplies the current value by the specified value.
|
||||
*
|
||||
* @param other the value to be multiplied by
|
||||
* @return a ChainedCalcUtil instance with the updated value
|
||||
* @return a {@code ChainedCalcUtil} instance with the updated value
|
||||
*/
|
||||
public ChainedCalcUtil multiply(Number other) {
|
||||
return operator(BigDecimal::multiply, other);
|
||||
@@ -182,7 +181,7 @@ public final class ChainedCalcUtil {
|
||||
*
|
||||
* @param other the value to be multiplied by
|
||||
* @param beforeOperateScale the scale to be applied before the operation
|
||||
* @return a ChainedCalcUtil instance with the updated value
|
||||
* @return a {@code ChainedCalcUtil} instance with the updated value
|
||||
*/
|
||||
public ChainedCalcUtil multiply(Number other, Integer beforeOperateScale) {
|
||||
return operator(BigDecimal::multiply, other, beforeOperateScale);
|
||||
@@ -192,7 +191,7 @@ public final class ChainedCalcUtil {
|
||||
* Divides the current value by the specified value.
|
||||
*
|
||||
* @param other the value to divide by
|
||||
* @return a ChainedCalcUtil instance with the updated value
|
||||
* @return a {@code ChainedCalcUtil} instance with the updated value
|
||||
*/
|
||||
public ChainedCalcUtil divide(Number other) {
|
||||
return operator(BigDecimal::divide, other);
|
||||
@@ -204,7 +203,7 @@ public final class ChainedCalcUtil {
|
||||
*
|
||||
* @param other the value to divide by
|
||||
* @param beforeOperateScale the scale to be applied before the operation
|
||||
* @return a ChainedCalcUtil instance with the updated value
|
||||
* @return a {@code ChainedCalcUtil} instance with the updated value
|
||||
*/
|
||||
public ChainedCalcUtil divide(Number other, Integer beforeOperateScale) {
|
||||
return operator(BigDecimal::divide, other, beforeOperateScale);
|
||||
@@ -215,7 +214,7 @@ public final class ChainedCalcUtil {
|
||||
*
|
||||
* @param other the value to divide by
|
||||
* @param scale the scale for the result
|
||||
* @return a ChainedCalcUtil instance with the updated value
|
||||
* @return a {@code ChainedCalcUtil} instance with the updated value
|
||||
*/
|
||||
public ChainedCalcUtil divideWithScale(Number other, Integer scale) {
|
||||
return baseOperator(otherValue ->
|
||||
@@ -229,54 +228,54 @@ public final class ChainedCalcUtil {
|
||||
* @param other the value to divide by
|
||||
* @param scale the scale for the result
|
||||
* @param beforeOperateScale the scale to be applied before the operation
|
||||
* @return a ChainedCalcUtil instance with the updated value
|
||||
* @return a {@code ChainedCalcUtil} instance with the updated value
|
||||
*/
|
||||
public ChainedCalcUtil divideWithScale(Number other, Integer scale, Integer beforeOperateScale) {
|
||||
return baseOperator(otherValue -> this.value.divide(otherValue, scale, RoundingMode.HALF_UP), other, beforeOperateScale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current value as a BigDecimal with the specified scale.
|
||||
* Returns the current value as a {@link BigDecimal} with the specified scale.
|
||||
*
|
||||
* @param scale the scale for the result
|
||||
* @return the current value as a BigDecimal with the specified scale
|
||||
* @return the current value as a {@link BigDecimal} with the specified scale
|
||||
*/
|
||||
public BigDecimal getValue(int scale) {
|
||||
return value.setScale(scale, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current value as a Double.
|
||||
* Returns the current value as a {@link Double}.
|
||||
*
|
||||
* @return the current value as a Double
|
||||
* @return the current value as a {@link Double}
|
||||
*/
|
||||
public Double getDouble() {
|
||||
return getValue().doubleValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current value as a Double with the specified scale.
|
||||
* Returns the current value as a {@link Double} with the specified scale.
|
||||
*
|
||||
* @param scale the scale for the result
|
||||
* @return the current value as a Double with the specified scale
|
||||
* @return the current value as a {@link Double} with the specified scale
|
||||
*/
|
||||
public Double getDouble(int scale) {
|
||||
return getValue(scale).doubleValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current value as a Long.
|
||||
* Returns the current value as a {@link Long}.
|
||||
*
|
||||
* @return the current value as a Long
|
||||
* @return the current value as a {@link Long}
|
||||
*/
|
||||
public Long getLong() {
|
||||
return getValue().longValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current value as an Integer.
|
||||
* Returns the current value as an {@link Integer}.
|
||||
*
|
||||
* @return the current value as an Integer
|
||||
* @return the current value as an {@link Integer}
|
||||
*/
|
||||
public Integer getInteger() {
|
||||
return getValue().intValue();
|
||||
@@ -332,7 +331,7 @@ public final class ChainedCalcUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the specified value to a BigDecimal.
|
||||
* Converts the specified value to a {@link BigDecimal}.
|
||||
*
|
||||
* @param value the value to convert
|
||||
* @param scale the scale to apply to the resulting BigDecimal, or null if
|
||||
|
||||
@@ -25,12 +25,10 @@ import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Utility class for performing hash operations on strings.
|
||||
* <p>
|
||||
* The HashUtil class provides convenient methods for calculating various hash
|
||||
* functions on strings, including MD2, MD5, SHA-1, SHA-224, SHA-256, SHA-384,
|
||||
* and SHA-512. It allows developers to easily obtain the hash value of a given
|
||||
* string using different algorithms.
|
||||
* The {@code HashUtil} class provides convenient methods for calculating
|
||||
* various hash functions on strings, including MD2, MD5, SHA-1, SHA-224,
|
||||
* SHA-256, SHA-384, and SHA-512. It allows developers to easily obtain the
|
||||
* hash value of a given string using different algorithms.
|
||||
* <p>
|
||||
* Example usage:
|
||||
* <pre>
|
||||
@@ -55,8 +53,8 @@ import java.util.Optional;
|
||||
* // Perform SHA-512 hash operation
|
||||
* String sha512Hash = HashUtil.sha512("someString");
|
||||
* </pre>
|
||||
* The above examples demonstrate how to use the HashUtil class to calculate
|
||||
* hash values for a given string using different algorithms.
|
||||
* The above examples demonstrate how to use the {@code HashUtil} class to
|
||||
* calculate hash values for a given string using different algorithms.
|
||||
* <p>
|
||||
* <b>Note:</b>
|
||||
* The hash functions provided by the HashUtil class are one-way hash
|
||||
|
||||
@@ -24,8 +24,8 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* MapUtil is a utility class that provides methods for converting objects to
|
||||
* maps and maps to objects.
|
||||
* {@code MapUtil} is a utility class that provides methods for converting
|
||||
* objects to maps and maps to objects.
|
||||
* <p>
|
||||
* It also provides methods for getting and setting field values using
|
||||
* reflection.
|
||||
@@ -207,10 +207,10 @@ public final class MapUtil {
|
||||
/**
|
||||
* Casts the specified value to the required type.
|
||||
*
|
||||
* @param value the value to be casted
|
||||
* @param requiredType the type to which the value should be casted
|
||||
* @param <T> the type to which the value should be casted
|
||||
* @return the casted value, or null if the value cannot be casted to the
|
||||
* @param value the value to be cast
|
||||
* @param requiredType the type to which the value should be cast
|
||||
* @param <T> the type to which the value should be cast
|
||||
* @return the cast value, or null if the value cannot be cast to the
|
||||
* required type
|
||||
*/
|
||||
public static <T> T cast(Object value, Class<T> requiredType) {
|
||||
|
||||
Reference in New Issue
Block a user