Class Optionals

java.lang.Object
com.github.mizool.core.Optionals

public final class Optionals extends Object
  • Method Details

    • unwrapUserRequestedObject

      public static <T> T unwrapUserRequestedObject(@NonNull @NonNull Optional<T> wrapped, @NonNull @NonNull Class<T> classOfT)
      Used when a user directly requests an object, resulting in an ObjectNotFoundException if it does not exist.
    • unwrapUserRequestedObject

      public static <T> Function<Optional<T>,T> unwrapUserRequestedObject(@NonNull @NonNull Class<T> classOfT)
      Used when a user directly requests an object, resulting in an ObjectNotFoundException if it does not exist.
    • unwrapRequiredObject

      public static <T> T unwrapRequiredObject(@NonNull @NonNull Optional<T> wrapped, @NonNull @NonNull Class<T> classOfT)
      Used when an object can be reasonably expected to exist, resulting in a DataInconsistencyException if it does not exist.
    • unwrapRequiredObject

      public static <T> Function<Optional<T>,T> unwrapRequiredObject(@NonNull @NonNull Class<T> classOfT)
      Used when an object can be reasonably expected to exist, resulting in a DataInconsistencyException if it does not exist.
    • unwrapUserMentionedObject

      public static <T> T unwrapUserMentionedObject(@NonNull @NonNull Optional<T> wrapped, @NonNull @NonNull Class<T> classOfT)
      Used when a user-submitted entity refers to another object, resulting in a UnprocessableEntityException if that object does not exist.
    • unwrapUserMentionedObject

      public static <T> Function<Optional<T>,T> unwrapUserMentionedObject(@NonNull @NonNull Class<T> classOfT)
      Used when a user-submitted entity refers to another object, resulting in a UnprocessableEntityException if that object does not exist.
    • streamPresentValue

      public static <T> Stream<T> streamPresentValue(@NonNull @NonNull Optional<T> optional)
      Used in streams to flat-map each Optional to its value if present.

      This method is intended to be used as follows:
      
           .flatMap(Optionals::streamPresentValue)
       
      Using this method is equivalent of chaining Optional.isPresent() and Optional.get() like this:
      
           .filter(Optional::isPresent)
           .map(Optional::get)