- Type Parameters:
C
- the type of component held by this instance
Component
types
which may or may not contain a non-null
value.
If a value is present, isPresent()
returns true
. If no
value is present, the object is considered empty and
isPresent()
returns false
.
Additional methods that depend on the presence or absence of a contained
value are provided, such as orElse()
(returns a default value if no value is present) and
ifPresent()
(performs an
action if a value is present).
This is a value-based
class; use of identity-sensitive operations (including reference equality
(==
), identity hash code, or synchronization) on instances of
OptionalUI
may have unpredictable results and should be avoided.
Note that
OptionalUI
is primarily intended for use as a SwingTree query return type where
there is a clear need to represent "no result," and where returning null
as well
as expose the UI components to the application thread directly
is likely to cause errors. A variable whose type is OptionalUI
should
never itself be null
; it should always point to an OptionalUI
instance.
Please take a look at the living swing-tree documentation where you can browse a large collection of examples demonstrating how to use the API of this class.
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Indicates whether some other object is "equal to" thisOptionalUI
.If a component is present, and the component matches the given predicate, returns anOptionalUI
describing the component, otherwise returns an emptyOptionalUI
.int
hashCode()
Returns the hash code of the component, if present, otherwise0
(zero) if no component is present.void
If a component is present, performs the given action with the component, otherwise does nothing.void
ifPresentOrElse
(Consumer<? super C> action, Runnable emptyAction) If a component is present, performs the given action with the component, otherwise performs the given empty-based action.boolean
isEmpty()
If a component is not present, returnstrue
, otherwisefalse
.boolean
If a component is present, returnstrue
, otherwisefalse
.<U> Optional<U>
If a component is present, returns anOptionalUI
describing (as if byofNullable(T)
) the result of applying the given mapping function to the component, otherwise returns an emptyOptionalUI
.or
(Supplier<? extends OptionalUI<? extends C>> supplier) If a component is present, returns anOptionalUI
describing the component, otherwise returns anOptionalUI
produced by the supplying function.If a component is present, returns the component, otherwise returnsother
.If a component is present, returns the component, otherwise returns the result produced by the supplying function.orElseNullable
(C other) If a component is present, returns the component, otherwise returnsother
or throws a null pointer exception ifother
isnull
.If a component is present, returns the component, otherwise throwsNoSuchElementException
.orElseThrow
(Supplier<? extends X> exceptionSupplier) If a component is present, returns the component, otherwise throws an exception produced by the exception supplying function.@Nullable C
orNull()
If a component is present, returns the component, otherwise returnsnull
.toString()
Returns a non-empty string representation of thisOptionalUI
suitable for debugging.
-
Method Details
-
isPresent
public boolean isPresent()If a component is present, returnstrue
, otherwisefalse
.- Returns:
true
if a component is present, otherwisefalse
-
isEmpty
public boolean isEmpty()If a component is not present, returnstrue
, otherwisefalse
.- Returns:
true
if a component is not present, otherwisefalse
- Since:
- 11
-
ifPresent
If a component is present, performs the given action with the component, otherwise does nothing.- Parameters:
action
- the action to be performed, if a component is present- Throws:
NullPointerException
- if component is present and the given action isnull
-
ifPresentOrElse
If a component is present, performs the given action with the component, otherwise performs the given empty-based action.- Parameters:
action
- the action to be performed, if a component is presentemptyAction
- the empty-based action to be performed, if no component is present- Throws:
NullPointerException
- if a component is present and the given action isnull
, or no component is present and the given empty-based action isnull
.- Since:
- 9
-
filter
If a component is present, and the component matches the given predicate, returns anOptionalUI
describing the component, otherwise returns an emptyOptionalUI
.- Parameters:
predicate
- the predicate to apply to a component, if present- Returns:
- an
OptionalUI
describing the component of thisOptionalUI
, if a component is present and the component matches the given predicate, otherwise an emptyOptionalUI
- Throws:
NullPointerException
- if the predicate isnull
-
map
If a component is present, returns anOptionalUI
describing (as if byofNullable(T)
) the result of applying the given mapping function to the component, otherwise returns an emptyOptionalUI
.If the mapping function returns a
null
result then this method returns an emptyOptionalUI
.- Type Parameters:
U
- The type of the component returned from the mapping function- Parameters:
mapper
- the mapping function to apply to a component, if present- Returns:
- an
Optional
describing the result of applying a mapping function to the UI component of thisOptionalUI
, if a component is present, otherwise an emptyOptionalUI
- Throws:
NullPointerException
- if the mapping function isnull
-
or
If a component is present, returns anOptionalUI
describing the component, otherwise returns anOptionalUI
produced by the supplying function. Use this to provide for alternative UI tree querying operations.- Parameters:
supplier
- the supplying function that produces anOptionalUI
to be returned- Returns:
- returns an
OptionalUI
describing the component of thisOptionalUI
, if a component is present, otherwise anOptionalUI
produced by the supplying function. - Throws:
NullPointerException
- if the supplying function isnull
or produces anull
result- Since:
- 9
-
orElseNullable
If a component is present, returns the component, otherwise returnsother
or throws a null pointer exception ifother
isnull
.- Parameters:
other
- the component to be returned, if no component is present. May not benull
.- Returns:
- the component, if present, otherwise
other
-
orElse
If a component is present, returns the component, otherwise returnsother
.- Parameters:
other
- the component to be returned, if no component is present. May not benull
, useorElseNullable(Component)
if it can be null.- Returns:
- the component, if present, otherwise
other
-
orNull
If a component is present, returns the component, otherwise returnsnull
.- Returns:
- The component wrapped in this OptionalUI, or null if no component is present.
-
orElseGet
If a component is present, returns the component, otherwise returns the result produced by the supplying function.- Parameters:
supplier
- the supplying function that produces a component to be returned- Returns:
- the component, if present, otherwise the result produced by the supplying function
- Throws:
NullPointerException
- if no component is present and the supplying function isnull
-
orElseThrow
If a component is present, returns the component, otherwise throwsNoSuchElementException
.- Returns:
- the non-
null
component described by thisOptionalUI
- Throws:
NoSuchElementException
- if no component is present- Since:
- 10
-
orElseThrow
If a component is present, returns the component, otherwise throws an exception produced by the exception supplying function. Note that A method reference to the exception constructor with an empty argument list can be used as the supplier. For example,IllegalStateException::new
- Type Parameters:
X
- Type of the exception to be thrown- Parameters:
exceptionSupplier
- the supplying function that produces an exception to be thrown- Returns:
- the component, if present
- Throws:
X
- if no component is presentNullPointerException
- if no component is present and the exception supplying function isnull
-
equals
Indicates whether some other object is "equal to" thisOptionalUI
. The other object is considered equal if:- it is also an
OptionalUI
and; - both instances have no component present or;
- the present components are "equal to" each other via
equals()
.
- it is also an
-
hashCode
public int hashCode()Returns the hash code of the component, if present, otherwise0
(zero) if no component is present. -
toString
Returns a non-empty string representation of thisOptionalUI
suitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions. If a component is present the result must include its string representation in the result. Empty and presentOptionalUI
s must be unambiguously differentiable.
-