Package io.github.mmm.value.observable
Class AbstractObservableValue<V>
- java.lang.Object
-
- io.github.mmm.event.AbstractEventSource<ObservableEvent<V>,ObservableEventListener<? super V>>
-
- io.github.mmm.value.observable.AbstractObservableValue<V>
-
- Type Parameters:
V- type of the observablevalue.
- All Implemented Interfaces:
EventSource<ObservableEvent<V>,ObservableEventListener<? super V>>,ObservableValue<V>,ReadableValue<V>,Supplier<V>
- Direct Known Subclasses:
AbstractWritableObservableValue,Binding
public abstract class AbstractObservableValue<V> extends AbstractEventSource<ObservableEvent<V>,ObservableEventListener<? super V>> implements ObservableValue<V>
Abstract base implementation ofObservableValuethat handlesObservableEventListeners.- Since:
- 1.0.0
-
-
Constructor Summary
Constructors Constructor Description AbstractObservableValue()The constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListener(ObservableEventListener<? super V> listener, boolean weak)Adds anEventListenerwhich will be notified whenever the an event occurs (something changes).protected voidfireChange(Object change)Fires a defaultvalue changeevent.protected voidfireEvent()Fires a default value change event.protected voidfireEvent(ObservableEvent<V> event)protected voidfireEventFor(AbstractObservableValue<?> observable)protected <T> voidfireEventFor(AbstractObservableValue<T> observable, ObservableEvent<T> event)protected voidfireEventWithOldValue(V oldValue)Fires a default value change event if theold valueis known (currently available anyhow).protected booleanhasChangeAwareListeners()protected voidinvalidate()Invalidates this observable in case it's value is computed.booleanremoveListener(ObservableEventListener<? super V> listener)This method removes anEventListener.StringtoString()voidtoString(StringBuilder sb)-
Methods inherited from class io.github.mmm.event.AbstractEventSource
getEventAdapter, hasListeners
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.github.mmm.event.EventSource
addListener, addWeakListener
-
Methods inherited from interface io.github.mmm.value.ReadableValue
get, getSafe
-
-
-
-
Method Detail
-
addListener
public void addListener(ObservableEventListener<? super V> listener, boolean weak)
Description copied from interface:EventSourceAdds anEventListenerwhich will be notified whenever the an event occurs (something changes). If the same listener is added more than once, it will be notified more than once. The sameEventListenerinstance may be registered for differentEventSources.- Specified by:
addListenerin interfaceEventSource<ObservableEvent<V>,ObservableEventListener<? super V>>- Overrides:
addListenerin classAbstractEventSource<ObservableEvent<V>,ObservableEventListener<? super V>>- Parameters:
listener- theEventListenerto register.weak- -trueif theEventListenermay be garbage collected without beingremovedvia aWeakReference,falseotherwise (if the listener will be associated using a strong reference). When providingtruehere (useWeakReference), you need to store a reference to your registeredEventListeneryourself in the owning parent object so it is not garbage-collected too early.- See Also:
EventSource.addWeakListener(EventListener)
-
removeListener
public boolean removeListener(ObservableEventListener<? super V> listener)
Description copied from interface:EventSourceThis method removes anEventListener. If theEventListenerwas not registered before this method does not do any change. Otherwise the first matchingEventListenerwill be removed. So if youaddedanEventListenermultiple times, only the first occurrence will be removed.
For performance reasonsEventListener.isMatchedUsingEquals()returnsfalseby default. To force the usage ofequalsinstead, ensure yourEventListenerimplementation overridesObject.equals(Object)andEventListener.isMatchedUsingEquals()returningtrue.- Specified by:
removeListenerin interfaceEventSource<ObservableEvent<V>,ObservableEventListener<? super V>>- Overrides:
removeListenerin classAbstractEventSource<ObservableEvent<V>,ObservableEventListener<? super V>>- Parameters:
listener- is theEventListenerto unregister.- Returns:
trueif the givenlistenerhas successfully been removed,falseif thelistenerwas NOTregistered.
-
hasChangeAwareListeners
protected boolean hasChangeAwareListeners()
- Returns:
trueif at least oneChangeAwareObservableEventListenerisregistered,falseotherwise (to avoid building andsendingchanges for better performance).
-
fireEvent
protected void fireEvent()
Fires a default value change event.- See Also:
fireEvent(ObservableEvent)
-
fireEventWithOldValue
protected void fireEventWithOldValue(V oldValue)
Fires a default value change event if theold valueis known (currently available anyhow).- Parameters:
oldValue- theold value. May benull.- See Also:
fireEvent(ObservableEvent)
-
fireChange
protected void fireChange(Object change)
Fires a defaultvalue changeevent.- Parameters:
change- thechange.- See Also:
fireEvent(ObservableEvent)
-
fireEvent
protected void fireEvent(ObservableEvent<V> event)
- Overrides:
fireEventin classAbstractEventSource<ObservableEvent<V>,ObservableEventListener<? super V>>- Parameters:
event- the event tosendto allregisteredEventListeners.
-
fireEventFor
protected <T> void fireEventFor(AbstractObservableValue<T> observable, ObservableEvent<T> event)
- Type Parameters:
T- type of the observable value.- Parameters:
observable- theAbstractObservableValuewhere to invokefireEvent(ObservableEvent)(if prevented due to visibility issues).event- theObservableEventto send.
-
fireEventFor
protected void fireEventFor(AbstractObservableValue<?> observable)
- Parameters:
observable- theAbstractObservableValuewhere to invokefireEvent()(if prevented due to visibility issues).
-
invalidate
protected void invalidate()
Invalidates this observable in case it's value is computed.
-
toString
public void toString(StringBuilder sb)
- Specified by:
toStringin interfaceObservableValue<V>- Parameters:
sb- theStringBuilderwhere to append the details of this property fortoString()-Representation.
-
-