public final class Container<T> extends Object
| Modifier and Type | Method and Description |
|---|---|
Optional<T> |
asOptional()
returns the value as an optional
|
static <T> Container<T> |
empty()
Returns an empty
Container instance. |
boolean |
equals(Object obj)
Indicates whether some other object is "equal to" this
Container. |
Container<T> |
filter(java.util.function.Predicate<? super T> predicate)
If a value is present, and the value matches the given predicate, returns an
Container describing the value, otherwise returns an empty
Container. |
<U> Container<U> |
flatMap(java.util.function.Function<? super T,? extends Container<? extends U>> mapper)
If a value is present, returns the result of applying the given
Container-bearing mapping function to the value, otherwise returns an
empty Container. |
T |
get()
returns the value
|
int |
hashCode()
Returns the hash code of the value, if present, otherwise
0 (zero) if
no value is present. |
void |
ifPresent(java.util.function.Consumer<? super T> action)
If a value is present, performs the given action with the value, otherwise
does nothing.
|
void |
ifPresentOrElse(java.util.function.Consumer<? super T> action,
Runnable emptyAction)
If a value is present, performs the given action with the value, otherwise
performs the given empty-based action.
|
boolean |
isEmpty()
If a value is not present, returns
true, otherwise false. |
boolean |
isModifiable()
returns the modifiable
|
boolean |
isPresent()
If a value is present, returns
true, otherwise false. |
Container<T> |
lock()
locks the container
|
<U> Container<U> |
map(java.util.function.Function<? super T,? extends U> mapper)
If a value is present, returns an
Container describing the result of
applying the given mapping function to the value, otherwise returns an empty
Container. |
static <T> Container<T> |
of()
Returns an
Container |
static <T> Container<T> |
of(T value)
Returns an
Container describing the given value. |
static <T> Container<T> |
ofOptional(Optional<T> value)
Returns an
Container describing the given value of the optional. |
static <T> Container<T> |
ofOptionalUnmodifiable(Optional<T> value)
Returns an
Container describing the given value of the optional. |
static <T> Container<T> |
ofUnmodifiable(T value)
Returns an
Container describing the given value. |
Container<T> |
or(java.util.function.Supplier<? extends Container<? extends T>> supplier)
If a value is present, returns an
Container describing the value,
otherwise returns an Container produced by the supplying function. |
T |
orElse(T other)
If a value is present, returns the value, otherwise returns
other. |
T |
orElseGet(java.util.function.Supplier<? extends T> supplier)
If a value is present, returns the value, otherwise returns the result
produced by the supplying function.
|
T |
orElseThrow()
If a value is present, returns the value, otherwise throws
NoSuchElementException. |
<X extends Throwable> |
orElseThrow(java.util.function.Supplier<? extends X> exceptionSupplier)
If a value is present, returns the value, otherwise throws an exception
produced by the exception supplying function.
|
Container<T> |
replace(T value)
replaces the value with the given value
|
java.util.stream.Stream<T> |
stream()
If a value is present, returns a sequential
Stream containing only
that value, otherwise returns an empty Stream. |
String |
toString()
Returns a non-empty string representation of this
Container suitable
for debugging. |
public static <T> Container<T> empty()
Container instance. No value is present for this
Container.T - The type of the non-existent valueContainerpublic static <T> Container<T> of()
ContainerT - the type of the valueContainerpublic static <T> Container<T> of(T value)
Container describing the given value.T - the type of the valuevalue - the value to describeContainer with the valuepublic static <T> Container<T> ofUnmodifiable(T value)
Container describing the given value.T - the type of the valuevalue - the value to describeContainer with the value or emptypublic static <T> Container<T> ofOptional(Optional<T> value)
Container describing the given value of the optional.T - the type of the valuevalue - {code Optional} that contains a value or nullContainer with the value of the optionalpublic static <T> Container<T> ofOptionalUnmodifiable(Optional<T> value)
Container describing the given value of the optional.T - the type of the valuevalue - {code Optional} that contains a value or nullContainer with the value of the optionalpublic Container<T> replace(T value)
value - the value to describeContainerpublic T get()
Containerpublic boolean isModifiable()
true if a value is present, otherwise falsepublic boolean isPresent()
true, otherwise false.true if a value is present, otherwise falsepublic boolean isEmpty()
true, otherwise false.true if a value is not present, otherwise falsepublic void ifPresent(java.util.function.Consumer<? super T> action)
action - the action to be performed, if a value is presentNullPointerException - if value is present and the given action is
nullpublic void ifPresentOrElse(java.util.function.Consumer<? super T> action, Runnable emptyAction)
action - the action to be performed, if a value is presentemptyAction - the empty-based action to be performed, if no value is
presentNullPointerException - if a value is present and the given action is
null, or no value is present and the
given empty-based action is null.public Container<T> filter(java.util.function.Predicate<? super T> predicate)
Container describing the value, otherwise returns an empty
Container.predicate - the predicate to apply to a value, if presentContainer describing the value of this Container,
if a value is present and the value matches the given predicate,
otherwise an empty ContainerNullPointerException - if the predicate is nullpublic <U> Container<U> map(java.util.function.Function<? super T,? extends U> mapper)
Container describing the result of
applying the given mapping function to the value, otherwise returns an empty
Container.
If the mapping function returns a null result then this method
returns an empty Container.
U - The type of the value returned from the mapping functionmapper - the mapping function to apply to a value, if presentContainer describing the result of applying a mapping
function to the value of this Container, if a value is
present, otherwise an empty ContainerNullPointerException - if the mapping function is nullpublic <U> Container<U> flatMap(java.util.function.Function<? super T,? extends Container<? extends U>> mapper)
Container-bearing mapping function to the value, otherwise returns an
empty Container.
This method is similar to map(Function), but the mapping function is
one whose result is already an Container, and if invoked,
flatMap does not wrap it within an additional Container.
U - The type of value of the Container returned by the
mapping functionmapper - the mapping function to apply to a value, if presentContainer-bearing mapping function
to the value of this Container, if a value is present,
otherwise an empty ContainerNullPointerException - if the mapping function is null or
returns a null resultpublic Container<T> or(java.util.function.Supplier<? extends Container<? extends T>> supplier)
Container describing the value,
otherwise returns an Container produced by the supplying function.supplier - the supplying function that produces an Container to
be returnedContainer describing the value of this
Container, if a value is present, otherwise an
Container produced by the supplying function.NullPointerException - if the supplying function is null or
produces a null resultpublic java.util.stream.Stream<T> stream()
Stream containing only
that value, otherwise returns an empty Stream.Streampublic T orElse(T other)
other.other - the value to be returned, if no value is present. May be
null.otherpublic T orElseGet(java.util.function.Supplier<? extends T> supplier)
supplier - the supplying function that produces a value to be returnedNullPointerException - if no value is present and the supplying
function is nullpublic T orElseThrow()
NoSuchElementException.null value described by this ContainerNoSuchElementException - if no value is presentpublic <X extends Throwable> T orElseThrow(java.util.function.Supplier<? extends X> exceptionSupplier) throws X extends Throwable
X - Type of the exception to be thrownexceptionSupplier - the supplying function that produces an exception to
be thrownX - if no value is presentNullPointerException - if no value is present and the exception
supplying function is nullX extends Throwablepublic boolean equals(Object obj)
Container. The
other object is considered equal if:
Container and;
equals().
public int hashCode()
0 (zero) if
no value is present.public String toString()
Container suitable
for debugging. The exact presentation format is unspecified and may vary
between implementations and versions.Copyright © 2021. All rights reserved.