Class ObjectUtils

java.lang.Object
dev.metaschema.core.util.ObjectUtils

public final class ObjectUtils extends Object
A collection of utilities for checking and managing Java objects.
  • Method Details

    • notNull

      @NonNull public static <T> T notNull(@Nullable T obj)
      Assert that the provided object is not null.

      This method sets the expectation that the provided object is not null in cases where a non-null value is required.

      Type Parameters:
      T - the object type
      Parameters:
      obj - the object
      Returns:
      the object
    • requireNonNull

      @NonNull public static <T> T requireNonNull(@Nullable T obj)
      Require a non-null value.
      Type Parameters:
      T - the type of the reference
      Parameters:
      obj - the object reference to check for nullity
      Returns:
      obj if not null
      Throws:
      NullPointerException - if obj is null
    • requireNonNull

      @NonNull public static <T> T requireNonNull(@Nullable T obj, @NonNull String message)
      Require a non-null value.
      Type Parameters:
      T - the type of the reference
      Parameters:
      obj - the object reference to check for nullity
      message - detail message to be used in the event that a NullPointerException is thrown
      Returns:
      obj if not null
      Throws:
      NullPointerException - if obj is null
    • filterNull

      @NonNull public static <T> Stream<T> filterNull(T item)
      A filter used to remove null items from a stream.
      Type Parameters:
      T - the item type
      Parameters:
      item - the item to filter
      Returns:
      the item as a steam or an empty stream if the item is null
    • asType

      @NonNull public static <T> T asType(@NonNull Object obj)
      Cast the provided object as the requested return type.
      Type Parameters:
      T - the Java type to cast the object to
      Parameters:
      obj - the object to cast
      Returns:
      the object cast to the requested type
      Throws:
      ClassCastException - if the object cannot be cast to the requested type
    • asNullableType

      @Nullable public static <T> T asNullableType(@Nullable Object obj)
      Cast the provided object as the requested return type.

      If the object is null, the returned value will be null.

      Type Parameters:
      T - the Java type to cast the object to
      Parameters:
      obj - the object to cast, which may be null
      Returns:
      the object cast to the requested type, or null if the provided object is null
      Throws:
      ClassCastException - if the object cannot be cast to the requested type