Class NullableReference<V>
- Type Parameters:
V- The type of value held by this reference.
- All Implemented Interfaces:
Serializable
AtomicReference with Optional-like functionality.
This class extends AtomicReference to provide both atomic reference operations and Optional-like
convenience methods for working with potentially null values. It is thread-safe and can be used in
concurrent environments where you need to atomically update a reference while also having Optional-like
operations.
Features:
- Extends AtomicReference - inherits all atomic operations (get, set, compareAndSet, etc.)
- Optional-like API - provides isPresent(), isEmpty(), map(), orElse(), etc.
- Thread-safe - all operations are atomic
- Null-safe - handles null values gracefully (null represents "empty")
Usage:
Thread Safety:
This class is thread-safe. All operations inherit thread-safety from AtomicReference.
Multiple threads can safely call methods like AtomicReference.get(), AtomicReference.set(Object), AtomicReference.compareAndSet(Object, Object),
and the Optional-like methods concurrently.
See Also:
AtomicReference- Base class providing atomic operationsOptional- Java's Optional classNullableSupplier- Similar functionality for suppliers
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor (initializes tonull ).NullableReference(V initialValue) Constructor with initial value. -
Method Summary
Modifier and TypeMethodDescriptionstatic <V> NullableReference<V>empty()Creates an empty NullableReference with an initial value ofnull .If a value is present, and the value matches the given predicate, returns an NullableReference describing the value, otherwise returns an empty NullableReference.<U> NullableReference<U>flatMap(Function<? super V, ? extends NullableReference<? extends U>> mapper) If a value is present, returns the result of applying the given NullableReference-bearing mapping function to the value, otherwise returns an empty NullableReference.voidIf a value is present, performs the given action with the value, otherwise does nothing.voidifPresentOrElse(Consumer<? super V> action, Runnable emptyAction) If a value is present, performs the given action with the value, otherwise performs the given empty-based action.booleanisEmpty()Returnstrue if the current value isnull .booleanReturnstrue if the current value is notnull .<U> NullableReference<U>If a value is present, applies the provided mapping function to it and returns an NullableReference describing the result.static <V> NullableReference<V>of(V initialValue) Creates an NullableReference with the specified initial value.static <V> NullableReference<V>ofNullable(V initialValue) Creates an NullableReference with the specified initial value (alias forof(Object)).If a value is present, returns the value, otherwise returnsother .If a value is present, returns the value, otherwise returns the result produced by the supplying function.orElseThrow(Supplier<? extends X> exceptionSupplier) If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.Converts this NullableReference to anOptional.Methods inherited from class java.util.concurrent.atomic.AtomicReference
accumulateAndGet, compareAndExchange, compareAndExchangeAcquire, compareAndExchangeRelease, compareAndSet, get, getAcquire, getAndAccumulate, getAndSet, getAndUpdate, getOpaque, getPlain, lazySet, set, setOpaque, setPlain, setRelease, toString, updateAndGet, weakCompareAndSet, weakCompareAndSetAcquire, weakCompareAndSetPlain, weakCompareAndSetRelease, weakCompareAndSetVolatile
-
Constructor Details
-
NullableReference
Constructor with initial value.- Parameters:
initialValue- The initial value. Can benull .
-
NullableReference
public NullableReference()Default constructor (initializes tonull ).
-
-
Method Details
-
empty
Creates an empty NullableReference with an initial value ofnull .- Type Parameters:
V- The value type.- Returns:
- A new empty NullableReference.
-
of
Creates an NullableReference with the specified initial value.- Type Parameters:
V- The value type.- Parameters:
initialValue- The initial value. Can benull .- Returns:
- A new NullableReference with the specified value.
-
ofNullable
Creates an NullableReference with the specified initial value (alias forof(Object)).- Type Parameters:
V- The value type.- Parameters:
initialValue- The initial value. Can benull .- Returns:
- A new NullableReference with the specified value.
-
isPresent
Returnstrue if the current value is notnull .- Returns:
true if the current value is notnull .
-
isEmpty
Returnstrue if the current value isnull .- Returns:
true if the current value isnull .
-
map
If a value is present, applies the provided mapping function to it and returns an NullableReference describing the result.- Type Parameters:
U- The type of the result of the mapping function.- Parameters:
mapper- A mapping function to apply to the value, if present. Must not benull .- Returns:
- An NullableReference describing the result of applying a mapping function to the value, if a value is present, otherwise an empty NullableReference.
-
flatMap
public <U> NullableReference<U> flatMap(Function<? super V, ? extends NullableReference<? extends U>> mapper) If a value is present, returns the result of applying the given NullableReference-bearing mapping function to the value, otherwise returns an empty NullableReference.- Type Parameters:
U- The type parameter to the NullableReference returned by the mapping function.- Parameters:
mapper- A mapping function to apply to the value, if present. Must not benull .- Returns:
- The result of applying an NullableReference-bearing mapping function to the value, if a value is present, otherwise an empty NullableReference.
-
filter
If a value is present, and the value matches the given predicate, returns an NullableReference describing the value, otherwise returns an empty NullableReference.- Parameters:
predicate- A predicate to apply to the value, if present. Must not benull .- Returns:
- An NullableReference describing the value if a value is present and the value matches the given predicate, otherwise an empty NullableReference.
-
orElse
If a value is present, returns the value, otherwise returnsother .- Parameters:
other- The value to be returned if there is no value present. Can benull .- Returns:
- The value, if present, otherwise
other .
-
orElseGet
If a value is present, returns the value, otherwise returns the result produced by the supplying function.- Parameters:
other- ASupplierwhose result is returned if no value is present. Must not benull .- Returns:
- The value, if present, otherwise the result of
other.get() .
-
orElseThrow
If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.- Type Parameters:
X- Type of the exception to be thrown.- Parameters:
exceptionSupplier- The supplying function that produces an exception to be thrown. Must not benull .- Returns:
- The value, if present.
- Throws:
X- If no value is present.
-
ifPresent
If a value is present, performs the given action with the value, otherwise does nothing.- Parameters:
action- The action to be performed, if a value is present. Must not benull .
-
ifPresentOrElse
If a value is present, performs the given action with the value, otherwise performs the given empty-based action.- Parameters:
action- The action to be performed, if a value is present. Must not benull .emptyAction- The empty-based action to be performed, if no value is present. Must not benull .
-
toOptional
Converts this NullableReference to anOptional.- Returns:
- An Optional containing the value if present, otherwise an empty Optional.
-