Class AbstractValueList<T>

java.lang.Object
is.codion.common.reactive.observer.AbstractObserver<List<T>>
is.codion.common.reactive.value.AbstractValueList<T>
Type Parameters:
T - the value type
All Implemented Interfaces:
Listeners.Superseding, Observable<List<T>>, Observer<List<T>>, Sensor, ObservableValueCollection<T,List<T>>, Value<List<T>>, ValueCollection<T,List<T>>, ValueList<T>, Iterable<T>

public abstract class AbstractValueList<T> extends AbstractObserver<List<T>> implements ValueList<T>

An abstract ValueList implementation, the extension point for a list value with operations of its own: the list is held here, so a subclass supplies nothing and adds only its own methods.

Every method is final, so a subclass can not put a step of its own in front of the ordering every value follows, see AbstractValue. The mutators are synchronized, each change made and notified while holding this value's monitor, and the value is an unmodifiable snapshot, replaced on each change, so reading it takes no lock. The list keeps the order the items were added in, duplicates included.

Validators, links and initial items are added in the subclass constructor, via Value.addValidator(Validator), Value.link(Value) and ValueCollection.addAll(Collection). A subclass changes the list through the mutators, never via setValue(Object), the storage the mutators write once they have validated, which itself neither validates nor notifies.

See Also:
  • Constructor Details

    • AbstractValueList

      protected AbstractValueList()
      Creates an empty AbstractValueList, notifying listeners when it changes.
    • AbstractValueList

      protected AbstractValueList(Value.Notify notify)
      Creates an empty AbstractValueList.
      Parameters:
      notify - specifies when to notify listeners
  • Method Details

    • set

      public final void set(@Nullable List<T> values)

      Value.set(Object), typed to the list itself, which overload resolution prefers whenever a caller passes the declared type - valueList.set(aList) lands here rather than on ValueCollection.set(Collection).

      It must therefore take the same snapshot. Storing the caller's collection would leave the value's contents mutable from the outside and able to change behind its back, with no validation and no notification, and hand observers a snapshot that is not one.

      Declared here rather than on the base class shared with AbstractValueSet, where it would erase to the same signature as ValueCollection.set(Collection).

      Specified by:
      set in interface Value<T>
      Parameters:
      values - the value
      See Also:
    • observable

      public final ObservableValueList<T> observable()
      Description copied from interface: ValueCollection
      Returns an ObservableValueCollection notified each time this value changes.
      Specified by:
      observable in interface Value<T>
      Specified by:
      observable in interface ValueCollection<T,List<T>>
      Specified by:
      observable in interface ValueList<T>
      Returns:
      a read-only Observable instance for this value
    • createObservable

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

      public final List<T> get()
      Specified by:
      get in interface Observable<T>
      Specified by:
      get in interface ObservableValueCollection<T, C extends Collection<T>>
      Specified by:
      get in interface ValueCollection<T, C extends Collection<T>>
      Returns:
      the value
    • 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>
    • update

      public final void update(UnaryOperator<List<T>> updateFunction)
      Description copied from interface: Value
      Updates the value with the result of applying the given function to the current value.
      Value<Integer> value = Value.nonNull(0);
      
      // increment the value by one
      value.update(currentValue -> currentValue + 1);
      Specified by:
      update in interface Value<T>
      Parameters:
      updateFunction - a function mapping from the current value to the new value
    • iterator

      public final Iterator<T> iterator()
      Specified by:
      iterator in interface Iterable<T>
    • optional

      public final Optional<List<T>> optional()
      Specified by:
      optional in interface Observable<T>
      Specified by:
      optional in interface ObservableValueCollection<T, C extends Collection<T>>
      Specified by:
      optional in interface ValueCollection<T, C extends Collection<T>>
      Returns:
      an Optional wrapping this value.
    • set

      public final void set(@Nullable Collection<? extends T> values)
      Description copied from interface: ValueCollection
      Sets the values.
      Specified by:
      set in interface ValueCollection<T, C extends Collection<T>>
      Parameters:
      values - the values to set
    • add

      public final boolean add(T value)
      Description copied from interface: ValueCollection
      Adds a value to this ValueCollection instance.
      Specified by:
      add in interface ValueCollection<T, C extends Collection<T>>
      Parameters:
      value - the value to add
      Returns:
      true if the value was added
      See Also:
    • addAll

      @SafeVarargs public final boolean addAll(T... values)
      Description copied from interface: ValueCollection
      Adds the given values to this ValueCollection instance.
      Specified by:
      addAll in interface ValueCollection<T, C extends Collection<T>>
      Parameters:
      values - the values to add
      Returns:
      true if a value was added
      See Also:
    • addAll

      public final boolean addAll(Collection<? extends T> values)
      Description copied from interface: ValueCollection
      Adds the given values to this ValueCollection instance.
      Specified by:
      addAll in interface ValueCollection<T, C extends Collection<T>>
      Parameters:
      values - the values to add
      Returns:
      true if a value was added
      See Also:
    • remove

      public final boolean remove(T value)
      Description copied from interface: ValueCollection
      Removes a single instance of the given value from this ValueCollection instance.
      Specified by:
      remove in interface ValueCollection<T, C extends Collection<T>>
      Parameters:
      value - the value to remove
      Returns:
      true if the value was removed
      See Also:
    • removeAll

      @SafeVarargs public final boolean removeAll(T... values)
      Description copied from interface: ValueCollection
      Removes the given values from this ValueCollection instance.
      Specified by:
      removeAll in interface ValueCollection<T, C extends Collection<T>>
      Parameters:
      values - the values to remove
      Returns:
      true if a value was removed
      See Also:
    • removeAll

      public final boolean removeAll(Collection<? extends T> values)
      Description copied from interface: ValueCollection
      Removes the given values from this ValueCollection instance.
      Specified by:
      removeAll in interface ValueCollection<T, C extends Collection<T>>
      Parameters:
      values - the values to remove
      Returns:
      true if a value was removed
      See Also:
    • contains

      public final boolean contains(T value)
      Description copied from interface: ObservableValueCollection
      Returns true if this collection contains the specified element
      Specified by:
      contains in interface ObservableValueCollection<T, C extends Collection<T>>
      Parameters:
      value - the element
      Returns:
      true if this collection contains the specified element
    • containsAll

      public final boolean containsAll(Collection<? extends T> values)
      Description copied from interface: ObservableValueCollection
      Returns true if this collection contains all the elements of the specified collection
      Specified by:
      containsAll in interface ObservableValueCollection<T, C extends Collection<T>>
      Parameters:
      values - the elements to check
      Returns:
      true if this collection contains all the elements of the specified collection
    • containsOnly

      public final boolean containsOnly(Collection<? extends T> values)
      Description copied from interface: ObservableValueCollection
      Returns true if this collection contains all the elements of the specified collection and no others, regardless of their order and of how many times an element occurs in either
      Specified by:
      containsOnly in interface ObservableValueCollection<T, C extends Collection<T>>
      Parameters:
      values - the elements to check
      Returns:
      true if this collection contains all the elements of the specified collection and no others
    • containsNone

      public final boolean containsNone(Collection<? extends T> values)
      Description copied from interface: ObservableValueCollection
      Returns true if this collection contains none of the elements of the specified collection
      Specified by:
      containsNone in interface ObservableValueCollection<T, C extends Collection<T>>
      Parameters:
      values - the elements to check
      Returns:
      true if this collection contains none of the elements of the specified collection
    • isEmpty

      public final boolean isEmpty()
      Specified by:
      isEmpty in interface ObservableValueCollection<T, C extends Collection<T>>
      Returns:
      true if this collection is empty
    • size

      public final int size()
      Specified by:
      size in interface ObservableValueCollection<T, C extends Collection<T>>
      Returns:
      the number of elements in this collection
    • value

      public final Value<T> value()
      Description copied from interface: ValueCollection
      Returns a Value instance based on this ValueCollection.

      Note that setting this value causes the underlying ValueCollection to contain only that single value and setting this value to null clears the ValueCollection.

      This value consistently returns the first value from the underlying ValueCollection in case it is sequenced and contains multiple items.

      Specified by:
      value in interface ValueCollection<T, C extends Collection<T>>
      Returns:
      a single item value based on this ValueCollection instance
    • isNull

      public final boolean isNull()
      Specified by:
      isNull in interface Observable<T>
      Returns:
      true if the underlying value is null.
    • sort

      public final void sort(Comparator<? super T> comparator)
      Description copied from interface: ValueCollection

      Sorts the items according to the given comparator, notifying observers if the order changed, or in any case, once, for a value notifying each time it is set.

      Note that the underlying collection may not preserve the item order. Refer to the implementation javadocs for details.

      Specified by:
      sort in interface ValueCollection<T, C extends Collection<T>>
      Parameters:
      comparator - the comparator to use to sort
    • shouldNotify

      protected final boolean shouldNotify(List<T> value)
      Always true, a collection value notifying each change, or each set, by its notify policy.
      Parameters:
      value - the value the consumers are to receive
      Returns:
      true
    • getValue

      protected final List<T> getValue()
      Returns the actual internal value
      Returns:
      the value
    • setValue

      protected final void setValue(List<T> value)
      Sets the actual internal value
      Parameters:
      value - the value
    • changed

      public final ChangeObserver<List<T>> changed()
      Description copied from interface: Observable
      The default implementation returns a new ChangeObserver instance on each call, but subclasses are free to return the same instance.

      Note that when a new instance is returned per call, a listener added through one changed() instance can only be removed through that same instance; retain it if the listener must later be removed.

      The instance subscribes to this observable only while it, its ChangeObserver.previous() or its ChangeObserver.current() has listeners or consumers, so one never listened to costs nothing, and one whose listeners and consumers are all removed is left to be collected. One whose weak listeners and consumers have all been collected unsubscribes at the next change.

      The observer is notified when this observable notifies a value differing from the one before, by Objects.deepEquals(Object, Object). An equal value, a refreshed instance for example, isn't reported, but replaces the one before as the Change.previous() of the next change.

      Specified by:
      changed in interface Observable<T>
      Returns:
      a ChangeObserver instance notified when this observable has changed
    • locked

      public final Value.Locked locked()
      Description copied from interface: Value

      Locking a value prevents it from being changed, it does not prevent it from being set.

      Note that locking a value does not prevent linked values from being changed.

      A locked value in a linked value chain, may cause it to go out of sync, since any subsequent linked values will not be updated after a locked value is encountered.

      Specified by:
      locked in interface Value<T>
      Returns:
      the Value.Locked instance controlling whether this value is locked
    • 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<List<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.

      The two behave as one logical value: either end validates against both ends' validators, before either is written, so the pair can not come to hold a value one end rejects. This holds for any Value, Value.validate(Object) being the gate.

      Each end updates the other from a listener, notified in turn with the others, so the pair can diverge where that update does not happen: a listener added before it throwing, which ends the notification, or the other end locked. A non-null value linked to a nullable one holding null holds its null value in its place.

      Cycle detection reaches as far as the framework's own link bookkeeping, which means every value extending AbstractValue. A Value implemented from scratch keeps its own links, if it has any, so a cycle running through one is not detected - the reciprocal update guard still keeps a single link from cycling back on itself.

      Specified by:
      link in interface Value<T>
      Parameters:
      originalValue - the original value to link this value to
    • unlink

      public final void unlink(Value<List<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<List<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<List<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 List<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 List<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(List<T> value)
      Description copied from interface: Value
      Validates the given value using all validators, as Value.set(Object) does, the null value standing in for null in case of a non-null value.
      Specified by:
      validate in interface Value<T>
      Parameters:
      value - the value to validate
    • notifyObserver

      protected final void notifyObserver()
      Notifies the underlying observer that the underlying value has changed or at least that it may have changed, in case shouldNotify(Object) allows it.