Class AbstractValue<T>

java.lang.Object
is.codion.common.value.AbstractValue<T>
Type Parameters:
T - the value type
All Implemented Interfaces:
Observable<T>, Observer<T>, Value<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 AbstractValue instance should call notifyListeners() each time the value is set (Value.Notify.WHEN_SET) or only when it changes (Value.Notify.WHEN_CHANGED), which is determined using Object.equals(Object).

Implementations that want to handle notifications manually should use the AbstractValue() or AbstractValue(Object) constructors.

  • Constructor Details

    • AbstractValue

      protected AbstractValue()

      Creates a nullable AbstractValue instance, which does not notify listeners.

    • AbstractValue

      protected AbstractValue(Value.Notify notify)
      Creates a nullable AbstractValue instance.
      Parameters:
      notify - specifies when to notify listeners
    • AbstractValue

      protected AbstractValue(@Nullable T nullValue)

      Creates an AbstractValue instance, which does not notify listeners.

      If nullValue is non-null, this AbstractValue instance will be non-nullable, meaning isNullable() returns false, get() is guaranteed to never return null and when set(Object) receives null it is automatically translated to nullValue.

      Parameters:
      nullValue - the value to use instead of null
    • AbstractValue

      protected AbstractValue(@Nullable T nullValue, Value.Notify notify)

      Creates an AbstractValue instance.

      If nullValue is non-null, this AbstractValue instance will be non-nullable, meaning isNullable() returns false, get() is guaranteed to never return null and when set(Object) receives null it is automatically translated to nullValue.

      Parameters:
      nullValue - the value to use instead of null
      notify - specifies when to notify listeners
  • Method Details

    • get

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

      public final boolean set(@Nullable 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
    • observable

      public Observable<T> observable()
      Specified by:
      observable in interface Value<T>
      Returns:
      a read-only Observable instance for this value
    • observer

      public final Observer<T> observer()
      Specified by:
      observer in interface Observable<T>
      Returns:
      an Observer notified each time the observed value may have changed
    • isNullable

      public final boolean isNullable()
      Description copied from interface: Observable
      If false then get() is guaranteed to never return null.
      Specified by:
      isNullable in interface Observable<T>
      Returns:
      true if this observable can be null
    • 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(Observable<T> observable)
      Description copied from interface: Value
      Creates a unidirectional link between this value and the given observable, so that changes in the observable are reflected in this one. Note that after a call to this method the value of this value is the same as the observable.
      Specified by:
      link in interface Value<T>
      Parameters:
      observable - the observable to link this value to
    • unlink

      public final void unlink(Observable<T> observable)
      Description copied from interface: Value
      Unlinks this value from the given observable
      Specified by:
      unlink in interface Value<T>
      Parameters:
      observable - the observable 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(@Nullable 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 @Nullable T getValue()
      Returns the actual internal value.
      Returns:
      the value
    • setValue

      protected abstract void setValue(@Nullable 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
    • createObservable

      protected Observable<T> createObservable()
      Returns:
      a new Observable instance representing this value