Package org.javimmutable.collections
Class NotNull<T>
java.lang.Object
org.javimmutable.collections.NotNull<T>
- All Implemented Interfaces:
Serializable
,Iterable<T>
,IStreamable<T>
,SplitableIterable<T>
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 Summary
Modifier and TypeMethodDescriptionInvokes absentAction if this is empty.Invokes presentAction with this value if this is full.applyThrows
(Proc0Throws<E> absentAction) Invokes absentAction if this is empty.applyThrows
(Proc1Throws<? super T, E> presentAction) Invokes presentAction with this value if this is full.static <T> NotNull
<T> Determine if the object is an instance of the specified Class or a subclass.static <T> NotNull
<T> empty()
Returns aNotNull
with no value.static <T> NotNull
<T> Returns aNotNull
containing the first value of the collection.static <T> NotNull
<T> Returns aNotNull
containing the first value of the collection for which the predicate returns true.abstract <A> NotNull
<A> Produce aNotNull
based on this one.Produce aNotNull
based on this one.abstract <A> NotNull
<A> Produce aNotNull
based on this one.flatMapThrows
(Func0Throws<NotNull<A>, E> absentMapping, Func1Throws<? super T, NotNull<A>, E> presentMapping) Produce aNotNull
based on this one.flatMapThrows
(Func0Throws<NotNull<T>, E> absentMapping) Produce aNotNull
based on this one.flatMapThrows
(Func1Throws<? super T, NotNull<A>, E> presentMapping) Produce aNotNull
based on this one.abstract T
Gets this value.abstract T
Gets this value.abstract T
Gets this value.abstract boolean
isEmpty()
Returns true if this has no valueabstract boolean
isFull()
Returns true if this has a valueProduce a fullNotNull
.abstract <U> NotNull
<U> Produce a fullNotNull
.abstract <U> NotNull
<U> Produce aNotNull
that is empty if this is empty or else contains the result of passing this value to the given mapping function.mapThrows
(Func0Throws<? extends T, E> absentMapping) Produce a fullNotNull
.mapThrows
(Func0Throws<? extends U, E> absentMapping, Func1Throws<? super T, ? extends U, E> presentMapping) Produce a fullNotNull
.mapThrows
(Func1Throws<? super T, ? extends U, E> presentMapping) Produce aNotNull
that is empty if this is empty or else contains the result of passing this value to the given mapping function.abstract <U> U
Gets a value based on this value.abstract <U> U
Gets a value based on this value.abstract <U,
E extends Exception>
UmatchOrThrows
(Func0Throws<U, E> absentMapping, Func1Throws<? super T, U, E> presentMapping) Gets a value based on this value.abstract <U,
E extends Exception>
UmatchThrows
(U absentValue, Func1Throws<? super T, U, E> presentMapping) Gets a value based on this value.maybe()
If this empty returnsMaybe.empty()
, otherwise returnsMaybe.of(T)
.static <T> NotNull
<T> of
(T valueOrNull) Returns aNotNull
containing the value.Returns this if this is full and predicate returns false.Returns this if this is full and predicate returns true.abstract T
Gets this value.Gets this value.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.javimmutable.collections.IStreamable
getSpliteratorCharacteristics, iterator, parallelStream, spliterator, stream
Methods inherited from interface org.javimmutable.collections.SplitableIterable
forEachThrows, indexedForEach, indexedForEachThrows, reduce, reduceThrows
-
Method Details
-
empty
-
of
Returns aNotNull
containing the value. Null is treated as empty. -
cast
Determine if the object is an instance of the specified Class or a subclass. If the object is null, returns aNotNull
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 tovalueOrNull
- object to be case- Returns:
- a
NotNull
-
first
-
first
-
maybe
If this empty returnsMaybe.empty()
, otherwise returnsMaybe.of(T)
. -
map
Produce a fullNotNull
. If thisNotNull
is full it is returned. Otherwise the absentMapping function is called to provide a value for the resultNotNull
.- Parameters:
absentMapping
- produces value if this is empty- Returns:
- a full
NotNull
-
map
Produce aNotNull
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 fullNotNull
. 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 emptypresentMapping
- 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 EProduce a fullNotNull
. If thisNotNull
is full it is returned. Otherwise the absentMapping function is called to provide a value for the resultNotNull
.- 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, throws EE> presentMapping) Produce aNotNull
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, throws E? extends U, E> presentMapping) Produce a fullNotNull
. 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 emptypresentMapping
- maps this value to new value- Returns:
- a full
NotNull
- Throws:
E
-
flatMap
-
flatMap
@Nonnull public abstract <A> NotNull<A> flatMap(@Nonnull Func1<? super T, NotNull<A>> presentMapping) -
flatMap
@Nonnull public abstract <A> NotNull<A> flatMap(@Nonnull Func0<NotNull<A>> absentMapping, @Nonnull Func1<? super T, NotNull<A>> presentMapping) -
flatMapThrows
@Nonnull public abstract <E extends Exception> NotNull<T> flatMapThrows(@Nonnull Func0Throws<NotNull<T>, E> absentMapping) throws E -
flatMapThrows
@Nonnull public abstract <A,E extends Exception> NotNull<A> flatMapThrows(@Nonnull Func1Throws<? super T, NotNull<A>, throws EE> presentMapping) -
flatMapThrows
@Nonnull public abstract <A,E extends Exception> NotNull<A> flatMapThrows(@Nonnull Func0Throws<NotNull<A>, E> absentMapping, @Nonnull Func1Throws<? super T, throws ENotNull<A>, E> presentMapping) -
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 emptyNotNull
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 emptyNotNull
is returned.- Parameters:
predicate
- determines whether to reject this value- Returns:
- a
NotNull
-
apply
Invokes absentAction if this is empty.- Parameters:
absentAction
- action to call if this is empty- Returns:
- this
-
apply
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 EInvokes presentAction with this value if this is full.- Parameters:
presentAction
- action to call if this is full- Returns:
- this
- Throws:
E
-
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
Gets this value. If this is empty returns absentValue instead.- Parameters:
absentValue
- value to return if this is empty
-
getOrNull
Gets this value. If this is empty returns null. -
getOr
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
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 emptypresentMapping
- 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 emptypresentMapping
- 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, throws EE> 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 emptypresentMapping
- 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, throws EU, E> 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 emptypresentMapping
- 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
-