Class AbstractValue<T>

java.lang.Object
is.codion.common.value.AbstractValue<T>
Type Parameters:
T - the value type
All Implemented Interfaces:
EventObserver<T>, Value<T>, ValueObserver<T>, Consumer<T>, Supplier<T>
Direct Known Subclasses:
AbstractComponentValue

public abstract class AbstractValue<T> extends Object implements Value<T>
An abstract Value implementation handling everything except the value itself.

The constructor parameter notify specifies whether this Value instance should automatically call notifyListeners() when the value is set or changed via set(Object). Some implementations may want to handle this manually.
  • Constructor Details

    • AbstractValue

      protected AbstractValue()
    • AbstractValue

      protected AbstractValue(T nullValue)
      Creates an AbstractValue instance, which does not notify listeners.
      Parameters:
      nullValue - the value to use instead of null
    • AbstractValue

      protected AbstractValue(T nullValue, Value.Notify notify)
      Creates an AbstractValue instance.
      Parameters:
      nullValue - the value to use instead of null
      notify - specifies when to notify listeners
  • Method Details

    • set

      public final boolean set(T value)
      Description copied from interface: Value
      Sets the value. Note that if the value is equal to the current value according to Objects.equals(java.lang.Object, java.lang.Object) the underlying value is still set, but no change event is triggered.
      Specified by:
      set in interface Value<T>
      Parameters:
      value - the value
      Returns:
      true if the underlying value changed
      See Also:
    • observer

      public final ValueObserver<T> observer()
      Description copied from interface: Value
      Returns a ValueObserver notified each time this value changes.
      Specified by:
      observer in interface Value<T>
      Returns:
      a ValueObserver for this value
    • nullable

      public final boolean nullable()
      Description copied from interface: ValueObserver
      If false then get() is guaranteed to never return null.
      Specified by:
      nullable in interface ValueObserver<T>
      Returns:
      true if this value can be null
    • accept

      public final void accept(T data)
      Description copied from interface: Value
      Sets the value
      Specified by:
      accept in interface Consumer<T>
      Specified by:
      accept in interface Value<T>
      Parameters:
      data - the value
      See Also:
    • addListener

      public final boolean addListener(Runnable listener)
      Description copied from interface: EventObserver
      Adds listener to this EventObserver. Adding the same listener a second time has no effect.
      Specified by:
      addListener in interface EventObserver<T>
      Parameters:
      listener - the listener to add
      Returns:
      true if this observer did not already contain the specified listener
    • removeListener

      public final boolean removeListener(Runnable listener)
      Description copied from interface: EventObserver
      Removes listener from this EventObserver
      Specified by:
      removeListener in interface EventObserver<T>
      Parameters:
      listener - the listener to remove
      Returns:
      true if this observer contained the specified listener
    • addDataListener

      public final boolean addDataListener(Consumer<T> listener)
      Description copied from interface: EventObserver
      Adds listener to this EventObserver. Adding the same listener a second time has no effect.
      Specified by:
      addDataListener in interface EventObserver<T>
      Parameters:
      listener - the listener to add
      Returns:
      true if this observer did not already contain the specified listener
    • removeDataListener

      public final boolean removeDataListener(Consumer<T> listener)
      Description copied from interface: EventObserver
      Removes listener from this EventObserver
      Specified by:
      removeDataListener in interface EventObserver<T>
      Parameters:
      listener - the listener to remove
      Returns:
      true if this observer contained the specified listener
    • addWeakListener

      public final boolean addWeakListener(Runnable listener)
      Description copied from interface: EventObserver
      Uses a WeakReference, adding listener does not prevent it from being garbage collected. Adding the same listener a second time has no effect.
      Specified by:
      addWeakListener in interface EventObserver<T>
      Parameters:
      listener - the listener
      Returns:
      true if this observer did not already contain the specified listener
    • removeWeakListener

      public final boolean removeWeakListener(Runnable listener)
      Description copied from interface: EventObserver
      Removes listener from this EventObserver
      Specified by:
      removeWeakListener in interface EventObserver<T>
      Parameters:
      listener - the listener to remove
      Returns:
      true if this observer contained the specified listener
    • addWeakDataListener

      public final boolean addWeakDataListener(Consumer<T> listener)
      Description copied from interface: EventObserver
      Uses a WeakReference, adding listener does not prevent it from being garbage collected. Adding the same listener a second time has no effect.
      Specified by:
      addWeakDataListener in interface EventObserver<T>
      Parameters:
      listener - the listener
      Returns:
      true if this observer did not already contain the specified listener
    • removeWeakDataListener

      public final boolean removeWeakDataListener(Consumer<T> listener)
      Description copied from interface: EventObserver
      Removes listener from this EventObserver.
      Specified by:
      removeWeakDataListener in interface EventObserver<T>
      Parameters:
      listener - the listener to remove
      Returns:
      true if this observer contained the specified listener
    • link

      public final void link(Value<T> originalValue)
      Description copied from interface: Value
      Creates a bidirectional link between this and the given original value, so that changes in one are reflected in the other. Note that after a call to this method this value is the same as originalValue.
      Specified by:
      link in interface Value<T>
      Parameters:
      originalValue - the original value to link this value to
    • unlink

      public final void unlink(Value<T> originalValue)
      Description copied from interface: Value
      Unlinks this value from the given original value
      Specified by:
      unlink in interface Value<T>
      Parameters:
      originalValue - the original value to unlink from this one
    • link

      public final void link(ValueObserver<T> originalValue)
      Description copied from interface: Value
      Creates a unidirectional link between this value and the given original value observer, so that changes in the original value are reflected in this one. Note that after a call to this method the value of this value is the same as the original value.
      Specified by:
      link in interface Value<T>
      Parameters:
      originalValue - the original value to link this value to
    • unlink

      public final void unlink(ValueObserver<T> originalValue)
      Description copied from interface: Value
      Unlinks this value from the given original value observer
      Specified by:
      unlink in interface Value<T>
      Parameters:
      originalValue - the original value to unlink
    • addValidator

      public final boolean addValidator(Value.Validator<T> validator)
      Description copied from interface: Value
      Adds a validator to this Value. Adding the same validator again has no effect.
      Specified by:
      addValidator in interface Value<T>
      Parameters:
      validator - the validator
      Returns:
      true if this value did not already contain the specified validator
    • removeValidator

      public final boolean removeValidator(Value.Validator<T> validator)
      Description copied from interface: Value
      Removes the given validator from this value
      Specified by:
      removeValidator in interface Value<T>
      Parameters:
      validator - the validator
      Returns:
      true if this value contained the specified validator
    • validate

      public final void validate(T value)
      Description copied from interface: Value
      Validate the given value using all validators
      Specified by:
      validate in interface Value<T>
      Parameters:
      value - the value to validate
    • setValue

      protected abstract void setValue(T value)
      Sets the actual internal value.
      Parameters:
      value - the value
    • notifyListeners

      protected final void notifyListeners()
      Notifies listeners that the underlying value has changed or at least that it may have changed