Class ObjectProperty<T>
java.lang.Object
com.thecsdev.common.properties.ObjectProperty<T>
- Type Parameters:
T- The type of the stored value.
- Direct Known Subclasses:
DefaultableProperty, NotNullProperty, StringProperty
A property wrapper for an
Inspired by JavaFX's property system.
Object of type T, similar to
AtomicReference, but with additional utility features.Inspired by JavaFX's property system.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanaddChangeListener(@NotNull IChangeListener<T> changeListener) Adds anIChangeListenerto the list of change listeners.final booleanAdds a filterFunctionto the list of filters.final TapplyFilters(T newValue) Applies filters to a given value.protected final voidassertCallerIsOwner(@NotNull Class<?> caller) Ensures that a givenClassis an owner usingisOwner(Class).get()Returns the current value of thisObjectProperty.final ValueHandle<T> Returns the internalAtomicReferencebacking this property.final @Nullable IChangeListener<T> getInterceptor(@NotNull Class<?> whoIsAsking) Returns the interceptor instance that interceptsset(Object)calls byClasses that do not own thisObjectProperty.Returns the current value of thisObjectPropertywrapped in anOptional.final Class<?> getOwner()Returns theClassthat owns thisObjectProperty.final booleanfinal booleanfinal booleanremoveChangeListener(Object changeListener) Removes anIChangeListenerfrom the list of change listeners.final booleanremoveFilter(Object filter) Removes a filterFunctionfrom the list of filters.final voidDeprecated.final voidSets the current value of thisObjectProperty.final voidsetHandle(@NotNull ValueHandle<T> newHandle) Replaces the internalAtomicReferencebacking this property.final voidsetInterceptor(@Nullable IChangeListener<T> interceptor, @NotNull Class<?> whoIsAsking) Assigns an interceptor instance for thisObjectProperty.
Please seegetInterceptor(Class)for more info.final voidAssigns a new ownerClassfor thisObjectProperty.final voidsetReadOnly(boolean readOnly, @NotNull Class<?> whoIsAsking) Sets thegetReadOnly()state.
-
Constructor Details
-
ObjectProperty
public ObjectProperty() -
ObjectProperty
-
-
Method Details
-
getOwner
Returns theClassthat owns thisObjectProperty.- API Note:
- This is likely the
Classthat created this instance.
-
setOwner
@CallerSensitive public final void setOwner(@Nullable @Nullable Class<?> owner, @NotNull @NotNull Class<?> whoIsAsking) throws NullPointerException, IllegalCallerException Assigns a new ownerClassfor thisObjectProperty.- Parameters:
owner- The new owner.whoIsAsking- TheClasscalling this method. It is CRITICAL not to lie here.- Throws:
NullPointerException- If aNotNullargument isnull.IllegalCallerException- SeeassertCallerIsOwner(Class).
-
isOwner
Returnstrueif a givenClassis considered an owner of thisObjectProperty. Those are:getOwner()#getOwner().isAssignableFrom(caller)ObjectProperty.class.isAssignableFrom(caller)
Classwill returnfalse.- Throws:
NullPointerException- If the argument isnull.- API Note:
- As a tip, remember that extending a
Classthat has declaredObjectPropertys means that you too (the subclass) now owns said properties as well.
-
assertCallerIsOwner
protected final void assertCallerIsOwner(@NotNull @NotNull Class<?> caller) throws IllegalCallerException Ensures that a givenClassis an owner usingisOwner(Class). If said class is not an owner,IllegalCallerExceptionis thrown.- Throws:
IllegalCallerException
-
getHandle
Returns the internalAtomicReferencebacking this property.This gives direct access to the underlying value, bypassing all access control such as
getOwner()andgetReadOnly().- Returns:
- The atomic reference holding the actual value
- API Note:
- Use with caution. This provides low-level access and can break property invariants if misused.
-
setHandle
@UnstableApi public final void setHandle(@NotNull @NotNull ValueHandle<T> newHandle) throws NullPointerException Replaces the internalAtomicReferencebacking this property.This completely overrides the current value holder, bypassing all checks including
getOwner()andgetReadOnly().- Parameters:
newHandle- The newValueHandleto use- Throws:
NullPointerException- ifhandleisnull- API Note:
- Use with caution. This provides low-level access and can break
property invariants if misused.
In addition,IChangeListeners are bound toValueHandles! (for now)
-
getReadOnly
public final boolean getReadOnly()- API Note:
- The owner of this
ObjectPropertybypassesreadOnlywhen callingset(Object).In addition, read-only prevents
getInterceptor(Class)from working.
-
setReadOnly
@CallerSensitive public final void setReadOnly(boolean readOnly, @NotNull @NotNull Class<?> whoIsAsking) throws IllegalCallerException Sets thegetReadOnly()state.- Parameters:
readOnly- The new readonly state.whoIsAsking- TheClasscalling this method. It is CRITICAL not to lie here.- Throws:
IllegalCallerException- SeeassertCallerIsOwner(Class).- API Note:
- Will prevent
getInterceptor(Class)from working if set totrue.
-
getInterceptor
@CallerSensitive @Nullable public final @Nullable IChangeListener<T> getInterceptor(@NotNull @NotNull Class<?> whoIsAsking) throws IllegalCallerException Returns the interceptor instance that interceptsset(Object)calls byClasses that do not own thisObjectProperty.- Parameters:
whoIsAsking- TheClasscalling this method. It is CRITICAL not to lie here.- Throws:
IllegalCallerException- SeeassertCallerIsOwner(Class).
-
setInterceptor
@CallerSensitive public final void setInterceptor(@Nullable @Nullable IChangeListener<T> interceptor, @NotNull @NotNull Class<?> whoIsAsking) throws IllegalCallerException Assigns an interceptor instance for thisObjectProperty.
Please seegetInterceptor(Class)for more info.- Parameters:
interceptor- The new interceptor instance.whoIsAsking- TheClasscalling this method. It is CRITICAL not to lie here.- Throws:
IllegalCallerException- SeeassertCallerIsOwner(Class).
-
get
Returns the current value of thisObjectProperty.- API Note:
Overrides should use the value ingetHandle().
-
getOptional
Returns the current value of thisObjectPropertywrapped in anOptional. -
set
Deprecated.Because this uses stack walking to enforceCallerSensitive, this method causes performance bottlenecks. Avoid this one.Sets the current value of thisObjectProperty.- Parameters:
value- The new value.- Throws:
IllegalCallerException- IfgetReadOnly(), and the caller is not an owner. SeeassertCallerIsOwner(Class).- See Also:
-
set
@CallerSensitive public final void set(@Nullable T value, @NotNull @NotNull Class<?> whoIsAsking) throws IllegalCallerException Sets the current value of thisObjectProperty.- Parameters:
value- The new value.whoIsAsking- TheClasscalling this method. It is CRITICAL not to lie here.- Throws:
IllegalCallerException- IfgetReadOnly(), and the caller is not an owner. SeeassertCallerIsOwner(Class).
-
addFilter
@CallerSensitive public final boolean addFilter(Function<T, T> filter, @NotNull @NotNull Class<?> whoIsAsking) throws IllegalCallerExceptionAdds a filterFunctionto the list of filters.- Parameters:
filter- The filter to add.whoIsAsking- TheClasscalling this method. It is CRITICAL not to lie here.- Throws:
IllegalCallerException- SeeassertCallerIsOwner(Class).
-
removeFilter
-
applyFilters
Applies filters to a given value. This is generally used inset(Object, Class)- See Also:
-
addChangeListener
public final boolean addChangeListener(@NotNull @NotNull IChangeListener<T> changeListener) throws NullPointerException Adds anIChangeListenerto the list of change listeners.- Parameters:
changeListener- TheIChangeListenerto add.- Throws:
NullPointerException- If the argument isnull.- API Note:
- IMPORTANT:
IChangeListeners are bound toValueHandles! UsingsetHandle(ValueHandle)changes theIChangeListeners as well!
-
removeChangeListener
Removes anIChangeListenerfrom the list of change listeners.- Parameters:
changeListener- TheIChangeListenerto remove.- Returns:
- A boolean indicating if the
IChangeListenerwas present prior to its removal.
-
CallerSensitive, this method causes performance bottlenecks.