java.lang.Object
me.sbasalaev.collection.Collection<T>
me.sbasalaev.Opt<T>
- All Implemented Interfaces:
Cloneable,Iterable<T>,Traversable<T>
Optional value.
Opt may contain a single value or no value. The value may be
fetched by a family of orElse methods providing the default
in case the value is absent, handled in a pattern maching style as
return optional.match(
(value) -> ...,
() -> ...
);
or traversed in a for statement
for (var value : optional) {
...
}
This class predates Optional introduced by Java 8 that
serves the same purpose but is not traversable.
Values can be converted between Opt and Optional
with fromJava() and toJava().-
Method Summary
Modifier and TypeMethodDescriptionclone()Returns this optional.static <T> Opt<T> empty()Empty optional.booleanWhether given object is equal to this optional.Returns this optional if its value matches given condition, empty optional otherwise.<R> Rfold(R first, BiFunction<? super R, ? super @Out T, ? extends R> combine) Starting from thefirst, appliescombineto elements of the collection returning result.static <T> Opt<T> Converts Java optional to Opt.inthashCode()Hashcode of the value or 0 for empty optional.iterator()<R> Opt<R> Maps value in this optional using given mapping.<R> REither maps the value or produces one if the optional is empty.voidEither runsactionon the value oremptyActionif the optional is empty.<U> Opt<U> Returns this optional if its value is of given class, empty optional otherwise.static <T> Opt<T> of(T value) Non-empty optional.static <T> Opt<T> ofNullable(@Nullable T value) Optional of non-null value, empty optional for null.Returns value in this optional ordefaultValueif the optional is empty.Returns value in this optional or the value fromvalueSupplierif the optional is empty.Returns value in this optional ornullif the optional is empty.orElseThrow(String message) Returns value in this optional or throwsNoSuchElementExceptionwith givenmessage.orElseThrow(Supplier<X> exceptionSupplier) Returns value in this optional or throws the exception from given supplier.intsize()Number of elements in this collection.Creates aSpliteratorover elements of this collection.stream()Returns a sequentialStreamwith this collection as its source.toJava()Converts this value to Java optional.Methods inherited from class me.sbasalaev.collection.Collection
count, fillArray, isEmpty, nonEmpty, toArray, toArray, toList, toSet, toString
-
Method Details
-
empty
Empty optional. -
of
Non-empty optional. -
ofNullable
Optional of non-null value, empty optional for null. -
fromJava
Converts Java optional to Opt. -
orElseNull
Returns value in this optional ornullif the optional is empty. -
orElse
Returns value in this optional ordefaultValueif the optional is empty. -
orElseGet
Returns value in this optional or the value fromvalueSupplierif the optional is empty. -
orElseThrow
Returns value in this optional or throwsNoSuchElementExceptionwith givenmessage.- Throws:
NoSuchElementException
-
orElseThrow
Returns value in this optional or throws the exception from given supplier.- Throws:
X- Since:
- 3.2
-
match
Either maps the value or produces one if the optional is empty. Can be used in a pattern matching style:return optional.match( (value) -> ..., () -> ... ); -
matchDo
Either runsactionon the value oremptyActionif the optional is empty. Can be used in a pattern matching style:optional.matchDo( (value) -> { ... }, () -> { ... } ); -
toJava
Converts this value to Java optional. -
mapped
Maps value in this optional using given mapping. This is eager operation that applies the mapping immediately.- Specified by:
mappedin classCollection<T>
-
filtered
Returns this optional if its value matches given condition, empty optional otherwise. This is eager operation that tests the condition immediately.- Specified by:
filteredin classCollection<T>
-
narrow
Returns this optional if its value is of given class, empty optional otherwise. -
spliterator
Description copied from class:CollectionCreates aSpliteratorover elements of this collection. The spliterator reportsSpliterator.NONNULLandSpliterator.SIZED.- Specified by:
spliteratorin interfaceIterable<T>- Specified by:
spliteratorin classCollection<T>
-
stream
Description copied from class:CollectionReturns a sequentialStreamwith this collection as its source.- Overrides:
streamin classCollection<T>
-
clone
Returns this optional.- Specified by:
clonein classCollection<T>
-
fold
Description copied from interface:TraversableStarting from thefirst, appliescombineto elements of the collection returning result. The order of elements is determined by the iterator. -
size
public int size()Description copied from class:CollectionNumber of elements in this collection. UnlikeTraversable.count()this method is guaranteed to be fast.- Specified by:
sizein classCollection<T>
-
iterator
-
equals
Whether given object is equal to this optional. The object is equal to this optional if it is an instance of Opt and either both are empty or contain equal values.- Specified by:
equalsin classCollection<T>
-
hashCode
public int hashCode()Hashcode of the value or 0 for empty optional.- Specified by:
hashCodein classCollection<T>
-