Package org.apache.juneau
Class Value<T>
java.lang.Object
org.apache.juneau.Value<T>
- Type Parameters:
T
- The value type.
Represents a simple settable value.
Similar to an Optional
but mutable.
Notes:
- This class is not thread safe.
See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Value<T>
empty()
Static creator.get()
Returns the value.Returns the value and then unsets it.static Type
Returns the generic parameter type of the Value type.void
If a value is present, invoke the specified consumer with the value, otherwise do nothing.boolean
isEmpty()
Returnstrue if the value is empty.boolean
Returnstrue if the value is set.static boolean
Convenience method for checking if the specified type is this class.listener
(ValueListener<T> listener) Adds a listener for this value.<T2> Value<T2>
Applies a mapping function against the contents of this value.static <T> Value<T>
of
(T object) Static creator.Returns the contents of this value or the default value ifnull .Return the value if present, otherwise invokeother
and return the result of that invocation.orElseThrow
(Supplier<? extends X> exceptionSupplier) Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.Sets the value.setIfEmpty
(T t) Sets the value if it's not already set.toString()
static Type
Returns the unwrapped type.
-
Constructor Details
-
Value
public Value()Constructor. -
Value
Constructor.- Parameters:
t
- Initial value.
-
-
Method Details
-
of
Static creator.- Type Parameters:
T
- The value type.- Parameters:
object
- The object being wrapped.- Returns:
- A new
Value
object.
-
empty
Static creator.- Type Parameters:
T
- The value type.- Returns:
- An empty
Value
object.
-
getParameterType
Returns the generic parameter type of the Value type.- Parameters:
t
- The type to find the parameter type of.- Returns:
- The parameter type of the value, or
null if the type is not a subclass ofValue .
-
unwrap
Returns the unwrapped type.- Parameters:
t
- The type to unwrap.- Returns:
- The unwrapped type, or the same type if the type isn't
Value
.
-
isType
Convenience method for checking if the specified type is this class.- Parameters:
t
- The type to check.- Returns:
true if the specified type is this class.
-
listener
Adds a listener for this value.- Parameters:
listener
- The new listener for this value.- Returns:
- This object.
-
set
Sets the value.- Parameters:
t
- The new value.- Returns:
- This object.
-
setIfEmpty
Sets the value if it's not already set.- Parameters:
t
- The new value.- Returns:
- This object.
-
get
Returns the value.- Returns:
- The value, or
null if it is not set.
-
getAndUnset
Returns the value and then unsets it.- Returns:
- The value before it was unset.
-
isPresent
Returnstrue if the value is set.- Returns:
true if the value is set.
-
ifPresent
If a value is present, invoke the specified consumer with the value, otherwise do nothing.- Parameters:
consumer
- Block to be executed if a value is present.
-
map
Applies a mapping function against the contents of this value.- Type Parameters:
T2
- The mapped value type.- Parameters:
mapper
- The mapping function.- Returns:
- The mapped value.
-
orElse
Returns the contents of this value or the default value ifnull .- Parameters:
def
- The default value.- Returns:
- The contents of this value or the default value if
null .
-
isEmpty
Returnstrue if the value is empty.- Returns:
true if the value is empty.
-
orElseGet
Return the value if present, otherwise invokeother
and return the result of that invocation.- Parameters:
other
- aSupplier
whose result is returned if no value is present- Returns:
- the value if present otherwise the result of
other.get()
- Throws:
NullPointerException
- if value is not present andother
is null
-
orElseThrow
Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.- Type Parameters:
X
- The exception type.- Parameters:
exceptionSupplier
- The supplier which will return the exception to be thrown- Returns:
- the present value
- Throws:
X
- if there is no value presentNullPointerException
- if no value is present andexceptionSupplier
is null
-
toString
-