T - Argument type.public abstract class Opt<T>
extends java.lang.Object
implements java.lang.Iterable<T>, java.io.Serializable
| Modifier | Constructor and Description |
|---|---|
protected |
Opt() |
| Modifier and Type | Method and Description |
|---|---|
static <T> Opt<T> |
absent() |
abstract boolean |
exists(Pred<? super T> predicate) |
abstract Opt<T> |
filter(Pred<? super T> predicate) |
abstract <V> Opt<V> |
flatMap(Func<? super T,Opt<V>> function) |
static <T> Opt<T> |
fromCollection(java.util.Collection<T> col)
Create an
Opt instance out of a Collection. |
abstract T |
get() |
abstract T |
getOrElse(T defaultValue) |
abstract T |
getOrElseNull() |
abstract boolean |
isPresent() |
java.util.Iterator<T> |
iterator() |
abstract <V> Opt<V> |
map(Func<? super T,V> function) |
static <T> Opt<T> |
of(T value) |
static <T> Opt<T> |
ofNullable(T value) |
abstract Opt<T> |
orElse(Opt<? extends T> alternative) |
abstract java.util.List<T> |
toList() |
abstract <K> java.util.Map<K,T> |
toMap(K key) |
abstract java.util.Set<T> |
toSet() |
public static <T> Opt<T> absent()
T - Absent value type.Opt instance that contains no value.public static <T> Opt<T> of(T value)
T - Value type.value - Value to wrap in an Opt, must be non-null.Opt instance with the given value.java.lang.NullPointerException - If value is null.public static <T> Opt<T> ofNullable(T value)
T - Value type.value - Possibly nullable value.Opt if the value is non-null, or an empty one otherwise.public static <T> Opt<T> fromCollection(java.util.Collection<T> col)
Opt instance out of a Collection. The collection's size may not be greater than 1.
Returns a non-empty Opt with the collection's only element if the collection is non-empty,
or an empty Opt otherwise.T - Collection type.col - Collection to extract an Opt out of. The collection's size may not be greater than 1.Opt if the Collection has a single element, or an empty one otherwise.java.lang.IllegalArgumentException - If the collection's size is greater than 1.public abstract boolean isPresent()
true if this Opt contains a value, otherwise false.public abstract T get()
Opt's non-null value.java.util.NoSuchElementException - If this Opt is empty.public abstract T getOrElseNull()
Opt's value if this Opt is non-empty, or null otherwise.public abstract T getOrElse(T defaultValue)
defaultValue - Default value to return if this Opt is empty.Opt's value if this Opt is non-empty, or defaultValue otherwise.public abstract Opt<T> orElse(Opt<? extends T> alternative)
alternative - Alternative Opt to return if this Opt is empty.Opt if it is non-empty, or the alternative Opt otherwise.public abstract <V> Opt<V> map(Func<? super T,V> function)
V - Returned Opt type.function - Function to apply to this Opt's value, if this Opt is non-empty.
May return null, in which case an empty Opt will be returned.Opt containing the result of applying function to this Opt's value
if this Opt is non-empty, or an empty Opt otherwise.public abstract <V> Opt<V> flatMap(Func<? super T,Opt<V>> function)
V - Returned Opt type.function - Function to apply to this Opt's value, if this Opt is non-empty.
Different from map(Func) in that function must return an Opt.Opt result of applying function to this Opt's value if this Opt is non-empty,
or an empty Opt otherwise.public abstract boolean exists(Pred<? super T> predicate)
predicate - Predicate to apply to this Opt's value if this Opt is non-empty.true if this Opt is non-empty and applying the predicate to this Opt's
value returns true, otherwise returns false.public abstract Opt<T> filter(Pred<? super T> predicate)
predicate - Predicate to apply to this Opt's value if this Opt is non-empty.Opt if this Opt is non-empty and applying the predicate to it returns true,
otherwise returns an empty Opt.public abstract java.util.List<T> toList()
List containing this Opt's value if this Opt is non-empty,
or an empty List otherwise.public abstract java.util.Set<T> toSet()
Set containing this Opt's value if this Opt is non-empty,
or an empty Set otherwise.public abstract <K> java.util.Map<K,T> toMap(K key)
K - Key type.key - Key to use for mapping if this Opt is non-empty.Map containing a single mapping from the given key to this Opt's value
if this Opt is non-empty, or an empty Map otherwise.