Class AbstractValue<T>

java.lang.Object
is.codion.common.value.AbstractValue<T>
Type Parameters:
T - the value type
All Implemented Interfaces:
Observer<T>, Value<T>, ValueObserver<T>, Consumer<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 call notifyListeners() when the value is set or changed via set(Object). Implementations that want to handle notifications manually should use the AbstractValue() or AbstractValue(Object) constructors.

  • Constructor Details

    • AbstractValue

      protected AbstractValue()
      Creates a AbstractValue instance, which does not notify listeners.
    • AbstractValue

      protected AbstractValue(T nullValue)
      Creates a 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

    • get

      public final T get()
      Specified by:
      get in interface ValueObserver<T>
      Returns:
      the value
    • set

      public final boolean set(T value)
      Description copied from interface: Value
      Sets the value. Note that change listener notifications depend on the Value.Notify policy associated with this value.
      Specified by:
      set in interface Value<T>
      Parameters:
      value - the value
      Returns:
      true if the underlying value changed
      See Also:
    • clear

      public final void clear()
      Description copied from interface: Value
      Clears this value, by setting it to null or the null value in case this is a non-null value.
      Specified by:
      clear in interface Value<T>
    • map

      public final boolean map(Function<T,T> mapper)
      Description copied from interface: Value
      Sets a new value mapped from the current value.
       
       Value<Integer> value = Value.value(0);
      
       // increment the value by one
       value.map(currentValue -> currentValue + 1);
       
       
      Specified by:
      map in interface Value<T>
      Parameters:
      mapper - maps from the current value to a new value
      Returns:
      true if the underlying value changed
    • observer

      public 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. Note that change listener notifications depend on the Value.Notify policy associated with this 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: Observer
      Adds listener to this Observer. Adding the same listener a second time has no effect.
      Specified by:
      addListener in interface Observer<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: Observer
      Removes listener from this Observer
      Specified by:
      removeListener in interface Observer<T>
      Parameters:
      listener - the listener to remove
      Returns:
      true if this observer contained the specified listener
    • addConsumer

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

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

      public final boolean addWeakListener(Runnable listener)
      Description copied from interface: Observer
      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 Observer<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: Observer
      Removes listener from this Observer
      Specified by:
      removeWeakListener in interface Observer<T>
      Parameters:
      listener - the listener to remove
      Returns:
      true if this observer contained the specified listener
    • addWeakConsumer

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

      public final boolean removeWeakConsumer(Consumer<? super T> consumer)
      Description copied from interface: Observer
      Removes consumer from this Observer.
      Specified by:
      removeWeakConsumer in interface Observer<T>
      Parameters:
      consumer - the consumer to remove
      Returns:
      true if this observer contained the specified consumer
    • 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<? super 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<? super 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
    • getValue

      protected abstract T getValue()
      Returns the actual internal value.
      Returns:
      the value
    • 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
    • createObserver

      protected ValueObserver<T> createObserver()
      Returns:
      a new ValueObserver instance representing this value