Package tk.airshipcraft.commonlib.utils
Class NullCoalescing
java.lang.Object
tk.airshipcraft.commonlib.utils.NullCoalescing
Provides utility methods for null-safe operations, inspired by JavaScript's nullish coalescing and optional chaining.
This class helps to reduce boilerplate code associated with null checks and default value assignments.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface for null-check operations. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TcastOrNull(Class<T> clazz, Object value) Attempts to cast an object to a specified type and returns null if the cast is not possible.static <T> Tchain(NullCoalescing.NullChecker<T> statement) Executes a function and returns its value if not null; otherwise, returns null.static <T> Tchain(NullCoalescing.NullChecker<T> statement, T fallback) Executes a function and returns its value if not null; otherwise, returns a fallback value.static <T> Tcoalesce(T... items) Returns the first non-null value from the provided items.static booleanCompares two objects for equality, handling nulls safely.static booleanequalsNotNull(Object former, Object latter) Compares two objects for equality, where nulls are considered unequal.static <T> voidExecutes a consumer if the provided value is not null.static <T> TIf the provided value is null, uses the supplier to generate a new value.
-
Constructor Details
-
NullCoalescing
public NullCoalescing()
-
-
Method Details
-
coalesce
Returns the first non-null value from the provided items.- Type Parameters:
T- The type of the values being checked.- Parameters:
items- The array of values to check for non-nullity.- Returns:
- The first non-null value, or null if all are null.
-
chain
Executes a function and returns its value if not null; otherwise, returns null. This is similar to JavaScript's optional chaining.- Type Parameters:
T- The type of the value expected from the function.- Parameters:
statement- The function to execute.- Returns:
- The value from the function if not null, or null if the function or its result is null.
-
chain
Executes a function and returns its value if not null; otherwise, returns a fallback value.- Type Parameters:
T- The type of the value expected from the function.- Parameters:
statement- The function to execute.fallback- The fallback value to return if the function or its result is null.- Returns:
- The value from the function if not null, or the fallback value.
-
exists
Executes a consumer if the provided value is not null.- Type Parameters:
T- The type of the value.- Parameters:
value- The value to check.handler- The consumer to execute if the value is not null.
-
notExists
If the provided value is null, uses the supplier to generate a new value.- Type Parameters:
T- The type of the value.- Parameters:
value- The value to check.handler- The supplier to use if the value is null.- Returns:
- The original value if not null; otherwise, the value from the supplier.
-
castOrNull
Attempts to cast an object to a specified type and returns null if the cast is not possible.- Type Parameters:
T- The target type for the cast.- Parameters:
clazz- The class object of the target type.value- The object to cast.- Returns:
- The cast object if successful, or null if the cast is not possible.
-
equals
Compares two objects for equality, handling nulls safely.- Parameters:
former- The first object to compare.latter- The second object to compare.- Returns:
- True if both objects are equal; false otherwise.
-
equalsNotNull
Compares two objects for equality, where nulls are considered unequal.- Parameters:
former- The first object to compare.latter- The second object to compare.- Returns:
- True if both objects are non-null and equal; false otherwise.
-