Package me.sbasalaev

Class Opt<T>

All Implemented Interfaces:
Cloneable, Iterable<T>, Traversable<T>

public final class Opt<@Out T> extends Collection<T>
Optional value. Opt may contain a single value or no value. The value may be fetched by a family of orElse methods providing the default in case the value is absent, handled in a pattern maching style as
   return optional.match(
     (value) -> ...,
     ()      -> ...
   );
or traversed in a for statement
   for (var value : optional) {
     ...
   }
This class predates Optional introduced by Java 8 that serves the same purpose but is not traversable. Values can be converted between Opt and Optional with fromJava() and toJava().
  • Method Details

    • empty

      public static <T> Opt<T> empty()
      Empty optional.
    • of

      public static <T> Opt<T> of(T value)
      Non-empty optional.
    • ofNullable

      public static <T> Opt<T> ofNullable(@Nullable T value)
      Optional of non-null value, empty optional for null.
    • fromJava

      public static <T> Opt<T> fromJava(Optional<T> optional)
      Converts Java optional to Opt.
    • orElseNull

      public @Nullable T orElseNull()
      Returns value in this optional or null if the optional is empty.
    • orElse

      public T orElse(@Out T defaultValue)
      Returns value in this optional or defaultValue if the optional is empty.
    • orElseGet

      public T orElseGet(Supplier<? extends @Out T> valueSupplier)
      Returns value in this optional or the value from valueSupplier if the optional is empty.
    • orElseThrow

      public T orElseThrow(String message) throws NoSuchElementException
      Returns value in this optional or throws NoSuchElementException with given message.
      Throws:
      NoSuchElementException
    • orElseThrow

      public <X extends Throwable> T orElseThrow(Supplier<X> exceptionSupplier) throws X
      Returns value in this optional or throws the exception from given supplier.
      Throws:
      X
      Since:
      3.2
    • match

      public <R> R match(Function<@Out T,R> onNonEmpty, Supplier<R> onEmpty)
      Either maps the value or produces one if the optional is empty. Can be used in a pattern matching style:
         return optional.match(
           (value) -> ...,
           ()      -> ...
         );
       
    • matchDo

      public void matchDo(Consumer<@Out T> action, Runnable emptyAction)
      Either runs action on the value or emptyAction if the optional is empty. Can be used in a pattern matching style:
         optional.matchDo(
           (value) -> { ... },
           ()      -> { ... }
         );
       
    • toJava

      public Optional<T> toJava()
      Converts this value to Java optional.
    • mapped

      public <R> Opt<R> mapped(Function<? super @Out T,? extends R> mapping)
      Maps value in this optional using given mapping. This is eager operation that applies the mapping immediately.
      Specified by:
      mapped in class Collection<T>
    • filtered

      public Opt<T> filtered(Predicate<? super @Out T> condition)
      Returns this optional if its value matches given condition, empty optional otherwise. This is eager operation that tests the condition immediately.
      Specified by:
      filtered in class Collection<T>
    • narrow

      public <U> Opt<U> narrow(Class<U> clazz)
      Returns this optional if its value is of given class, empty optional otherwise.
    • spliterator

      public Spliterator<T> spliterator()
      Description copied from class: Collection
      Creates a Spliterator over elements of this collection. The spliterator reports Spliterator.NONNULL and Spliterator.SIZED.
      Specified by:
      spliterator in interface Iterable<T>
      Specified by:
      spliterator in class Collection<T>
    • stream

      public Stream<T> stream()
      Description copied from class: Collection
      Returns a sequential Stream with this collection as its source.
      Overrides:
      stream in class Collection<T>
    • clone

      public Opt<T> clone()
      Returns this optional.
      Specified by:
      clone in class Collection<T>
    • fold

      public <R> R fold(R first, BiFunction<? super R,? super @Out T,? extends R> combine)
      Description copied from interface: Traversable
      Starting from the first, applies combine to elements of the collection returning result. The order of elements is determined by the iterator.
    • size

      public int size()
      Description copied from class: Collection
      Number of elements in this collection. Unlike Traversable.count() this method is guaranteed to be fast.
      Specified by:
      size in class Collection<T>
    • iterator

      public Iterator<T> iterator()
    • equals

      public boolean equals(@Nullable Object obj)
      Whether given object is equal to this optional. The object is equal to this optional if it is an instance of Opt and either both are empty or contain equal values.
      Specified by:
      equals in class Collection<T>
    • hashCode

      public int hashCode()
      Hashcode of the value or 0 for empty optional.
      Specified by:
      hashCode in class Collection<T>