Class ObjectUtils

java.lang.Object
org.litebridge.commons.ObjectUtils

public final class ObjectUtils extends Object
Utility class for working with objects.
  • Method Details

    • requireNonNull

      public static <T, X extends Throwable> T requireNonNull(@Nullable T obj, Supplier<? extends X> exceptionSupplier) throws X
      Ensures that the provided object is not null.

      If the object is null, an exception provided by the exceptionSupplier is thrown.

      Type Parameters:
      T - the type of the object to be validated
      X - the type of the exception to be thrown if validation fails
      Parameters:
      obj - the object to be checked for nullability
      exceptionSupplier - the supplier that provides an exception to be thrown if obj is null
      Returns:
      the non-null object passed as the obj parameter
      Throws:
      X - if obj is null
    • requireNull

      public static <X extends Throwable> void requireNull(@Nullable Object obj, Supplier<? extends X> exceptionSupplier) throws X
      Ensures that the provided object is null.

      If the object is not null, an exception provided by the exceptionSupplier is thrown.

      Type Parameters:
      X - the type of the exception to be thrown
      Parameters:
      obj - the object to be checked for nullity
      exceptionSupplier - the supplier that provides an exception to be thrown if obj is not null
      Throws:
      X - if obj is not null
    • requireInstanceOf

      public static <X extends Throwable, T> T requireInstanceOf(Class<T> type, @Nullable Object obj) throws X
      Ensures that the provided object is an instance of the specified type.

      If the object is not an instance of the type, an IllegalArgumentException is thrown.

      Type Parameters:
      X - the type of the exception to be thrown if validation fails
      T - the expected type of the object
      Parameters:
      type - the expected class type
      obj - the object to check
      Returns:
      the object cast to the expected type
      Throws:
      X - if obj is not an instance of type
    • requireInstanceOf

      public static <X extends Throwable, T> T requireInstanceOf(Class<T> type, @Nullable Object obj, Supplier<? extends X> exceptionSupplier) throws X
      Ensures that the provided object is an instance of the specified type.

      If the object is not an instance of the type, an exception provided by the exceptionSupplier is thrown.

      Type Parameters:
      X - the type of the exception to be thrown if validation fails
      T - the expected type of the object
      Parameters:
      type - the expected class type
      obj - the object to check
      exceptionSupplier - the supplier that provides an exception to be thrown if obj is not an instance of type
      Returns:
      the object cast to the expected type
      Throws:
      X - if obj is not an instance of type
    • getFieldValue

      public static <T> T getFieldValue(Object dto, String fieldName, Class<T> fieldType)
      Retrieves the value of a specified field from the given object.

      The field is accessed reflectively, and its value is returned as an object of the specified type.

      Type Parameters:
      T - the expected type of the field's value
      Parameters:
      dto - the object from which the field value is to be retrieved
      fieldName - the name of the field to retrieve
      fieldType - the expected class type of the field's value
      Returns:
      the value of the specified field, cast to the specified type
      Throws:
      IllegalStateException - if the field is inaccessible or retrieval fails