E
- The type of the optional element in this container.@Immutable public abstract class Option<E> extends AbstractCollection<E> implements Serializable
Option<String> option = Option.apply("Hello world!"); // or Option.apply(null)
for (String string : option) System.out.println(string);
A less idiomatic way is the following:
Option<String> option = Option.some("Hello world!"); // or Option.none()
if (!option.isEmpty()) System.out.println(option.get());
If you use this class in these ways, your code clearly expresses the
intention that its prepared to deal with the absence of an object in a
collection and won't throw a NullPointerException
in this case.
Here's a more complex example with composed options:
class Container {
Option<String> getMessage() { return Option.some("Hello world!"); }
}
Option<Container> option = Option.some(new Container()); // or Option.none()
for (Container c : option)
for (String s : c.getMessage())
System.out.println(s);
This class is inspired by the Scala Library and checked with Google's Guava
Library:
A noteable difference to Scala's Option
class is that this
collection class cannot contain null elements because I can't imagine a
valid use case - not even when considering interoperability with other
collections of nullable items.
A noteable difference to Guava's Optional
class is that this class
is a collection while Optional
is not, so you can't use a for-loop
with the latter.
A noteable difference to both libraries is that this class doesn't support a generic Function interface. This is because without support for closures in Java 7, using a generic functional interface in Java is not as convenient as the for-loop.
Modifier and Type | Method and Description |
---|---|
static <T> Option<T> |
apply(T element)
Returns an option for the given nullable element.
|
abstract boolean |
equals(Object other) |
abstract E |
get()
If present, returns the single element contained in this collection,
or otherwise throws an exception.
|
abstract E |
getOrElse(E alternative)
If present, returns the single element contained in this collection,
or otherwise the given alternative.
|
abstract int |
hashCode() |
static <T> Option<T> |
none()
Returns an option with no element.
|
Option<E> |
orElse(Option<E> alternative)
If an element is present in this collection, then this collection is
returned, or otherwise the given alternative.
|
abstract E |
orNull()
Equivalent to
getOrElse(null) , but probably more
efficient. |
static <T> Option<T> |
some(T element)
Returns an option with the given element.
|
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
parallelStream, removeIf, spliterator, stream
public static <T> Option<T> apply(@CheckForNull T element)
T
- The type of the nullable element.element
- the element.public abstract boolean equals(Object other)
equals
in interface Collection<E>
equals
in class Object
public abstract E get() throws NoSuchElementException
NoSuchElementException
- if no element is present in this
collection.@Nullable public abstract E getOrElse(@CheckForNull E alternative)
alternative
- the alternative element.public abstract int hashCode()
hashCode
in interface Collection<E>
hashCode
in class Object
public static <T> Option<T> none()
T
- the type of the absent element.public Option<E> orElse(Option<E> alternative)
alternative
- the alternative option.@Nullable public abstract E orNull()
getOrElse(null)
, but probably more
efficient.public static <T> Option<T> some(T element)
T
- the type of the element.element
- the element in this option.NullPointerException
- if element
is null
.Copyright © 2012–2017 Schlichtherle IT Services. All rights reserved.