Class Immutable<T>
- Type Parameters:
T- the type of the wrapped value
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 Summary
-
Method Details
-
of
Creates a newImmutableinstance 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
@NotNullto 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
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:
-