Interface Either<U,V>

Type Parameters:
U - primary value type
V - fallback value type

@Immutable @API(status=STABLE) public interface Either<U,V>
An object that contains either a value of type Either or type Either.
  • Method Details

    • ofPrimary

      static <U, V> @NonNull Either<U,V> ofPrimary(@NonNull U value)
      Creates an instance with a value of the primary type Either.
      Type Parameters:
      U - primary value type
      V - secondary value type
      Parameters:
      value - primary value
      Returns:
      the instance
    • ofFallback

      static <U, V> @NonNull Either<U,V> ofFallback(@NonNull V value)
      Creates an instance with a value of the fallback type Either.
      Type Parameters:
      U - primary value type
      V - secondary value type
      Parameters:
      value - primary value
      Returns:
      the instance
    • primary

      @NonNull Optional<U> primary()
      Returns an optional containing the value of type Either, if it exists.
      Returns:
      the first value
    • fallback

      @NonNull Optional<V> fallback()
      Returns an optional containing the value of type Either, if it exists.
      Returns:
      the second value
    • primaryOrMapFallback

      default @NonNull U primaryOrMapFallback(@NonNull Function<V,U> mapFallback)
      Extract primary(), or if it's not present, map fallback() to U.
      Parameters:
      mapFallback - function mapping the fallback type to U
      Returns:
      extracted or mapped value
    • fallbackOrMapPrimary

      default @NonNull V fallbackOrMapPrimary(@NonNull Function<U,V> mapPrimary)
      Extract fallback(), or if it's not present, map primary() to V.
      Parameters:
      mapPrimary - function mapping the primary type to V
      Returns:
      extracted or mapped value
    • mapEither

      default <R> @NonNull R mapEither(@NonNull Function<U,R> mapPrimary, @NonNull Function<V,R> mapFallback)
      Extract primary() or fallback(), in that order, applying the appropriate function to map the result to R.
      Type Parameters:
      R - mapped type
      Parameters:
      mapPrimary - function mapping the primary type to R
      mapFallback - function mapping the fallback type to R
      Returns:
      mapped value