Class NullCoalescing

java.lang.Object
tk.airshipcraft.commonlib.utils.NullCoalescing

public final class NullCoalescing extends Object
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 Classes
    Modifier and Type
    Class
    Description
    static interface 
    Functional interface for null-check operations.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    castOrNull(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> T
    Executes a function and returns its value if not null; otherwise, returns null.
    static <T> T
    chain(NullCoalescing.NullChecker<T> statement, T fallback)
    Executes a function and returns its value if not null; otherwise, returns a fallback value.
    static <T> T
    coalesce(T... items)
    Returns the first non-null value from the provided items.
    static boolean
    equals(Object former, Object latter)
    Compares two objects for equality, handling nulls safely.
    static boolean
    equalsNotNull(Object former, Object latter)
    Compares two objects for equality, where nulls are considered unequal.
    static <T> void
    exists(T value, Consumer<T> handler)
    Executes a consumer if the provided value is not null.
    static <T> T
    notExists(T value, Supplier<T> handler)
    If the provided value is null, uses the supplier to generate a new value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • NullCoalescing

      public NullCoalescing()
  • Method Details

    • coalesce

      @SafeVarargs public static <T> T coalesce(T... items)
      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

      public static <T> T chain(NullCoalescing.NullChecker<T> statement)
      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

      public static <T> T chain(NullCoalescing.NullChecker<T> statement, T fallback)
      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

      public static <T> void exists(T value, Consumer<T> handler)
      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

      public static <T> T notExists(T value, Supplier<T> handler)
      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

      public static <T> T castOrNull(Class<T> clazz, Object value)
      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

      public static boolean equals(Object former, Object latter)
      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

      public static boolean equalsNotNull(Object former, Object latter)
      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.