Package jasima.core.util.observer
Class ObservableValue<VALUE>
- java.lang.Object
-
- jasima.core.util.observer.ObservableValue<VALUE>
-
- Type Parameters:
VALUE- The type of the value stored.
- Direct Known Subclasses:
ConstValue,DerivedObservable
public class ObservableValue<VALUE> extends Object
This class provides functionality to store a value and allows listeners to get notified whenever this value is changing. Together with its subclassDerivedObservableit allows a simple way to create reactive expressions.Instances of this class can be seen s a kind of variables that can be used in more complex expressions provided by
DerivedObservableor used directly to e.g. collect statistics or update GUI elements.- Author:
- Torsten Hildebrandt
- See Also:
DerivedObservable,ObservableValues
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classObservableValue.EventTypestatic interfaceObservableValue.ObservableListener<V>
-
Constructor Summary
Constructors Constructor Description ObservableValue()Creates new instance with its value being initialized withnull.ObservableValue(VALUE initialValue)Creates new instance with a certain initial value.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ObservableValue.ObservableListener<VALUE>addListener(ObservableValue.ObservableListener<VALUE> l)Adds a new listener to be notified whenever the stored value is changing.ObservableValue.ObservableListener<VALUE>addWeakListener(ObservableValue.ObservableListener<VALUE> l)Adds a new listener to be notified whenever the stored value is changing.Set<ObservableValue<?>>dependencySet()Returns the set of immediate dependencies, i.e., all values this observable value depends on.booleanequals(Object obj)Returnstrueif the value currently stored in this object is either equal toobjor ifobjis anotherObservableValuewith the stored values being equals.protected voidfireEvent(ObservableValue.EventType event)Broadcast an event to all listeners.VALUEget()Returns the currently stored value.VALUEgetLastValue()Returns the last value stored.protected voidinternalSet(VALUE newValue)booleanisStale()Returnstrueif this the currently stored value needs updating.The implementation here always returnsfalse.intnumListener()Returns the number of listeners currently registered for this observable value.booleanremoveListener(ObservableValue.ObservableListener<VALUE> l)Removes a listener that was previously added using#addListener(BiConsumer)oraddWeakListener(ObservableListener).voidset(VALUE newValue)Sets a new value.ObservableValue<VALUE>update(Function<VALUE,VALUE> updateFunc)Updates the current value of this observable by calling the function passed as a parameter.longversionId()Provides a version id.voidwhenEquals(VALUE v, Runnable action)
-
-
-
Constructor Detail
-
ObservableValue
public ObservableValue()
Creates new instance with its value being initialized withnull.
-
ObservableValue
public ObservableValue(VALUE initialValue)
Creates new instance with a certain initial value.- Parameters:
initialValue- The initial value of this observable value.
-
-
Method Detail
-
get
public VALUE get()
Returns the currently stored value.
-
getLastValue
public VALUE getLastValue()
Returns the last value stored. This method returns null if only the initial value was set.
-
set
public void set(VALUE newValue)
Sets a new value. If the new values differs from the old one (checked byObjects.equals(Object, Object)), then all registered listeners are notified about changes.- Parameters:
newValue- The new value to store. Can benull.
-
update
public ObservableValue<VALUE> update(Function<VALUE,VALUE> updateFunc)
Updates the current value of this observable by calling the function passed as a parameter. The current value is passed as a parameter, the value returned is used as the new value.- Parameters:
updateFunc- The update function to use.- Returns:
- the current observable instance to allow chaining calls.
-
internalSet
protected void internalSet(VALUE newValue)
-
versionId
public long versionId()
Provides a version id. The version id is increasing whenever the stored value changes.
-
isStale
public boolean isStale()
Returnstrueif this the currently stored value needs updating.The implementation here always returnsfalse.
-
dependencySet
public Set<ObservableValue<?>> dependencySet()
Returns the set of immediate dependencies, i.e., all values this observable value depends on. The implementation here always returns an empty set.
-
numListener
public int numListener()
Returns the number of listeners currently registered for this observable value.
-
addListener
public ObservableValue.ObservableListener<VALUE> addListener(ObservableValue.ObservableListener<VALUE> l)
Adds a new listener to be notified whenever the stored value is changing. This method stores the listener via a strong reference.- Parameters:
l- The listener.- Returns:
- the listener (same as l).
-
addWeakListener
public ObservableValue.ObservableListener<VALUE> addWeakListener(ObservableValue.ObservableListener<VALUE> l)
Adds a new listener to be notified whenever the stored value is changing. This method stores the listener using a weak reference so it can be garbage collected automatically when there are no further references to it.- Parameters:
l- The listener.- Returns:
- the listener (same as l).
-
removeListener
public boolean removeListener(ObservableValue.ObservableListener<VALUE> l)
Removes a listener that was previously added using#addListener(BiConsumer)oraddWeakListener(ObservableListener).- Parameters:
l- The listener.- Returns:
trueif l was still registered as a listener and could be removed.
-
fireEvent
protected void fireEvent(ObservableValue.EventType event)
Broadcast an event to all listeners.- Parameters:
event- The event denoted by a string.
-
equals
public boolean equals(Object obj)
Returnstrueif the value currently stored in this object is either equal toobjor ifobjis anotherObservableValuewith the stored values being equals.Warning: be careful when storing
ObservableValues in Java Collections (especially Maps and Sets), as they might require a somewhat different contract of equals/hashCode.
-
-