Class NotNull<T>

java.lang.Object
org.javimmutable.collections.NotNull<T>
All Implemented Interfaces:
Serializable, Iterable<T>, IStreamable<T>, SplitableIterable<T>

public abstract class NotNull<T> extends Object implements IStreamable<T>, Serializable
Used to handle cases when a value may or may not exist and to eliminate the use of null values. The value of a NotNull cannot be null. Provides a variety of utility methods to allow call chaining.
See Also:
  • Method Details

    • empty

      @Nonnull public static <T> NotNull<T> empty()
      Returns a NotNull with no value. All empty NotNulls share a common instance.
    • of

      @Nonnull public static <T> NotNull<T> of(@Nullable T valueOrNull)
      Returns a NotNull containing the value. Null is treated as empty.
    • cast

      public static <T> NotNull<T> cast(@Nonnull Class<T> klass, @Nullable Object valueOrNull)
      Determine if the object is an instance of the specified Class or a subclass. If the object is null, returns a NotNull containing null. If the object is not null but not of the correct class, returns an empty Holder. Otherwise returns a Holder containing the value cast to the target type.
      Type Parameters:
      T - type of the class
      Parameters:
      klass - class to cast the object to
      valueOrNull - object to be case
      Returns:
      a NotNull
    • first

      @Nonnull public static <T> NotNull<T> first(@Nonnull Iterable<? extends T> collection)
      Returns a NotNull containing the first value of the collection. If the collection is empty an empty NotNull is returned.
    • first

      @Nonnull public static <T> NotNull<T> first(@Nonnull Iterable<? extends T> collection, @Nonnull Func1<? super T,Boolean> predicate)
      Returns a NotNull containing the first value of the collection for which the predicate returns true. If the collection is empty or predicate always returns false an empty NotNull is returned.
    • maybe

      @Nonnull public abstract Maybe<T> maybe()
      If this empty returns Maybe.empty(), otherwise returns Maybe.of(T).
    • map

      @Nonnull public abstract NotNull<T> map(@Nonnull Func0<? extends T> absentMapping)
      Produce a full NotNull. If this NotNull is full it is returned. Otherwise the absentMapping function is called to provide a value for the result NotNull.
      Parameters:
      absentMapping - produces value if this is empty
      Returns:
      a full NotNull
    • map

      @Nonnull public abstract <U> NotNull<U> map(@Nonnull Func1<? super T,? extends U> presentMapping)
      Produce a NotNull that is empty if this is empty or else contains the result of passing this value to the given mapping function.
      Parameters:
      presentMapping - maps this value to new value
      Returns:
      a possibly empty NotNull
    • map

      @Nonnull public abstract <U> NotNull<U> map(@Nonnull Func0<? extends U> absentMapping, @Nonnull Func1<? super T,? extends U> presentMapping)
      Produce a full NotNull. If this is empty the absentMapping function is called to provide a value. Otherwise the presentMapping function is called to produce a new value based on this value.
      Parameters:
      absentMapping - produces value when this is empty
      presentMapping - maps this value to new value
      Returns:
      a full NotNull
    • mapThrows

      @Nonnull public abstract <E extends Exception> NotNull<T> mapThrows(@Nonnull Func0Throws<? extends T,E> absentMapping) throws E
      Produce a full NotNull. If this NotNull is full it is returned. Otherwise the absentMapping function is called to provide a value for the result NotNull.
      Parameters:
      absentMapping - produces value if this is empty
      Returns:
      a full NotNull
      Throws:
      E
    • mapThrows

      @Nonnull public abstract <U, E extends Exception> NotNull<U> mapThrows(@Nonnull Func1Throws<? super T,? extends U,E> presentMapping) throws E
      Produce a NotNull that is empty if this is empty or else contains the result of passing this value to the given mapping function.
      Parameters:
      presentMapping - maps this value to new value
      Returns:
      a possibly empty NotNull
      Throws:
      E
    • mapThrows

      @Nonnull public abstract <U, E extends Exception> NotNull<U> mapThrows(@Nonnull Func0Throws<? extends U,E> absentMapping, @Nonnull Func1Throws<? super T,? extends U,E> presentMapping) throws E
      Produce a full NotNull. If this is empty the absentMapping function is called to provide a value. Otherwise the presentMapping function is called to produce a new value based on this value.
      Parameters:
      absentMapping - produces value when this is empty
      presentMapping - maps this value to new value
      Returns:
      a full NotNull
      Throws:
      E
    • flatMap

      @Nonnull public abstract NotNull<T> flatMap(@Nonnull Func0<NotNull<T>> absentMapping)
      Produce a NotNull based on this one. If this is empty absentMapping is called to produce a new NotNull. Otherwise this is returned.
      Parameters:
      absentMapping - produce new NotNull if this is empty
      Returns:
      a NotNull
    • flatMap

      @Nonnull public abstract <A> NotNull<A> flatMap(@Nonnull Func1<? super T,NotNull<A>> presentMapping)
      Produce a NotNull based on this one. If this is full its value is passed to presentMapping to produce a new NotNull. Otherwise this is returned.
      Parameters:
      presentMapping - produce a new NotNull from this value
      Returns:
      a NotNull
    • flatMap

      @Nonnull public abstract <A> NotNull<A> flatMap(@Nonnull Func0<NotNull<A>> absentMapping, @Nonnull Func1<? super T,NotNull<A>> presentMapping)
      Produce a NotNull based on this one. If this is full its value is passed to presentMapping to produce a new NotNull. Otherwise absentMapping is called to produce a new NotNull..
      Parameters:
      absentMapping - produce a new NotNull when this is empty
      presentMapping - produce a new NotNull from this value
      Returns:
      a NotNull
    • flatMapThrows

      @Nonnull public abstract <E extends Exception> NotNull<T> flatMapThrows(@Nonnull Func0Throws<NotNull<T>,E> absentMapping) throws E
      Produce a NotNull based on this one. If this is empty absentMapping is called to produce a new NotNull. Otherwise this is returned.
      Parameters:
      absentMapping - produce new NotNull if this is empty
      Returns:
      a NotNull
      Throws:
      E
    • flatMapThrows

      @Nonnull public abstract <A, E extends Exception> NotNull<A> flatMapThrows(@Nonnull Func1Throws<? super T,NotNull<A>,E> presentMapping) throws E
      Produce a NotNull based on this one. If this is full its value is passed to presentMapping to produce a new NotNull. Otherwise this is returned.
      Parameters:
      presentMapping - produce a new NotNull from this value
      Returns:
      a NotNull
      Throws:
      E
    • flatMapThrows

      @Nonnull public abstract <A, E extends Exception> NotNull<A> flatMapThrows(@Nonnull Func0Throws<NotNull<A>,E> absentMapping, @Nonnull Func1Throws<? super T,NotNull<A>,E> presentMapping) throws E
      Produce a NotNull based on this one. If this is full its value is passed to presentMapping to produce a new NotNull. Otherwise absentMapping is called to produce a new NotNull..
      Parameters:
      absentMapping - produce a new NotNull when this is empty
      presentMapping - produce a new NotNull from this value
      Returns:
      a NotNull
      Throws:
      E
    • select

      @Nonnull public abstract NotNull<T> select(@Nonnull java.util.function.Predicate<? super T> predicate)
      Returns this if this is full and predicate returns true. Otherwise an empty NotNull is returned.
      Parameters:
      predicate - determines whether to accept this value
      Returns:
      a NotNull
    • reject

      @Nonnull public abstract NotNull<T> reject(@Nonnull java.util.function.Predicate<? super T> predicate)
      Returns this if this is full and predicate returns false. Otherwise an empty NotNull is returned.
      Parameters:
      predicate - determines whether to reject this value
      Returns:
      a NotNull
    • apply

      @Nonnull public abstract NotNull<T> apply(@Nonnull Proc0 absentAction)
      Invokes absentAction if this is empty.
      Parameters:
      absentAction - action to call if this is empty
      Returns:
      this
    • apply

      @Nonnull public abstract NotNull<T> apply(@Nonnull Proc1<? super T> presentAction)
      Invokes presentAction with this value if this is full.
      Parameters:
      presentAction - action to call if this is full
      Returns:
      this
    • applyThrows

      @Nonnull public abstract <E extends Exception> NotNull<T> applyThrows(@Nonnull Proc0Throws<E> absentAction) throws E
      Invokes absentAction if this is empty.
      Parameters:
      absentAction - action to call if this is empty
      Returns:
      this
      Throws:
      E
    • applyThrows

      @Nonnull public abstract <E extends Exception> NotNull<T> applyThrows(@Nonnull Proc1Throws<? super T,E> presentAction) throws E
      Invokes presentAction with this value if this is full.
      Parameters:
      presentAction - action to call if this is full
      Returns:
      this
      Throws:
      E
    • unsafeGet

      public abstract T unsafeGet()
      Gets this value. Throws NoSuchElementException if this is empty.
      Returns:
      this value
      Throws:
      NoSuchElementException - if this is empty
    • unsafeGet

      public abstract <E extends Exception> T unsafeGet(@Nonnull Func0<E> absentExceptionMapping) throws E
      Gets this value. Calls absentExceptionMapping to get an exception to throw if this is empty.
      Returns:
      this value
      Throws:
      E - an exception produced by mapping if this is empty
    • get

      public abstract T get(T absentValue)
      Gets this value. If this is empty returns absentValue instead.
      Parameters:
      absentValue - value to return if this is empty
    • getOrNull

      public abstract T getOrNull()
      Gets this value. If this is empty returns null.
    • getOr

      public abstract T getOr(@Nonnull Func0<? extends T> absentMapping)
      Gets this value. If this is empty returns result of calling absentMapping instead.
      Parameters:
      absentMapping - function to generate value to return if this is empty
    • match

      public abstract <U> U match(U absentValue, @Nonnull Func1<? super T,U> presentMapping)
      Gets a value based on this value. If this is empty absentValue is returned. Otherwise this value is passed to presentMapping to obtain a return value.
      Parameters:
      absentValue - value to return if this is empty
      presentMapping - function to map this value into return value
      Returns:
      a value
    • matchOr

      public abstract <U> U matchOr(@Nonnull Func0<U> absentMapping, @Nonnull Func1<? super T,U> presentMapping)
      Gets a value based on this value. If this is empty absentMapping is called to obtain a return value. Otherwise this value is passed to presentMapping to obtain a return value.
      Parameters:
      absentMapping - function to produce a value to return if this is empty
      presentMapping - function to map this value into return value
      Returns:
      a value
    • matchThrows

      public abstract <U, E extends Exception> U matchThrows(U absentValue, @Nonnull Func1Throws<? super T,U,E> presentMapping) throws E
      Gets a value based on this value. If this is empty absentValue is returned. Otherwise this value is passed to presentMapping to obtain a return value.
      Parameters:
      absentValue - value to return if this is empty
      presentMapping - function to map this value into return value
      Returns:
      a value
      Throws:
      E
    • matchOrThrows

      public abstract <U, E extends Exception> U matchOrThrows(@Nonnull Func0Throws<U,E> absentMapping, @Nonnull Func1Throws<? super T,U,E> presentMapping) throws E
      Gets a value based on this value. If this is empty absentMapping is called to obtain a return value. Otherwise this value is passed to presentMapping to obtain a return value.
      Parameters:
      absentMapping - function to produce a value to return if this is empty
      presentMapping - function to map this value into return value
      Returns:
      a value
      Throws:
      E
    • isEmpty

      public abstract boolean isEmpty()
      Returns true if this has no value
    • isFull

      public abstract boolean isFull()
      Returns true if this has a value