Class NullableReference<V>

java.lang.Object
java.util.concurrent.atomic.AtomicReference<V>
org.apache.juneau.commons.concurrent.NullableReference<V>
Type Parameters:
V - The type of value held by this reference.
All Implemented Interfaces:
Serializable

public class NullableReference<V> extends AtomicReference<V>
A thread-safe reference that combines 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:

// Create an empty reference NullableReference<String> ref = NullableReference.empty(); // Set a value atomically ref.set("value"); // Check if value is present if (ref.isPresent()) { String value = ref.get(); } // Use Optional-like methods String result = ref.map(String::toUpperCase).orElse("default"); // Atomic update ref.compareAndSet("value", "newValue");

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:
See Also:
  • Constructor Details

    • NullableReference

      public NullableReference(V initialValue)
      Constructor with initial value.
      Parameters:
      initialValue - The initial value. Can be null.
    • NullableReference

      Default constructor (initializes to null).
  • Method Details

    • empty

      public static <V> NullableReference<V> empty()
      Creates an empty NullableReference with an initial value of null.
      Type Parameters:
      V - The value type.
      Returns:
      A new empty NullableReference.
    • of

      public static <V> NullableReference<V> of(V initialValue)
      Creates an NullableReference with the specified initial value.
      Type Parameters:
      V - The value type.
      Parameters:
      initialValue - The initial value. Can be null.
      Returns:
      A new NullableReference with the specified value.
    • ofNullable

      public static <V> NullableReference<V> ofNullable(V initialValue)
      Creates an NullableReference with the specified initial value (alias for of(Object)).
      Type Parameters:
      V - The value type.
      Parameters:
      initialValue - The initial value. Can be null.
      Returns:
      A new NullableReference with the specified value.
    • isPresent

      public boolean isPresent()
      Returns true if the current value is not null.
      Returns:
      true if the current value is not null.
    • isEmpty

      public boolean isEmpty()
      Returns true if the current value is null.
      Returns:
      true if the current value is null.
    • map

      public <U> NullableReference<U> map(Function<? super V,? extends U> mapper)
      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 be null.
      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 be null.
      Returns:
      The result of applying an NullableReference-bearing mapping function to the value, if a value is present, otherwise an empty NullableReference.
    • filter

      public NullableReference<V> filter(Predicate<? super V> predicate)
      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 be null.
      Returns:
      An NullableReference describing the value if a value is present and the value matches the given predicate, otherwise an empty NullableReference.
    • orElse

      public V orElse(V other)
      If a value is present, returns the value, otherwise returns other.
      Parameters:
      other - The value to be returned if there is no value present. Can be null.
      Returns:
      The value, if present, otherwise other.
    • orElseGet

      public V orElseGet(Supplier<? extends V> other)
      If a value is present, returns the value, otherwise returns the result produced by the supplying function.
      Parameters:
      other - A Supplier whose result is returned if no value is present. Must not be null.
      Returns:
      The value, if present, otherwise the result of other.get().
    • orElseThrow

      public <X extends Throwable> V orElseThrow(Supplier<? extends X> exceptionSupplier) throws X
      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 be null.
      Returns:
      The value, if present.
      Throws:
      X - If no value is present.
    • ifPresent

      public void ifPresent(Consumer<? super V> action)
      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 be null.
    • ifPresentOrElse

      public void ifPresentOrElse(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.
      Parameters:
      action - The action to be performed, if a value is present. Must not be null.
      emptyAction - The empty-based action to be performed, if no value is present. Must not be null.
    • toOptional

      public Optional<V> toOptional()
      Converts this NullableReference to an Optional.
      Returns:
      An Optional containing the value if present, otherwise an empty Optional.