- 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.@Nullable C
orElseNullable
(@Nullable 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.If no component is present, the supplying function is called to provide an alternative UI component to be used in place of the missing component.If no component is present and the supplied boolean is true, the supplying function is called to provide an alternative UI component to be used in place of the missing component.<A extends B,
B extends C>
OptionalUI<C> orGetUi
(Supplier<UIForAnything<?, A, B>> supplier) If a component is present, returns anOptionalUI
describing the component, otherwise returns aOptionalUI
containing the component built by the UI declaration inside the supplying function.<A extends B,
B extends C>
OptionalUI<C> orGetUiIf
(boolean condition, Supplier<UIForAnything<?, A, B>> supplier) If no component is present and the supplied boolean is true, the supplying function is called to provide an alternative UI declaration to be used to build the missing component.@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.An alternative tomap(Function)
that maps to the same type in yet anotherOptionalUI
instance.An alternative toupdate(Function)
andmap(Function)
that maps to the same type in yet anotherOptionalUI
instance but with the difference that the mapping function is only applied if the component is present and the supplied boolean is true.<U extends C>
OptionalUI<C> An alternative toupdate(Function)
andmap(Function)
that maps to the same type in yet anotherOptionalUI
instance but with the difference that the mapping function is only applied if the component is present and assignable to the given type.
-
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
-
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
.
-
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
-
update
An alternative tomap(Function)
that maps to the same type in yet anotherOptionalUI
instance. This is useful for chaining UI centric operations. The mapping function should return anOptionalUI
instance.- Parameters:
mapper
- The mapping function to apply to a component, if present.- Returns:
- an
OptionalUI
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
-
updateIf
An alternative toupdate(Function)
andmap(Function)
that maps to the same type in yet anotherOptionalUI
instance but with the difference that the mapping function is only applied if the component is present and assignable to the given type.
It is a type conditional mapping operation.- Type Parameters:
U
- The type of the component returned from the mapping function.- Parameters:
type
- The type to check if the component is assignable to.mapper
- The mapping function to apply to a component of the given type, if present.- Returns:
- An
OptionalUI
describing the result of applying a mapping function to the UI component of thisOptionalUI
, if a component is present and the component is assignable to the given type, otherwise an emptyOptionalUI
. - Throws:
NullPointerException
- if the mapping function isnull
NullPointerException
- if the given type isnull
-
updateIf
An alternative toupdate(Function)
andmap(Function)
that maps to the same type in yet anotherOptionalUI
instance but with the difference that the mapping function is only applied if the component is present and the supplied boolean is true.
It is a conditional mapping operation.- Parameters:
condition
- The boolean to check before applying the mapping function.mapper
- The mapping function to apply to a component, if present and the condition is true.- Returns:
- An
OptionalUI
describing the result of applying a mapping function to the UI component of thisOptionalUI
, if a component is present and the condition is true, 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 alternative UI components.- 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
-
orGet
If no component is present, the supplying function is called to provide an alternative UI component to be used in place of the missing component. Otherwise, returns aOptionalUI
containing the current component and the supplying function is not called. Use this to define alternative UI components.- Parameters:
supplier
- the supplying function that produces a UI component to be used if no component is present.- 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
-
orGetIf
If no component is present and the supplied boolean is true, the supplying function is called to provide an alternative UI component to be used in place of the missing component. Otherwise, returns aOptionalUI
containing the current component and the supplying function is not called. Use this to define alternative UI components if a condition is met.- Parameters:
condition
- The boolean condition to check before calling the supplying function. If false, the supplying function is simply ignored.supplier
- the supplying function that produces a UI component to be used if no component is present.- 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
-
orGetUi
If a component is present, returns anOptionalUI
describing the component, otherwise returns aOptionalUI
containing the component built by the UI declaration inside the supplying function. Use this to provide alternative UI components.- Type Parameters:
A
- The type of the component to be built.B
- The base type of the component to be built.- Parameters:
supplier
- the supplying function that produces a UI declaration to be used if no component is present.- 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
-
orGetUiIf
public <A extends B,B extends C> OptionalUI<C> orGetUiIf(boolean condition, Supplier<UIForAnything<?, A, B>> supplier) If no component is present and the supplied boolean is true, the supplying function is called to provide an alternative UI declaration to be used to build the missing component. Otherwise, returns the currentOptionalUI
and the supplying function is not called. Use this to define alternative UI declaration if a condition is met.- Type Parameters:
A
- The type of the component to be built.B
- The base type of the component to be built.- Parameters:
condition
- The boolean condition to check before calling the supplying function. If false, the supplying function is simply ignored.supplier
- the supplying function that produces a UI declaration to be used if no component is present.- 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
-
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
-
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 present
OptionalUI
s must be unambiguously differentiable.
-