Package ovh.mythmc.social.api.util
Interface Mutable<T>
- Type Parameters:
T- the type of the value contained in theMutable
- All Superinterfaces:
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:
of(Object)for wrapping an objectof(Optional)for wrapping anOptionalvaluereferable(Object, Class, Function, Function)for wrapping an object with reference mappingsempty()for creating an empty container
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 ClassesModifier and TypeInterfaceDescriptionstatic interfaceTheSubscriptioninterface represents a way to unsubscribe from change notifications on aMutableobject. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Mutable<T> empty()Creates an emptyMutablecontainer with anullvalue.get()Retrieves the current value held by theMutable.voidExecutes the given consumer if the value is present (i.e., notnull).booleanisEmpty()Checks if the value held by theMutableisnull.booleanChecks if theMutablecontains a non-null value.static <T> Mutable<T> Creates aMutablecontainer from anOptionalvalue.static <T> Mutable<T> of(T object) Creates aMutablecontainer that holds the given object.onChange(@NotNull BiConsumer<T, T> onChange) Registers aBiConsumerto monitor changes to the value.default TReturns 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 aMutablecontainer that is "referable", meaning that it can be mapped from one type to another and vice versa using the provided mapping functions.voidSets a new value for theMutable.
-
Method Details
-
of
Creates aMutablecontainer that holds the given object.- Type Parameters:
T- the type of the object- Parameters:
object- the object to be wrapped in theMutable- Returns:
- a new
Mutablecontaining the given object
-
of
Creates aMutablecontainer from anOptionalvalue. -
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 aMutablecontainer 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 wrapR- the type of the reference type used for mapping- Parameters:
object- the object to wrap in theMutablereferenceType- the class type of the reference objectmap- a function to map the reference type to the actual objectreverse- a function to map the object back to the reference type- Returns:
- a new
Mutablewith mapping functions applied
-
empty
Creates an emptyMutablecontainer with anullvalue.- Type Parameters:
T- the type of the value (which will benull)- Returns:
- a new empty
Mutable
-
get
T get()Retrieves the current value held by theMutable.- Returns:
- the current value
-
set
Sets a new value for theMutable.- Parameters:
object- the new value to set
-
isEmpty
boolean isEmpty()Checks if the value held by theMutableisnull.- Returns:
trueif the value isnull, otherwisefalse
-
isPresent
boolean isPresent()Checks if theMutablecontains a non-null value.- Returns:
trueif the value is non-null, otherwisefalse
-
ifPresent
Executes the given consumer if the value is present (i.e., notnull).- Parameters:
object- the consumer to execute with the current value
-
onChange
Registers aBiConsumerto 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- theBiConsumerto call when the value changes- Returns:
- a
Mutable.Subscriptionthat can be used to unsubscribe from change notifications
-
or
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
-