Module com.machinezoo.noexception
Interface OptionalComparator<T>
-
- Type Parameters:
-
T
- seeComparator
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface OptionalComparator<T>
Variation ofComparator
that returnsOptionalInt
instead of the raw value.OptionalComparator
is typically obtained fromExceptionHandler.comparator(Comparator)
, in which case its return value is empty when the underlyingComparator
throws an exception. See noexception tutorial.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description OptionalInt
compare(T left, T right)
Variation ofComparator.compare(Object, Object)
that returnsOptionalInt
.default Comparator<T>
orElse(int result)
Converts thisOptionalComparator
to plainComparator
using default value.default Comparator<T>
orElseGet(IntSupplier source)
-
-
-
Method Detail
-
compare
OptionalInt compare(T left, T right)
Variation ofComparator.compare(Object, Object)
that returnsOptionalInt
. If thisOptionalComparator
is obtained fromExceptionHandler.comparator(Comparator)
, theOptionalInt
will be empty only if the underlyingComparator
throws. Otherwise the returnedOptionalInt
just wraps the return value of underlyingComparator
.- Parameters:
-
left
- seeComparator.compare(Object, Object)
-
right
- seeComparator.compare(Object, Object)
- Returns:
-
OptionalInt
typically wrapping return value ofComparator.compare(Object, Object)
, or an emptyOptionalInt
(typically signifying an exception) - See Also:
-
ExceptionHandler.comparator(Comparator)
,Comparator.compare(Object, Object)
-
orElse
default Comparator<T> orElse(int result)
Converts thisOptionalComparator
to plainComparator
using default value. The returnedComparator
will unwrap present value from theOptionalInt
if possible, or returnresult
if theOptionalInt
is empty.- Parameters:
-
result
- default value to return instead of an emptyOptionalInt
- Returns:
-
plain
Comparator
that either unwrapsOptionalInt
or returns default value - See Also:
-
orElseGet(IntSupplier)
,OptionalInt.orElse(int)
-
orElseGet
default Comparator<T> orElseGet(IntSupplier source)
Converts thisOptionalComparator
to plainComparator
using fallbackIntSupplier
. The returnedComparator
will unwrap present value from theOptionalInt
if possible, or fall back to callingsource
if theOptionalInt
is empty.- Parameters:
-
source
-IntSupplier
to query for fallback value whenOptionalInt
is empty - Returns:
-
plain
Comparator
that either unwrapsOptionalInt
or falls back tosource
- See Also:
-
orElse(int)
,OptionalInt.orElseGet(IntSupplier)
-
-