Class Options

java.lang.Object
dmx.fun.Options

@NullMarked public final class Options extends Object
Collector facade for Stream<Option<T>>.

Centralises all Option and NonEmptyList collector factories that operate on option streams as static methods, mirroring the role of Collectors. The underlying factories on Option and NonEmptyList are kept for backward compatibility; this class simply delegates to them.

Usage:

import static dmx.fun.Options.*;

List<String>              present = stream.collect(Options.presentToList());
Option<List<String>>      seq     = stream.collect(Options.sequence());
Option<NonEmptyList<String>> nel  = stream.collect(Options.toNonEmptyList());
See Also:
  • Method Details

    • presentToList

      public static <V> Collector<Option<? extends V>, ?, List<V>> presentToList()
      Returns a Collector that collects all present values from a Stream<Option<V>> into an unordered List, discarding empty options.

      Delegates to Option.presentValuesToList().

      Type Parameters:
      V - the value type
      Returns:
      a collector producing a List<V> of present values
    • sequence

      public static <V> Collector<Option<V>, ?, Option<List<V>>> sequence()
      Returns a Collector that reduces a Stream<Option<V>> to a single Option<List<V>>: Option.some(list) if every element is present, or Option.none() if any element is empty.

      Delegates to Option.sequenceCollector().

      Type Parameters:
      V - the value type
      Returns:
      a collector producing Option<List<V>>
    • toNonEmptyList

      public static <T> Collector<T, ?, Option<NonEmptyList<T>>> toNonEmptyList()
      Returns a Collector that accumulates a Stream<T> into an Option<NonEmptyList<T>>: Option.some(nel) if the stream is non-empty, or Option.none() if the stream is empty.

      Delegates to NonEmptyList.toNonEmptyList().

      Type Parameters:
      T - the element type
      Returns:
      a collector producing Option<NonEmptyList<T>>