Package net.sf.jstuff.core.collection
Class ObservableCollection<E,C extends Collection<E>>
java.lang.Object
net.sf.jstuff.core.collection.ObservableCollection<E,C>
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,CollectionExt<E>
,EventListenable<ObservableCollection.ChangeEvent<E>>
- Direct Known Subclasses:
ObservableList
,ObservableSet
public class ObservableCollection<E,C extends Collection<E>>
extends Object
implements CollectionExt<E>, EventListenable<ObservableCollection.ChangeEvent<E>>
A
Collection
wrapper that emits ObservableCollection.ChangeEvent
s when modified through this ObservableCollection
.
Changes made directly to the underlying Collection
instance are not observable and will not trigger events.
ObservableCollection.ChangeEvent
s may include index information if the underlying collection has a defined order:
- For collections with insertion order (e.g.
List
,LinkedHashSet
), added and removed items include their index. - For collections with stable iteration order (e.g.
SortedSet
), only removed items include their index. - For unordered collections (e.g.
HashSet
), indices are-1
.
Listeners can subscribe to receive ObservableCollection.ChangeEvent
s describing the type of change and the affected items.
- Author:
- Sebastian Thomschke
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final record
Describes a change that occurred in anObservableCollection
. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final EventDispatcher<ObservableCollection.ChangeEvent<E>>
protected final boolean
protected final boolean
protected static final int
protected final C
-
Constructor Summary
ConstructorsConstructorDescriptionObservableCollection
(C coll) ObservableCollection
(C coll, EventDispatcher<ObservableCollection.ChangeEvent<E>> dispatcher) -
Method Summary
Modifier and TypeMethodDescriptionboolean
int
Adds all items to the collection accepted by the excludeint
Adds all items to the collection accepted by the excludeint
Adds all items to the collectionint
Adds all items to the collection accepted by the excludeboolean
addAll
(Collection<? extends E> itemsToAdd) int
Adds all items to the collectionint
Adds all items to the collection accepted by the excludevoid
clear()
protected List<ObservableCollection.ChangeEvent.ItemChange<E>>
boolean
boolean
containsAll
(Collection<?> c) boolean
Compares thisObservableCollection
to another object.protected void
fire
(ObservableCollection.ChangeEvent.Operation action, List<ObservableCollection.ChangeEvent.ItemChange<E>> changes) protected void
fire
(ObservableCollection.ChangeEvent.Operation action, ObservableCollection.ChangeEvent.ItemChange<E> change) int
hashCode()
boolean
isEmpty()
iterator()
boolean
boolean
boolean
removeAll
(Collection<?> itemsToRemove) boolean
protected boolean
removeMatching
(Predicate<? super E> shouldRemove, ObservableCollection.ChangeEvent.Operation action) boolean
boolean
retainAll
(Collection<?> itemsToKeep) int
size()
boolean
subscribe
(EventListener<ObservableCollection.ChangeEvent<E>> listener) Object[]
toArray()
<T> T[]
toArray
(T[] a) toString()
boolean
unsubscribe
(EventListener<ObservableCollection.ChangeEvent<E>> listener) Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, spliterator, stream, toArray
Methods inherited from interface net.sf.jstuff.core.collection.ext.CollectionExt
containsIdentical, isNotEmpty, removeIfNot
Methods inherited from interface net.sf.jstuff.core.event.EventListenable
subscribe
-
Field Details
-
UNKNOWN_INDEX
protected static final int UNKNOWN_INDEX- See Also:
-
events
-
wrapped
-
supportsInsertionIndex
protected final boolean supportsInsertionIndex -
supportsRemovalIndex
protected final boolean supportsRemovalIndex
-
-
Constructor Details
-
ObservableCollection
-
ObservableCollection
public ObservableCollection(C coll, EventDispatcher<ObservableCollection.ChangeEvent<E>> dispatcher)
-
-
Method Details
-
add
- Specified by:
add
in interfaceCollection<E>
-
addAll
- Specified by:
addAll
in interfaceCollection<E>
-
addAll
Description copied from interface:CollectionExt
Adds all items to the collection accepted by the exclude- Specified by:
addAll
in interfaceCollectionExt<E>
- Returns:
- number of items added
-
addAll
Description copied from interface:CollectionExt
Adds all items to the collection accepted by the exclude- Specified by:
addAll
in interfaceCollectionExt<E>
- Returns:
- number of items added
-
addAll
Description copied from interface:CollectionExt
Adds all items to the collection- Specified by:
addAll
in interfaceCollectionExt<E>
- Returns:
- number of items added
-
addAll
Description copied from interface:CollectionExt
Adds all items to the collection accepted by the exclude- Specified by:
addAll
in interfaceCollectionExt<E>
- Returns:
- number of items added
-
addAll
Description copied from interface:CollectionExt
Adds all items to the collection- Specified by:
addAll
in interfaceCollectionExt<E>
- Returns:
- number of items added
-
addAll
Description copied from interface:CollectionExt
Adds all items to the collection accepted by the exclude- Specified by:
addAll
in interfaceCollectionExt<E>
- Returns:
- number of items added
-
clear
public void clear()- Specified by:
clear
in interfaceCollection<E>
-
clearInternal
-
contains
- Specified by:
contains
in interfaceCollection<E>
-
containsAll
- Specified by:
containsAll
in interfaceCollection<E>
-
equals
Compares thisObservableCollection
to another object.Returns
true
if:- The other object is the same instance.
- The other object is an
ObservableCollection
with an equal underlying collection. - The other object is a
Collection
equal to the underlying collection.
- Specified by:
equals
in interfaceCollection<E>
- Overrides:
equals
in classObject
-
fire
protected void fire(ObservableCollection.ChangeEvent.Operation action, ObservableCollection.ChangeEvent.ItemChange<E> change) -
fire
protected void fire(ObservableCollection.ChangeEvent.Operation action, List<ObservableCollection.ChangeEvent.ItemChange<E>> changes) -
hashCode
public int hashCode()- Specified by:
hashCode
in interfaceCollection<E>
- Overrides:
hashCode
in classObject
- Returns:
- the hash code based on the wrapped collection
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<E>
-
iterator
-
remove
- Specified by:
remove
in interfaceCollection<E>
-
removeAll
- Specified by:
removeAll
in interfaceCollection<E>
-
removeAll
-
removeIf
- Specified by:
removeIf
in interfaceCollection<E>
-
removeMatching
protected boolean removeMatching(Predicate<? super E> shouldRemove, ObservableCollection.ChangeEvent.Operation action) -
retainAll
- Specified by:
retainAll
in interfaceCollection<E>
-
retainAll
-
size
public int size()- Specified by:
size
in interfaceCollection<E>
-
subscribe
- Specified by:
subscribe
in interfaceEventListenable<E>
-
toArray
- Specified by:
toArray
in interfaceCollection<E>
-
toArray
public <T> T[] toArray(T[] a) - Specified by:
toArray
in interfaceCollection<E>
-
toString
-
unsubscribe
- Specified by:
unsubscribe
in interfaceEventListenable<E>
-