Interface Mutable<T>

Type Parameters:
T - the type of the value contained in the Mutable
All Superinterfaces:
Serializable

public interface Mutable<T> extends Serializable
The Mutable interface represents an object that holds a value of type T, which can be modified after it is created. It provides a mechanism to wrap values in a mutable container with methods to retrieve, modify, and monitor changes to the value.

This interface supports several ways to create mutable containers:

In addition, it provides various methods to interact with the contained value, including checking if the value is present, setting the value, and executing actions when the value changes.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    The Subscription interface represents a way to unsubscribe from change notifications on a Mutable object.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Mutable<T>
    Creates an empty Mutable container with a null value.
    get()
    Retrieves the current value held by the Mutable.
    void
    Executes the given consumer if the value is present (i.e., not null).
    boolean
    Checks if the value held by the Mutable is null.
    boolean
    Checks if the Mutable contains a non-null value.
    static <T> Mutable<T>
    of(@NotNull Optional<T> optionalObject)
    Creates a Mutable container from an Optional value.
    static <T> Mutable<T>
    of(T object)
    Creates a Mutable container that holds the given object.
    Registers a BiConsumer to monitor changes to the value.
    default T
    or(T other)
    Returns the current value if it is present; otherwise, returns the provided fallback value.
    static <T, R> Mutable<T>
    referable(T object, @NotNull Class<R> referenceType, @NotNull Function<R,@NotNull T> map, @NotNull Function<T,@NotNull R> reverse)
    Creates a Mutable container that is "referable", meaning that it can be mapped from one type to another and vice versa using the provided mapping functions.
    void
    set(T object)
    Sets a new value for the Mutable.
  • Method Details

    • of

      static <T> Mutable<T> of(T object)
      Creates a Mutable container that holds the given object.
      Type Parameters:
      T - the type of the object
      Parameters:
      object - the object to be wrapped in the Mutable
      Returns:
      a new Mutable containing the given object
    • of

      static <T> Mutable<T> of(@NotNull @NotNull Optional<T> optionalObject)
      Creates a Mutable container from an Optional value.
      Type Parameters:
      T - the type of the object inside the Optional
      Parameters:
      optionalObject - the Optional to wrap
      Returns:
      a new Mutable containing the value inside the Optional, or null if empty
    • referable

      static <T, R> Mutable<T> referable(T object, @NotNull @NotNull Class<R> referenceType, @NotNull @NotNull Function<R,@NotNull T> map, @NotNull @NotNull Function<T,@NotNull R> reverse)
      Creates a Mutable container that is "referable", meaning that it can be mapped from one type to another and vice versa using the provided mapping functions.
      Type Parameters:
      T - the type of the object to wrap
      R - the type of the reference type used for mapping
      Parameters:
      object - the object to wrap in the Mutable
      referenceType - the class type of the reference object
      map - a function to map the reference type to the actual object
      reverse - a function to map the object back to the reference type
      Returns:
      a new Mutable with mapping functions applied
    • empty

      static <T> Mutable<T> empty()
      Creates an empty Mutable container with a null value.
      Type Parameters:
      T - the type of the value (which will be null)
      Returns:
      a new empty Mutable
    • get

      T get()
      Retrieves the current value held by the Mutable.
      Returns:
      the current value
    • set

      void set(T object)
      Sets a new value for the Mutable.
      Parameters:
      object - the new value to set
    • isEmpty

      boolean isEmpty()
      Checks if the value held by the Mutable is null.
      Returns:
      true if the value is null, otherwise false
    • isPresent

      boolean isPresent()
      Checks if the Mutable contains a non-null value.
      Returns:
      true if the value is non-null, otherwise false
    • ifPresent

      void ifPresent(@NotNull @NotNull Consumer<T> object)
      Executes the given consumer if the value is present (i.e., not null).
      Parameters:
      object - the consumer to execute with the current value
    • onChange

      Registers a BiConsumer to monitor changes to the value. The consumer will be called when the value is updated, providing the old value and the new value.
      Parameters:
      onChange - the BiConsumer to call when the value changes
      Returns:
      a Mutable.Subscription that can be used to unsubscribe from change notifications
    • or

      @NotNull default T or(@NotNull T other)
      Returns the current value if it is present; otherwise, returns the provided fallback value.
      Parameters:
      other - the value to return if the current value is not present
      Returns:
      the current value if present, otherwise the fallback value