Class CheckedValue<T,E extends Exception>

java.lang.Object
org.opendaylight.yangtools.concepts.Either<T,E>
org.opendaylight.yangtools.concepts.CheckedValue<T,E>
Type Parameters:
T - Value type
E - Exception type
All Implemented Interfaces:
Immutable

@Beta @NonNullByDefault public class CheckedValue<T,E extends Exception> extends Either<T,E>
Utility holder similar to Optional, except the empty case contains an Exception, which should be reported, for example via throwing it. It provides analogous methods such as isPresent(), ifPresent(Consumer), get(), orElse(Object), orElseGet(Supplier), orElseThrow(Function).
Author:
Robert Varga
  • Constructor Details

    • CheckedValue

      protected CheckedValue(T value)
    • CheckedValue

      protected CheckedValue(E violation, @Nullable Void dummy)
  • Method Details

    • ofException

      public static <T, E extends Exception> CheckedValue<T,E> ofException(E cause)
      Create a new instance containing an Exception.
      Type Parameters:
      T - Value type
      E - Exception type
      Parameters:
      cause - Throwable
      Returns:
      A new instance
      Throws:
      NullPointerException - if cause is null
    • ofValue

      public static <T, E extends Exception> CheckedValue<T,E> ofValue(T value)
      Create a new instance containing specified value.
      Type Parameters:
      T - Value type
      E - Exception type
      Parameters:
      value - Value
      Returns:
      A new instance
      Throws:
      NullPointerException - if value is null
    • ofVariant

      public static <T, U, E extends Exception> CheckedValue<T,E> ofVariant(Either<T,U> variant, Function<U,E> mapper)
      Convert a Variant into a CheckedValue, converting the second value into an exception.
      Type Parameters:
      T - First alternative type
      U - Second alternative type
      E - Exception type
      Parameters:
      variant - Input variant
      mapper - Mapping function from second alternative to an exception
      Returns:
      Resulting CheckedValue
    • get

      public final T get()
      Return the contained value if isPresent() would return true, throws IllegalStateException otherwise.
      Returns:
      Contained value
      Throws:
      IllegalStateException - if an error string is present.
    • getException

      public final E getException()
      Return the contained error string if isPresent() would return false, throws IllegalStateException otherwise.
      Returns:
      Throwable which was used to instantiate this object, or absent if it was instantiated using an error string.
      Throws:
      IllegalStateException - if a value is present.
    • isPresent

      public final boolean isPresent()
      Return true if a value is present.
      Returns:
      True if a value is present.
    • ifPresent

      public final void ifPresent(Consumer<? super T> consumer)
      If a value is present, invoke the specified consumer with the value, otherwise do nothing.
      Parameters:
      consumer - block to be executed if a value is present
      Throws:
      NullPointerException - if value is present and consumer is null
    • map

      public <U> CheckedValue<U,E> map(Function<? super T,U> mapper)
    • mapException

      public <X extends Exception> CheckedValue<T,X> mapException(Function<? super E,X> mapper)
    • flatMap

      public <U> CheckedValue<U,E> flatMap(Function<? super T,CheckedValue<U,E>> mapper)
    • orElse

      public final T orElse(T other)
      Return contained value if present, otherwise return supplied value.
      Parameters:
      other - Replacement value
      Returns:
      Contained value or {code other}
    • orElseGet

      public final T orElseGet(Supplier<T> supplier)
      Return contained value if present, otherwise return the value produced by a supplier.
      Parameters:
      supplier - Replacement value supplier
      Returns:
      Contained value or supplier's value
      Throws:
      NullPointerException - if supplier is null
    • orElseThrow

      public final T orElseThrow() throws E
      Return contained value if present or throw the exception alternative.
      Returns:
      Contained value
      Throws:
      E - When there is no contained value
    • orElseThrow

      public final <X extends Throwable> T orElseThrow(Function<E,X> exceptionMapper) throws X
      Return contained value if present or throw the exception alternative mapped through provided mapper.
      Type Parameters:
      X - Thrown exception type
      Parameters:
      exceptionMapper - Exception mapper
      Returns:
      Contained value
      Throws:
      NullPointerException - if exceptionMapper is null
      X - When there is no contained value
    • completeFuture

      public final boolean completeFuture(CompletableFuture<T> future)
      Complete target CompletableFuture either successfully or exceptionally based on the state of this object.
      Parameters:
      future - Future to complete
      Returns:
      True if this call has transitioned the future to a completed state, false otherwise.
      Throws:
      NullPointerException - if {code future} is null
    • completeFuture

      public final boolean completeFuture(SettableFuture<T> future)
      Complete target SettableFuture either successfully or exceptionally based on the state of this object.
      Parameters:
      future - Future to complete
      Returns:
      True if this call has transitioned the future to a completed state, false otherwise.
      Throws:
      NullPointerException - if {code future} is null
    • toCompletableFuture

      public final CompletableFuture<T> toCompletableFuture()
      Transform this object into an immediately-completed CompletableFuture. The future will be successful if this object has a contained value or unsuccessful if this objects contains an exception.
      Returns:
      A CompletableFuture.
    • toFluentFuture

      public final FluentFuture<T> toFluentFuture()
      Transform this object into an immediately-completed FluentFuture. The future will be successful if this object has a contained value or unsuccessful if this objects contains an exception.
      Returns:
      A FluentFuture.