Class Immutable<T>

java.lang.Object
com.github.thought2code.mcp.annotated.util.Immutable<T>
Type Parameters:
T - the type of the wrapped value

public final class Immutable<T> extends Object
A wrapper class that provides immutable access to a value through a supplier.

This class encapsulates a value and provides thread-safe, immutable access to it through a Supplier. Once created, the value cannot be modified, ensuring immutability and thread safety in concurrent environments.

The class is generic and can wrap any type T. The value is stored internally as a supplier that always returns the same value, providing a consistent interface for value access.

Use cases include:

  • Ensuring thread-safe access to shared values
  • Preventing modification of critical data
  • Providing a consistent supplier interface for value access
Author:
codeboyzhou
See Also:
  • Method Details

    • of

      @NotNull public static <T> @NotNull Immutable<T> of(T value)
      Creates a new Immutable instance wrapping the specified value.

      This static factory method creates an immutable wrapper around the provided value. The wrapped value cannot be modified after creation, ensuring thread-safe access.

      The method is annotated with @NotNull to indicate that it never returns null.

      Type Parameters:
      T - the type of the value to wrap
      Parameters:
      value - the value to wrap in an immutable container
      Returns:
      a new Immutable instance containing the specified value
      See Also:
      • Immutable(Object)
    • get

      public T get()
      Returns the wrapped value.

      This method retrieves the value stored in this immutable container. The value is returned through the internal supplier, ensuring consistent access semantics.

      The returned value is the same object that was provided to the of(Object) factory method.

      Returns:
      the wrapped value
      See Also: