docs(global): Improve documentation comments
Enriched the content of the documentation to ensure every package and class has proper documentation comments; Modified the formatting for consistent and neat presentation.
This commit is contained in:
@@ -24,15 +24,18 @@ import java.util.Base64;
|
||||
/**
|
||||
* Utility class for Base64 encoding and decoding of strings.
|
||||
* <p>
|
||||
* The {@code Base64Util} class provides static methods to encode and decode strings using 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.
|
||||
* The {@code Base64Util} class provides static methods to encode and decode
|
||||
* strings using 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.
|
||||
* <p>
|
||||
* This class is designed as a final class with a private constructor to prevent instantiation.
|
||||
* All methods in this class are static, allowing easy access to the Base64 encoding and decoding functionality.
|
||||
* This class is designed as a final class with a private constructor to
|
||||
* prevent instantiation. All methods in this class are static, allowing easy
|
||||
* access to the Base64 encoding and decoding functionality.
|
||||
* <p>
|
||||
* Example usage:
|
||||
* <pre>{@code
|
||||
* <pre>
|
||||
* String original = "Hello, World!";
|
||||
*
|
||||
* // Encode the string using UTF-8 charset
|
||||
@@ -42,17 +45,15 @@ import java.util.Base64;
|
||||
* // Decode the encoded string using UTF-8 charset
|
||||
* String decoded = Base64Util.decode(encoded);
|
||||
* System.out.println("Decoded string: " + decoded);
|
||||
* }</pre>
|
||||
* </pre>
|
||||
* <p>
|
||||
* <b>Note:</b> This utility class uses the default charset (UTF-8) if no specific charset is provided.
|
||||
* It is recommended to specify the charset explicitly to ensure consistent encoding and decoding.
|
||||
* <p>
|
||||
* <b>Thread Safety:</b> The methods in this class are thread-safe.
|
||||
* Multiple threads can safely use the methods for encoding and decoding strings simultaneously.
|
||||
* <b>Note:</b> This utility class uses the default charset (UTF-8) if no
|
||||
* specific charset is provided. It is recommended to specify the charset
|
||||
* explicitly to ensure consistent encoding and decoding.
|
||||
*
|
||||
* @since 10 Jul 2023
|
||||
* @author Zihlu Wang
|
||||
* @version 1.0.0
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public final class Base64Util {
|
||||
|
||||
|
||||
@@ -25,15 +25,14 @@ import java.util.function.Supplier;
|
||||
/**
|
||||
* Utility class to simplify if...else logic using lambda expressions.
|
||||
* <p>
|
||||
* 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 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.
|
||||
* <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 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.
|
||||
* <p>
|
||||
* <b>Example:</b>
|
||||
* <pre>
|
||||
@@ -54,7 +53,8 @@ import java.util.function.Supplier;
|
||||
* }, () -> {
|
||||
* // do something
|
||||
* });
|
||||
* // If you only need an if branch, you can remove the second Supplier instance.
|
||||
* // If you only need an if branch, you can remove the second Supplier
|
||||
* // instance.
|
||||
*
|
||||
* // To check if all boolean expressions are true, use the 'and' method:
|
||||
* BranchUtil.and(1 == 1, 2 == 1)
|
||||
@@ -66,17 +66,17 @@ import java.util.function.Supplier;
|
||||
* </pre>
|
||||
*
|
||||
* <b>Note:</b>
|
||||
* The {@link #and(Boolean...)} and {@link #or(Boolean...)} methods accept any number of boolean expressions.
|
||||
* The {@link #and(Boolean...)} and {@link #or(Boolean...)} methods accept any
|
||||
* number of boolean expressions.
|
||||
* <p>
|
||||
* Created on 9 Jul 2023.
|
||||
*
|
||||
* @param <T> the type of the result to be handled by the methods
|
||||
* @author Zihlu Wang
|
||||
* @version 1.0.0
|
||||
* @see java.util.function.Supplier
|
||||
* @see java.util.function.BooleanSupplier
|
||||
* @see java.lang.Runnable
|
||||
* @since 9 Jul 2023
|
||||
* @version 1.0.0
|
||||
* @author Zihlu Wang
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public final class BranchUtil<T> {
|
||||
|
||||
@@ -95,11 +95,13 @@ public final class BranchUtil<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@code BranchUtil} instance to evaluate a logical OR operation on the provided boolean expressions.
|
||||
* Creates a {@code BranchUtil} instance to evaluate a logical OR operation
|
||||
* on the provided boolean expressions.
|
||||
*
|
||||
* @param booleans the boolean expressions to be evaluated
|
||||
* @param <T> the type of the result to be handled by the methods
|
||||
* @return a {@code BranchUtil} instance representing the result of the logical OR operation
|
||||
* @return a {@code BranchUtil} instance representing the result of the
|
||||
* logical OR operation
|
||||
*/
|
||||
public static <T> BranchUtil<T> or(Boolean... booleans) {
|
||||
var result = Arrays.stream(booleans)
|
||||
@@ -109,11 +111,13 @@ public final class BranchUtil<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@code BranchUtil} instance to evaluate a logical AND operation on the provided boolean expressions.
|
||||
* Creates a {@code BranchUtil} instance to evaluate a logical AND
|
||||
* operation on the provided boolean expressions.
|
||||
*
|
||||
* @param booleans the boolean expressions to be evaluated
|
||||
* @param <T> the type of the result to be handled by the methods
|
||||
* @return a {@code BranchUtil} instance representing the result of the logical AND operation
|
||||
* @return a {@code BranchUtil} instance representing the result of the
|
||||
* logical AND operation
|
||||
*/
|
||||
public static <T> BranchUtil<T> and(Boolean... booleans) {
|
||||
var result = Arrays.stream(booleans)
|
||||
@@ -123,11 +127,14 @@ public final class BranchUtil<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@code BranchUtil} instance to evaluate a logical OR operation on the provided boolean suppliers.
|
||||
* Creates a {@code BranchUtil} instance to evaluate a logical OR operation
|
||||
* on the provided boolean suppliers.
|
||||
*
|
||||
* @param booleanSuppliers the boolean suppliers to be evaluated
|
||||
* @param <T> the type of the result to be handled by the methods
|
||||
* @return a {@code BranchUtil} instance representing the result of the logical OR operation
|
||||
* @param <T> the type of the result to be handled by the
|
||||
* methods
|
||||
* @return a {@code BranchUtil} instance representing the result of the
|
||||
* logical OR operation
|
||||
*/
|
||||
public static <T> BranchUtil<T> or(BooleanSupplier... booleanSuppliers) {
|
||||
var result = Arrays.stream(booleanSuppliers)
|
||||
@@ -137,11 +144,14 @@ public final class BranchUtil<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@code BranchUtil} instance to evaluate a logical AND operation on the provided boolean suppliers.
|
||||
* Creates a {@code BranchUtil} instance to evaluate a logical AND
|
||||
* operation on the provided boolean suppliers.
|
||||
*
|
||||
* @param booleanSuppliers the boolean suppliers to be evaluated
|
||||
* @param <T> the type of the result to be handled by the methods
|
||||
* @return a {@code BranchUtil} instance representing the result of the logical AND operation
|
||||
* @param <T> the type of the result to be handled by the
|
||||
* methods
|
||||
* @return a {@code BranchUtil} instance representing the result of the
|
||||
* logical AND operation
|
||||
*/
|
||||
public static <T> BranchUtil<T> and(BooleanSupplier... booleanSuppliers) {
|
||||
var result = Arrays.stream(booleanSuppliers)
|
||||
@@ -151,13 +161,21 @@ public final class BranchUtil<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the result of the boolean expressions by executing the appropriate handler based on the result.
|
||||
* If the result is {@code true}, the {@code ifHandler} is executed. If the result is {@code false} and an
|
||||
* {@code elseHandler} is provided, it is executed. Returns the result of the executed handler.
|
||||
* Handles the result of the boolean expressions by executing the
|
||||
* appropriate handler based on the result.
|
||||
* <p>
|
||||
* If the result is {@code true}, the {@code ifHandler} is executed. If the
|
||||
* result is {@code false} and an {@code elseHandler} is provided, it is
|
||||
* executed.
|
||||
* <p>
|
||||
* Returns the result of the executed handler.
|
||||
*
|
||||
* @param ifHandler the handler to be executed if the result is {@code true}
|
||||
* @param elseHandler the handler to be executed if the result is {@code false} (optional)
|
||||
* @return the result of the executed handler, or {@code null} if no elseHandler is provided
|
||||
* @param ifHandler the handler to be executed if the result is
|
||||
* {@code true}
|
||||
* @param elseHandler the handler to be executed if the result is
|
||||
* {@code false} (optional)
|
||||
* @return the result of the executed handler, or {@code null} if no
|
||||
* {@code elseHandler} is provided
|
||||
*/
|
||||
public T handle(Supplier<T> ifHandler, Supplier<T> elseHandler) {
|
||||
if (this.result && Objects.nonNull(ifHandler)) {
|
||||
@@ -172,10 +190,13 @@ public final class BranchUtil<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the result of the boolean expressions by executing the provided handler if the result is {@code true}.
|
||||
* Handles the result of the boolean expressions by executing the provided
|
||||
* handler if the result is {@code true}.
|
||||
* <p>
|
||||
* Returns the result of the executed handler.
|
||||
*
|
||||
* @param ifHandler the handler to be executed if the result is {@code true}
|
||||
* @param ifHandler the handler to be executed if the result is
|
||||
* {@code true}
|
||||
* @return the result of the executed handler
|
||||
*/
|
||||
public T handle(Supplier<T> ifHandler) {
|
||||
@@ -183,12 +204,17 @@ public final class BranchUtil<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the result of the boolean expressions by executing the appropriate handler based on the result.
|
||||
* If the result is {@code true}, the {@code ifHandler} is executed. If the result is {@code false} and an
|
||||
* {@code elseHandler} is provided, it is executed.
|
||||
* Handles the result of the boolean expressions by executing the
|
||||
* appropriate handler based on the result.
|
||||
* <p>
|
||||
* If the result is {@code true}, the {@code ifHandler} is executed. If the
|
||||
* result is {@code false} and an {@code elseHandler} is provided, it is
|
||||
* executed.
|
||||
*
|
||||
* @param ifHandler the handler to be executed if the result is {@code true}
|
||||
* @param elseHandler the handler to be executed if the result is {@code false} (optional)
|
||||
* @param ifHandler the handler to be executed if the result is
|
||||
* {@code true}
|
||||
* @param elseHandler the handler to be executed if the result is
|
||||
* {@code false} (optional)
|
||||
*/
|
||||
public void handle(Runnable ifHandler, Runnable elseHandler) {
|
||||
if (this.result && Objects.nonNull(ifHandler)) {
|
||||
@@ -204,9 +230,11 @@ public final class BranchUtil<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the result of the boolean expressions by executing the provided handler if the result is {@code true}.
|
||||
* Handles the result of the boolean expressions by executing the provided
|
||||
* handler if the result is {@code true}.
|
||||
*
|
||||
* @param ifHandler the handler to be executed if the result is {@code true}
|
||||
* @param ifHandler the handler to be executed if the result is
|
||||
* {@code true}
|
||||
*/
|
||||
public void handle(Runnable ifHandler) {
|
||||
handle(ifHandler, null);
|
||||
|
||||
@@ -78,12 +78,14 @@ import java.util.function.Function;
|
||||
* .divide(7)
|
||||
* .getValue(2);
|
||||
* </pre>
|
||||
* The above expressions perform various mathematical calculations using the ChainedCalcUtil class.
|
||||
* The above expressions perform various mathematical calculations using the
|
||||
* 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 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.
|
||||
*
|
||||
* @author sunzsh
|
||||
* @version 1.0.0
|
||||
@@ -124,7 +126,8 @@ public final class ChainedCalcUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the specified value to the current value with a specified scale before the operation.
|
||||
* Adds the specified value to the current value with a specified scale
|
||||
* before the operation.
|
||||
*
|
||||
* @param other the value to be added
|
||||
* @param beforeOperateScale the scale to be applied before the operation
|
||||
@@ -145,7 +148,8 @@ public final class ChainedCalcUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts the specified value from the current value with a specified scale before the operation.
|
||||
* Subtracts the specified value from the current value with a specified
|
||||
* scale before the operation.
|
||||
*
|
||||
* @param other the value to be subtracted
|
||||
* @param beforeOperateScale the scale to be applied before the operation
|
||||
@@ -166,7 +170,8 @@ public final class ChainedCalcUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies the current value by the specified value with a specified scale before the operation.
|
||||
* Multiplies the current value by the specified value with a specified
|
||||
* scale before the operation.
|
||||
*
|
||||
* @param other the value to be multiplied by
|
||||
* @param beforeOperateScale the scale to be applied before the operation
|
||||
@@ -187,7 +192,8 @@ public final class ChainedCalcUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides the current value by the specified value with a specified scale before the operation.
|
||||
* Divides the current value by the specified value with a specified scale
|
||||
* before the operation.
|
||||
*
|
||||
* @param other the value to divide by
|
||||
* @param beforeOperateScale the scale to be applied before the operation
|
||||
@@ -210,7 +216,8 @@ public final class ChainedCalcUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides the current value by the specified value with a specified scale and a scale applied before the operation.
|
||||
* Divides the current value by the specified value with a specified scale
|
||||
* and a scale applied before the operation.
|
||||
*
|
||||
* @param other the value to divide by
|
||||
* @param scale the scale for the result
|
||||
@@ -278,7 +285,8 @@ public final class ChainedCalcUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the specified operator function to the current value and another value.
|
||||
* Applies the specified operator function to the current value and another
|
||||
* value.
|
||||
*
|
||||
* @param operator the operator function to apply
|
||||
* @param otherValue the value to apply the operator with
|
||||
@@ -289,11 +297,13 @@ public final class ChainedCalcUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the specified operator function to the current value and another value with a specified scale before the operation.
|
||||
* Applies the specified operator function to the current value and another
|
||||
* value with a specified scale before the operation.
|
||||
*
|
||||
* @param operator the operator function to apply
|
||||
* @param other the value to apply the operator with
|
||||
* @param beforeOperateScale the scale to be applied before the operation, or null if not applicable
|
||||
* @param beforeOperateScale the scale to be applied before the operation,
|
||||
* or null if not applicable
|
||||
* @return a ChainedCalcUtil instance with the updated value
|
||||
*/
|
||||
private ChainedCalcUtil operator(BiFunction<BigDecimal, BigDecimal, BigDecimal> operator, Object other, Integer beforeOperateScale) {
|
||||
@@ -301,11 +311,13 @@ public final class ChainedCalcUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the specified operator function to the current value and another value.
|
||||
* Applies the specified operator function to the current value and another
|
||||
* value.
|
||||
*
|
||||
* @param operatorFunction the operator function to apply
|
||||
* @param anotherValue the value to apply the operator with
|
||||
* @param beforeOperateScale the scale to be applied before the operation, or null if not applicable
|
||||
* @param beforeOperateScale the scale to be applied before the operation,
|
||||
* or null if not applicable
|
||||
* @return a ChainedCalcUtil instance with the updated value
|
||||
*/
|
||||
private synchronized ChainedCalcUtil baseOperator(Function<BigDecimal, BigDecimal> operatorFunction,
|
||||
@@ -325,7 +337,8 @@ public final class ChainedCalcUtil {
|
||||
* Converts the specified value to a BigDecimal.
|
||||
*
|
||||
* @param value the value to convert
|
||||
* @param scale the scale to apply to the resulting BigDecimal, or null if not applicable
|
||||
* @param scale the scale to apply to the resulting BigDecimal, or null if
|
||||
* not applicable
|
||||
* @return the converted BigDecimal value
|
||||
*/
|
||||
private BigDecimal convertBigDecimal(Object value, Integer scale) {
|
||||
|
||||
@@ -27,8 +27,9 @@ 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
|
||||
* 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.
|
||||
* <p>
|
||||
* Example usage:
|
||||
@@ -54,18 +55,19 @@ 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 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 functions, meaning the original data cannot be
|
||||
* retrieved from the hash value. These hash functions are commonly used for data integrity checks and password
|
||||
* storage, but they should not be used for encryption purposes.
|
||||
* The hash functions provided by the HashUtil class are one-way hash
|
||||
* functions, meaning the original data cannot be retrieved from the hash
|
||||
* value. These hash functions are commonly used for data integrity checks and
|
||||
* password storage, but they should not be used for encryption purposes.
|
||||
*
|
||||
* @see java.security.MessageDigest
|
||||
* @since 9 Jul 2023
|
||||
* @version 1.0.0
|
||||
* @author Zihlu Wang
|
||||
* @version 1.0.0
|
||||
* @see java.security.MessageDigest
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public final class HashUtil {
|
||||
|
||||
@@ -76,13 +78,17 @@ public final class HashUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the hash value of the specified string using the specified algorithm and charset.
|
||||
* 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)
|
||||
* @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 {
|
||||
@@ -108,10 +114,12 @@ public final class HashUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the MD2 hash value of the specified string using the given charset.
|
||||
* Calculates the MD2 hash value of the specified string using the given
|
||||
* charset.
|
||||
*
|
||||
* @param value the string to calculate the MD2 hash value for
|
||||
* @param charset the charset to use for encoding the string (default is UTF-8 if null)
|
||||
* @param charset the charset to use for encoding the string (default is
|
||||
* UTF-8 if null)
|
||||
* @return the MD2 hash value as a hexadecimal string
|
||||
*/
|
||||
public static String md2(String value, Charset charset) {
|
||||
@@ -120,7 +128,8 @@ public final class HashUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the MD2 hash value of the specified string using the UTF-8 charset.
|
||||
* Calculates the MD2 hash value of the specified string using the UTF-8
|
||||
* charset.
|
||||
*
|
||||
* @param value the string to calculate the MD2 hash value for
|
||||
* @return the MD2 hash value as a hexadecimal string
|
||||
@@ -130,10 +139,12 @@ public final class HashUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the MD5 hash value of the specified string using the given charset.
|
||||
* Calculates the MD5 hash value of the specified string using the given
|
||||
* charset.
|
||||
*
|
||||
* @param value the string to calculate the MD5 hash value for
|
||||
* @param charset the charset to use for encoding the string (default is UTF-8 if null)
|
||||
* @param charset the charset to use for encoding the string (default is
|
||||
* UTF-8 if null)
|
||||
* @return the MD5 hash value as a hexadecimal string
|
||||
*/
|
||||
public static String md5(String value, Charset charset) {
|
||||
@@ -142,7 +153,8 @@ public final class HashUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the MD5 hash value of the specified string using the UTF-8 charset.
|
||||
* Calculates the MD5 hash value of the specified string using the UTF-8
|
||||
* charset.
|
||||
*
|
||||
* @param value the string to calculate the MD5 hash value for
|
||||
* @return the MD5 hash value as a hexadecimal string
|
||||
@@ -152,10 +164,12 @@ public final class HashUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the SHA-1 hash value of the specified string using the given charset.
|
||||
* Calculates the SHA-1 hash value of the specified string using the given
|
||||
* charset.
|
||||
*
|
||||
* @param value the string to calculate the SHA-1 hash value for
|
||||
* @param charset the charset to use for encoding the string (default is UTF-8 if null)
|
||||
* @param charset the charset to use for encoding the string (default is
|
||||
* UTF-8 if null)
|
||||
* @return the SHA-1 hash value as a hexadecimal string
|
||||
*/
|
||||
public static String sha1(String value, Charset charset) {
|
||||
@@ -164,7 +178,8 @@ public final class HashUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the SHA-1 hash value of the specified string using the UTF-8 charset.
|
||||
* Calculates the SHA-1 hash value of the specified string using the UTF-8
|
||||
* charset.
|
||||
*
|
||||
* @param value the string to calculate the SHA-1 hash value for
|
||||
* @return the SHA-1 hash value as a hexadecimal string
|
||||
@@ -174,10 +189,12 @@ public final class HashUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the SHA-224 hash value of the specified string using the given charset.
|
||||
* Calculates the SHA-224 hash value of the specified string using the
|
||||
* given charset.
|
||||
*
|
||||
* @param value the string to calculate the SHA-224 hash value for
|
||||
* @param charset the charset to use for encoding the string (default is UTF-8 if null)
|
||||
* @param charset the charset to use for encoding the string (default is
|
||||
* UTF-8 if null)
|
||||
* @return the SHA-224 hash value as a hexadecimal string
|
||||
*/
|
||||
public static String sha224(String value, Charset charset) {
|
||||
@@ -186,7 +203,8 @@ public final class HashUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the SHA-224 hash value of the specified string using the UTF-8 charset.
|
||||
* Calculates the SHA-224 hash value of the specified string using the
|
||||
* UTF-8 charset.
|
||||
*
|
||||
* @param value the string to calculate the SHA-224 hash value for
|
||||
* @return the SHA-224 hash value as a hexadecimal string
|
||||
@@ -196,10 +214,12 @@ public final class HashUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the SHA-256 hash value of the specified string using the given charset.
|
||||
* Calculates the SHA-256 hash value of the specified string using the
|
||||
* given charset.
|
||||
*
|
||||
* @param value the string to calculate the SHA-256 hash value for
|
||||
* @param charset the charset to use for encoding the string (default is UTF-8 if null)
|
||||
* @param charset the charset to use for encoding the string (default is
|
||||
* UTF-8 if null)
|
||||
* @return the SHA-256 hash value as a hexadecimal string
|
||||
*/
|
||||
public static String sha256(String value, Charset charset) {
|
||||
@@ -208,7 +228,8 @@ public final class HashUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the SHA-256 hash value of the specified string using the UTF-8 charset.
|
||||
* Calculates the SHA-256 hash value of the specified string using the
|
||||
* UTF-8 charset.
|
||||
*
|
||||
* @param value the string to calculate the SHA-256 hash value for
|
||||
* @return the SHA-256 hash value as a hexadecimal string
|
||||
@@ -218,10 +239,12 @@ public final class HashUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the SHA-384 hash value of the specified string using the given charset.
|
||||
* Calculates the SHA-384 hash value of the specified string using the
|
||||
* given charset.
|
||||
*
|
||||
* @param value the string to calculate the SHA-384 hash value for
|
||||
* @param charset the charset to use for encoding the string (default is UTF-8 if null)
|
||||
* @param charset the charset to use for encoding the string (default is
|
||||
* UTF-8 if null)
|
||||
* @return the SHA-384 hash value as a hexadecimal string
|
||||
*/
|
||||
public static String sha384(String value, Charset charset) {
|
||||
@@ -230,7 +253,8 @@ public final class HashUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the SHA-384 hash value of the specified string using the UTF-8 charset.
|
||||
* Calculates the SHA-384 hash value of the specified string using the
|
||||
* UTF-8 charset.
|
||||
*
|
||||
* @param value the string to calculate the SHA-384 hash value for
|
||||
* @return the SHA-384 hash value as a hexadecimal string
|
||||
@@ -240,10 +264,12 @@ public final class HashUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the SHA-512 hash value of the specified string using the given charset.
|
||||
* Calculates the SHA-512 hash value of the specified string using the
|
||||
* given charset.
|
||||
*
|
||||
* @param value the string to calculate the SHA-384 hash value for
|
||||
* @param charset the charset to use for encoding the string (default is UTF-8 if null)
|
||||
* @param charset the charset to use for encoding the string (default is
|
||||
* UTF-8 if null)
|
||||
* @return the SHA-512 hash value as a hexadecimal string
|
||||
*/
|
||||
public static String sha512(String value, Charset charset) {
|
||||
@@ -252,7 +278,8 @@ public final class HashUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the SHA-512 hash value of the specified string using the UTF-8 charset.
|
||||
* Calculates the SHA-512 hash value of the specified string using the
|
||||
* UTF-8 charset.
|
||||
*
|
||||
* @param value the string to calculate the SHA-384 hash value for
|
||||
* @return the SHA-512 hash value as a hexadecimal string
|
||||
|
||||
@@ -24,8 +24,11 @@ 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.
|
||||
* It also provides methods for getting and setting field values using reflection.
|
||||
* 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.
|
||||
*
|
||||
* @author Zihlu Wang
|
||||
* @version 1.0.0
|
||||
@@ -41,11 +44,13 @@ public final class MapUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an object to a map by mapping the field names to their corresponding values.
|
||||
* Converts an object to a map by mapping the field names to their
|
||||
* corresponding values.
|
||||
*
|
||||
* @param obj the object to be converted to a map
|
||||
* @return a map representing the fields and their values of the object
|
||||
* @throws IllegalAccessException if an error occurs while accessing the fields of the object
|
||||
* @throws IllegalAccessException if an error occurs while accessing the
|
||||
* fields of the object
|
||||
*/
|
||||
public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
|
||||
if (obj == null) {
|
||||
@@ -67,16 +72,22 @@ public final class MapUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a map to an object of the specified type by setting the field values using the map entries.
|
||||
* Converts a map to an object of the specified type by setting the field
|
||||
* values using the map entries.
|
||||
*
|
||||
* @param map the map representing the fields and their values
|
||||
* @param requiredType the class of the object to be created
|
||||
* @param <T> the type of the object to be created
|
||||
* @return an object of the specified type with the field values set from the map
|
||||
* @throws NoSuchMethodException if the constructor of the required type is not found
|
||||
* @throws InvocationTargetException if an error occurs while invoking the constructor
|
||||
* @throws InstantiationException if the required type is abstract or an interface
|
||||
* @throws IllegalAccessException if an error occurs while accessing the fields of the object
|
||||
* @return an object of the specified type with the field values set from
|
||||
* the map
|
||||
* @throws NoSuchMethodException if the constructor of the required
|
||||
* type is not found
|
||||
* @throws InvocationTargetException if an error occurs while invoking the
|
||||
* constructor
|
||||
* @throws InstantiationException if the required type is abstract or an
|
||||
* interface
|
||||
* @throws IllegalAccessException if an error occurs while accessing the
|
||||
* fields of the object
|
||||
*/
|
||||
public static <T> T mapToObject(Map<String, Object> map, Class<T> requiredType) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
|
||||
var bean = requiredType.getConstructor().newInstance();
|
||||
@@ -130,9 +141,12 @@ public final class MapUtil {
|
||||
* @param fieldName the name of the field
|
||||
* @param fieldType the class representing the type of the field value
|
||||
* @param <T> the type of the field value
|
||||
* @return the value of the field in the object, or null if the field does not exist or cannot be accessed
|
||||
* @throws IllegalAccessException if an error occurs while accessing the field
|
||||
* @throws InvocationTargetException if an error occurs while invoking the field getter method
|
||||
* @return the value of the field in the object, or null if the field does
|
||||
* not exist or cannot be accessed
|
||||
* @throws IllegalAccessException if an error occurs while accessing the
|
||||
* field
|
||||
* @throws InvocationTargetException if an error occurs while invoking the
|
||||
* field getter method
|
||||
*/
|
||||
public static <T> T getFieldValue(Object obj, String fieldName, Class<T> fieldType) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||
var methodName = getMethodName("get", fieldName);
|
||||
@@ -149,8 +163,10 @@ public final class MapUtil {
|
||||
* @param obj the object in which to set the field value
|
||||
* @param fieldName the name of the field
|
||||
* @param fieldValue the value to be set
|
||||
* @throws InvocationTargetException if an error occurs while invoking the field setter method
|
||||
* @throws IllegalAccessException if an error occurs while accessing the field
|
||||
* @throws InvocationTargetException if an error occurs while invoking the
|
||||
* field setter method
|
||||
* @throws IllegalAccessException if an error occurs while accessing the
|
||||
* field
|
||||
*/
|
||||
public static void setFieldValue(Object obj, String fieldName, Object fieldValue) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
|
||||
var objectClass = obj.getClass();
|
||||
@@ -174,7 +190,8 @@ public final class MapUtil {
|
||||
/**
|
||||
* Returns the default string representation of the specified object.
|
||||
*
|
||||
* @param obj the object for which to return the default string representation
|
||||
* @param obj the object for which to return the default string
|
||||
* representation
|
||||
* @return the default string representation of the object
|
||||
*/
|
||||
private static String defaultObject(Object obj) {
|
||||
@@ -191,7 +208,8 @@ public final class MapUtil {
|
||||
* @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 required type
|
||||
* @return the casted value, or null if the value cannot be casted to the
|
||||
* required type
|
||||
*/
|
||||
public static <T> T cast(Object value, Class<T> requiredType) {
|
||||
if (requiredType.isInstance(value)) {
|
||||
|
||||
@@ -16,7 +16,13 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* Commonly used utils that helps to improve dev efficiency.
|
||||
* <h3>JDevKit Dev-Utils - Common Utility Classes</h3>
|
||||
* <p>
|
||||
* This package is part of JDevKit, an open-source Java Development Kit that
|
||||
* provides a set of convenient tools to streamline code development and
|
||||
* enhance productivity. This package serves as the root package for the module
|
||||
* dev-utils, which contains a collection of common utility classes commonly
|
||||
* used in all Java Application development.
|
||||
*
|
||||
* @author Zihlu Wang
|
||||
* @since 1.0.0
|
||||
|
||||
Reference in New Issue
Block a user