Module org.jhotdraw8.icollection
Interface Option<T>
- Type Parameters:
T- The type of the optional value.
- All Superinterfaces:
Iterable<T>,ReadOnlyCollection<T>
- All Known Implementing Classes:
Option.None,Option.Some
An Option is a read-only collection of one element or of zero elements.
Instances of Option are either an instance of Option.Some or of Option.None.
This class has been derived from 'vavr' Option.java.
- Option.java. Copyright 2023 (c) vavr. MIT License.
- github.com
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final recordstatic final record -
Method Summary
Modifier and TypeMethodDescriptionget()Gets the value if this is aSomeor throws if this is aNone.Returns the value if this is aSomeor theothervalue if this is aNone.booleanisEmpty()Returns true, if this isNone, otherwise false, if this isSome.static <T> Option<T> none()static <T> Option<T> of(T value) Creates a newOptionof a given value.Returns thisOptionif it is nonempty, otherwise return the alternative.orNull()Returns thisOptionif this is defined, ornullif it is empty.orThrow()Returns thisOptionif this is defined, or throws aNoSuchElementExceptionif it is empty.static <T> Option<T> some(T value) Methods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlyCollection
asCollection, characteristics, contains, containsAll, iterator, size, stream, toArray, toArray
-
Method Details
-
none
-
some
-
of
Creates a newOptionof a given value.// = Some(3), an Option which contains the value 3 Option<Integer> option = Option.of(3); // = None, the empty Option Option<Integer> none = Option.of(null);- Type Parameters:
T- type of the value- Parameters:
value- A value- Returns:
Some(value)if value is notnull,Noneotherwise
-
isEmpty
boolean isEmpty()Returns true, if this isNone, otherwise false, if this isSome.// Prints "false" System.out.println(Option.of(10).isEmpty()); // Prints "true" System.out.println(Option.none().isEmpty());- Specified by:
isEmptyin interfaceReadOnlyCollection<T>- Returns:
- true, if this
Optionis empty, false otherwise
-
orElse
Returns thisOptionif it is nonempty, otherwise return the alternative.Option<String> other = Option.of("Other"); // = Some("Hello World") Option.of("Hello World").orElse(other); // = Some("Other") Option.none().orElse(other);- Parameters:
other- An alternativeOption- Returns:
- this
Optionif it is nonempty, otherwise return the alternative.
-
getOrElse
Returns the value if this is aSomeor theothervalue if this is aNone.Please note, that the other value is eagerly evaluated.
// Prints "Hello" System.out.println(Option.of("Hello").getOrElse("World")); // Prints "World" Option.none().getOrElse("World");- Parameters:
other- An alternative value- Returns:
- This value, if this Option is defined or the
othervalue, if this Option is empty.
-
get
Gets the value if this is aSomeor throws if this is aNone.// Prints "57" System.out.println(Option.of(57).get()); // Throws a NoSuchElementException Option.none().get();- Returns:
- the value
- Throws:
NoSuchElementException- if this is aNone.
-
orNull
Returns thisOptionif this is defined, ornullif it is empty.// = Some("Hello World") Option.of("Hello World").orNull(); // = null Option.none().orNull();- Returns:
- this value if it is defined, or
nullif it is empty.
-
orThrow
Returns thisOptionif this is defined, or throws aNoSuchElementExceptionif it is empty.// = Some("Hello World") Option.of("Hello World").orThrow(); // = null Option.none().orThrow();- Returns:
- this value if it is defined, or
nullif it is empty.
-