Class AbstractValueSet<T>
- Type Parameters:
T- the value type
- All Implemented Interfaces:
Listeners.Superseding, Observable<Set<T>>, Observer<Set<T>>, Sensor, ObservableValueCollection<T,Set<T>>, Value<Set<T>>, ValueCollection<T, Set<T>>, ValueSet<T>, Iterable<T>
An abstract ValueSet implementation, the extension point for a set value with operations of its own:
the set 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 set keeps the order the items were added in.
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 set through the mutators, never
via setValue(Object), the storage the mutators write once they have validated, which itself neither
validates nor notifies.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractObserver
AbstractObserver.AbstractBuilder<T,B> Nested classes/interfaces inherited from interface Observer
Observer.Builder<T,B> Nested classes/interfaces inherited from interface Value
Value.Builder<T,B>, Value.BuilderFactory, Value.Locked, Value.Notify, Value.Validator<T> Nested classes/interfaces inherited from interface ValueCollection
ValueCollection.Builder<T,C, B> Nested classes/interfaces inherited from interface ValueSet
ValueSet.Builder<T> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreates an emptyAbstractValueSet, notifying listeners when it changes.protectedAbstractValueSet(Value.Notify notify) Creates an emptyAbstractValueSet. -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanAdds a value to thisValueCollectioninstance.final booleanaddAll(Collection<? extends T> values) Adds the given values to thisValueCollectioninstance.final booleanAdds the given values to thisValueCollectioninstance.final booleanaddValidator(Value.Validator<? super Set<T>> validator) Adds a validator to thisValue.final ChangeObserver<Set<T>> changed()The default implementation returns a newChangeObserverinstance on each call, but subclasses are free to return the same instance.final voidclear()Clears this value, by setting it to null or the null value in case this is a non-null value.final booleanReturns true if this collection contains the specified elementfinal booleancontainsAll(Collection<? extends T> values) Returns true if this collection contains all the elements of the specified collectionfinal booleancontainsNone(Collection<? extends T> values) Returns true if this collection contains none of the elements of the specified collectionfinal booleancontainsOnly(Collection<? extends T> values) 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 eitherprotected final ObservableValueSet<T> get()getValue()Returns the actual internal valuefinal booleanisEmpty()final booleanisNull()final booleanIf false then get() is guaranteed to never return null.iterator()final voidlink(Observable<Set<T>> observable) Creates a unidirectional link between this value and the given observable, so that changes in the observable are reflected in this one.final voidCreates a bidirectional link between this and the given original value, so that changes in one are reflected in the other.final Value.Lockedlocked()Locking a value prevents it from being changed, it does not prevent it from being set.protected final voidNotifies the underlying observer that the underlying value has changed or at least that it may have changed, in caseshouldNotify(Object)allows it.final ObservableValueSet<T> Returns anObservableValueCollectionnotified each time this value changes.optional()final booleanRemoves a single instance of the given value from thisValueCollectioninstance.final booleanremoveAll(Collection<? extends T> values) Removes the given values from thisValueCollectioninstance.final booleanRemoves the given values from thisValueCollectioninstance.final booleanremoveValidator(Value.Validator<? super Set<T>> validator) Removes the given validator from this valuefinal voidset(@Nullable Collection<? extends T> values) Sets the values.final voidValue.set(Object), typed to the set itself, which overload resolution prefers whenever a caller passes the declared type -valueSet.set(aSet)lands here rather than onValueCollection.set(Collection).protected final voidSets the actual internal valueprotected final booleanshouldNotify(Set<T> value) Always true, a collection value notifying each change, or each set, by its notify policy.final intsize()final voidsort(Comparator<? super T> comparator) 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.final voidunlink(Observable<Set<T>> observable) Unlinks this value from the given observablefinal voidUnlinks this value from the given original valuefinal voidupdate(UnaryOperator<Set<T>> updateFunction) Updates the value with the result of applying the given function to the current value.final voidValidates the given value using all validators, asValue.set(Object)does, the null value standing in for null in case of a non-null value.value()Returns aValueinstance based on thisValueCollection.Methods inherited from class AbstractObserver
addConsumer, addListener, addWeakConsumer, addWeakListener, notifyListeners, observer, removeConsumer, removeListener, removeWeakConsumer, removeWeakListener, sensor, when, whenMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Iterable
forEach, iterator, spliteratorMethods inherited from interface Observable
changed, getOrThrow, getOrThrow, is, isNot, isNullMethods inherited from interface ObservableValueCollection
contains, containsAll, containsNone, containsOnly, isEmpty, sizeMethods inherited from interface Observer
addConsumer, addWeakConsumer, observer, removeConsumer, removeWeakConsumer, sensor, when, whenMethods inherited from interface Sensor
addListener, addWeakListener, removeListener, removeWeakListener
-
Constructor Details
-
AbstractValueSet
protected AbstractValueSet()Creates an emptyAbstractValueSet, notifying listeners when it changes. -
AbstractValueSet
Creates an emptyAbstractValueSet.- Parameters:
notify- specifies when to notify listeners
-
-
Method Details
-
set
Value.set(Object), typed to the set itself, which overload resolution prefers whenever a caller passes the declared type -valueSet.set(aSet)lands here rather than onValueCollection.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
AbstractValueList, where it would erase to the same signature asValueCollection.set(Collection). -
observable
Description copied from interface:ValueCollectionReturns anObservableValueCollectionnotified each time this value changes.- Specified by:
observablein interfaceValue<T>- Specified by:
observablein interfaceValueCollection<T,Set<T>> - Specified by:
observablein interfaceValueSet<T>- Returns:
- a read-only
Observableinstance for this value
-
createObservable
- Returns:
- a new
Observableinstance representing this value
-
get
- Specified by:
getin interfaceObservable<T>- Specified by:
getin interfaceObservableValueCollection<T, C extends Collection<T>>- Specified by:
getin interfaceValueCollection<T, C extends Collection<T>>- Returns:
- the value
-
clear
-
update
Description copied from interface:ValueUpdates 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); -
iterator
-
optional
- Specified by:
optionalin interfaceObservable<T>- Specified by:
optionalin interfaceObservableValueCollection<T, C extends Collection<T>>- Specified by:
optionalin interfaceValueCollection<T, C extends Collection<T>>- Returns:
- an
Optionalwrapping this value.
-
set
Description copied from interface:ValueCollectionSets the values.- Specified by:
setin interfaceValueCollection<T, C extends Collection<T>>- Parameters:
values- the values to set
-
add
Description copied from interface:ValueCollectionAdds a value to thisValueCollectioninstance.- Specified by:
addin interfaceValueCollection<T, C extends Collection<T>>- Parameters:
value- the value to add- Returns:
- true if the value was added
- See Also:
-
addAll
Description copied from interface:ValueCollectionAdds the given values to thisValueCollectioninstance.- Specified by:
addAllin interfaceValueCollection<T, C extends Collection<T>>- Parameters:
values- the values to add- Returns:
- true if a value was added
- See Also:
-
addAll
Description copied from interface:ValueCollectionAdds the given values to thisValueCollectioninstance.- Specified by:
addAllin interfaceValueCollection<T, C extends Collection<T>>- Parameters:
values- the values to add- Returns:
- true if a value was added
- See Also:
-
remove
Description copied from interface:ValueCollectionRemoves a single instance of the given value from thisValueCollectioninstance.- Specified by:
removein interfaceValueCollection<T, C extends Collection<T>>- Parameters:
value- the value to remove- Returns:
- true if the value was removed
- See Also:
-
removeAll
Description copied from interface:ValueCollectionRemoves the given values from thisValueCollectioninstance.- Specified by:
removeAllin interfaceValueCollection<T, C extends Collection<T>>- Parameters:
values- the values to remove- Returns:
- true if a value was removed
- See Also:
-
removeAll
Description copied from interface:ValueCollectionRemoves the given values from thisValueCollectioninstance.- Specified by:
removeAllin interfaceValueCollection<T, C extends Collection<T>>- Parameters:
values- the values to remove- Returns:
- true if a value was removed
- See Also:
-
contains
Description copied from interface:ObservableValueCollectionReturns true if this collection contains the specified element- Specified by:
containsin interfaceObservableValueCollection<T, C extends Collection<T>>- Parameters:
value- the element- Returns:
- true if this collection contains the specified element
-
containsAll
Description copied from interface:ObservableValueCollectionReturns true if this collection contains all the elements of the specified collection- Specified by:
containsAllin interfaceObservableValueCollection<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
Description copied from interface:ObservableValueCollectionReturns 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:
containsOnlyin interfaceObservableValueCollection<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
Description copied from interface:ObservableValueCollectionReturns true if this collection contains none of the elements of the specified collection- Specified by:
containsNonein interfaceObservableValueCollection<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:
isEmptyin interfaceObservableValueCollection<T, C extends Collection<T>>- Returns:
- true if this collection is empty
-
size
public final int size()- Specified by:
sizein interfaceObservableValueCollection<T, C extends Collection<T>>- Returns:
- the number of elements in this collection
-
value
Description copied from interface:ValueCollectionReturns aValueinstance based on thisValueCollection.Note that setting this value causes the underlying
ValueCollectionto contain only that single value and setting this value to null clears theValueCollection.This value consistently returns the first value from the underlying
ValueCollectionin case it is sequenced and contains multiple items.- Specified by:
valuein interfaceValueCollection<T, C extends Collection<T>>- Returns:
- a single item value based on this
ValueCollectioninstance
-
isNull
public final boolean isNull()- Specified by:
isNullin interfaceObservable<T>- Returns:
- true if the underlying value is null.
-
sort
Description copied from interface:ValueCollectionSorts 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:
sortin interfaceValueCollection<T, C extends Collection<T>>- Parameters:
comparator- the comparator to use to sort
-
shouldNotify
-
getValue
-
setValue
-
changed
Description copied from interface:ObservableThe default implementation returns a newChangeObserverinstance 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 itsChangeObserver.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 theChange.previous()of the next change.- Specified by:
changedin interfaceObservable<T>- Returns:
- a
ChangeObserverinstance notified when this observable has changed
-
locked
Description copied from interface:ValueLocking 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:
lockedin interfaceValue<T>- Returns:
- the
Value.Lockedinstance controlling whether this value is locked
-
isNullable
public final boolean isNullable()Description copied from interface:ObservableIf false then get() is guaranteed to never return null.- Specified by:
isNullablein interfaceObservable<T>- Returns:
- true if this observable can be null
-
link
Description copied from interface:ValueCreates 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. AValueimplemented 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. -
unlink
-
link
Description copied from interface:ValueCreates 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. -
unlink
-
addValidator
Description copied from interface:ValueAdds a validator to thisValue. Adding the same validator again has no effect.- Specified by:
addValidatorin interfaceValue<T>- Parameters:
validator- the validator- Returns:
- true if this value did not already contain the specified validator
-
removeValidator
Description copied from interface:ValueRemoves the given validator from this value- Specified by:
removeValidatorin interfaceValue<T>- Parameters:
validator- the validator- Returns:
- true if this value contained the specified validator
-
validate
Description copied from interface:ValueValidates the given value using all validators, asValue.set(Object)does, the null value standing in for null in case of a non-null value. -
notifyObserver
protected final void notifyObserver()Notifies the underlying observer that the underlying value has changed or at least that it may have changed, in caseshouldNotify(Object)allows it.
-