Class ObservableListBase<E>
- Type Parameters:
E
- the type of the elements contained in the List
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,List<E>
,Observable
,ObservableList<E>
- Direct Known Subclasses:
ModifiableObservableListBase
,TransformationList
ObservableList
implementations.
The base class provides two functionalities for the implementing classes.
- Listener handling by implementing
addListener
andremoveListener
methods.fireChange(javafx.collections.ListChangeListener.Change)
method is provided for notifying the listeners with aChange
object. - Methods for building up a
ListChangeListener.Change
object. There are various methods callednext*
, likenextAdd(int, int)
for new items in the lists ornextRemove(int, java.lang.Object)
for an item being removed from the list.These methods must be always enclosed in
beginChange()
andendChange()
block.See the example below.
public void removeOddIndexes() { beginChange(); try { for (int i = 1; i < size(); ++i) { remove(i); } } finally { endChange(); } } public void remove(int i) { beginChange(); try { E removed = ... //do some stuff that will actually remove the element at index i nextRemove(i, removed); } finally { endChange(); } }The
try
/finally
blocks in the example are needed only if there's a possibility for an exception to occur
inside a beginChange()
/ endChange()
block
Note: If you want to create modifiable ObservableList
implementation, consider
using ModifiableObservableListBase
as a superclass.
Note: In order to create list with sequential access, you should override AbstractList.listIterator()
,
AbstractList.iterator()
methods and use them in AbstractList.get(int)
, AbstractCollection.size()
and other methods accordingly.
- Since:
- JavaFX 8.0
- See Also:
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
A convenient method for var-arg adding of elements.final void
addListener
(InvalidationListener listener) Adds anInvalidationListener
which will be notified whenever theObservable
becomes invalid.final void
addListener
(ListChangeListener<? super E> listener) Add a listener to this observable list.protected final void
Begins a change block.protected final void
Ends the change block.protected final void
fireChange
(ListChangeListener.Change<? extends E> change) Notifies all listeners of a changeprotected final boolean
Returns true if there are some listeners registered for this list.protected final void
nextAdd
(int from, int to) Adds a new add operation to the change.protected final void
nextPermutation
(int from, int to, int[] perm) Adds a new permutation operation to the change.protected final void
nextRemove
(int idx, E removed) Adds a new remove operation to the change with single item removed.protected final void
nextRemove
(int idx, List<? extends E> removed) Adds a new remove operation to the change with multiple items removed.protected final void
nextReplace
(int from, int to, List<? extends E> removed) Adds a new replace operation to the change.protected final void
Adds a new set operation to the change.protected final void
nextUpdate
(int pos) Adds a new update operation to the change.void
remove
(int from, int to) Basically a shortcut to sublist(from, to).clear() As this is a common operation, ObservableList has this method for convenient usage.boolean
A convenient method for var-arg usage of removaAll method.final void
removeListener
(InvalidationListener listener) Removes the given listener from the list of listeners, that are notified whenever the value of theObservable
becomes invalid.final void
removeListener
(ListChangeListener<? super E> listener) Tries to removed a listener from this observable list.boolean
A convenient method for var-arg usage of retain method.boolean
Clears the ObservableList and add all the elements passed as var-args.boolean
setAll
(Collection<? extends E> col) Clears the ObservableList and add all elements from the collection.Methods inherited from class java.util.AbstractList
add, add, addAll, clear, equals, get, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subList
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, size, toArray, toArray, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream
Methods inherited from interface java.util.List
add, add, addAll, addAll, clear, contains, containsAll, equals, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, replaceAll, retainAll, set, size, sort, spliterator, subList, toArray, toArray
Methods inherited from interface javafx.collections.ObservableList
filtered, sorted
-
Constructor Details
-
ObservableListBase
public ObservableListBase()
-
-
Method Details
-
nextUpdate
protected final void nextUpdate(int pos) Adds a new update operation to the change.Note: needs to be called inside
beginChange()
/endChange()
block.Note: needs to reflect the current state of the list.
- Parameters:
pos
- the position in the list where the updated element resides.
-
nextSet
Adds a new set operation to the change. Equivalent tonextRemove(idx); nextAdd(idx, idx + 1);
.Note: needs to be called inside
beginChange()
/endChange()
block.Note: needs to reflect the current state of the list.
- Parameters:
idx
- the index of the item that was setold
- the old value at theidx
position.
-
nextReplace
Adds a new replace operation to the change. Equivalent tonextRemove(from, removed); nextAdd(from, to);
Note: needs to be called inside
beginChange()
/endChange()
block.Note: needs to reflect the current state of the list.
- Parameters:
from
- the index where the items were replacedto
- the end index (exclusive) of the range where the new items resideremoved
- the list of items that were removed
-
nextRemove
Adds a new remove operation to the change with multiple items removed.Note: needs to be called inside
beginChange()
/endChange()
block.Note: needs to reflect the current state of the list.
- Parameters:
idx
- the index where the items were removedremoved
- the list of items that were removed
-
nextRemove
Adds a new remove operation to the change with single item removed.Note: needs to be called inside
beginChange()
/endChange()
block.Note: needs to reflect the current state of the list.
- Parameters:
idx
- the index where the item was removedremoved
- the item that was removed
-
nextPermutation
protected final void nextPermutation(int from, int to, int[] perm) Adds a new permutation operation to the change. The permutation on index"i"
contains the index, where the item from the index"i"
was moved.It's not necessary to provide the smallest permutation possible. It's correct to always call this method with
nextPermutation(0, size(), permutation);
Note: needs to be called inside
beginChange()
/endChange()
block.Note: needs to reflect the current state of the list.
- Parameters:
from
- marks the beginning (inclusive) of the range that was permutatedto
- marks the end (exclusive) of the range that was permutatedperm
- the permutation in that range. Even iffrom != 0
, the array should contain the indexes of the list. Therefore, such permutation would not contain indexes of range(0, from)
-
nextAdd
protected final void nextAdd(int from, int to) Adds a new add operation to the change. There's no need to provide the list of added items as they can be found directly in the list under the specified indexes.Note: needs to be called inside
beginChange()
/endChange()
block.Note: needs to reflect the current state of the list.
- Parameters:
from
- marks the beginning (inclusive) of the range that was addedto
- marks the end (exclusive) of the range that was added
-
beginChange
protected final void beginChange()Begins a change block. Must be called before any of thenext*
methods is called. For everybeginChange()
, there must be a correspondingendChange()
call.beginChange()
calls can be nested in abeginChange()
/endChange()
block.- See Also:
-
endChange
protected final void endChange()Ends the change block. If the block is the outer-most block for theObservableList
, theChange
is constructed and all listeners are notified.Ending a nested block doesn't fire a notification.
- See Also:
-
addListener
Description copied from interface:Observable
Adds anInvalidationListener
which will be notified whenever theObservable
becomes invalid. If the same listener is added more than once, then it will be notified more than once. That is, no check is made to ensure uniqueness.Note that the same actual
InvalidationListener
instance may be safely registered for differentObservables
.The
Observable
stores a strong reference to the listener which will prevent the listener from being garbage collected and may result in a memory leak. It is recommended to either unregister a listener by callingremoveListener
after use or to use an instance ofWeakInvalidationListener
avoid this situation.- Specified by:
addListener
in interfaceObservable
- Parameters:
listener
- The listener to register- See Also:
-
removeListener
Description copied from interface:Observable
Removes the given listener from the list of listeners, that are notified whenever the value of theObservable
becomes invalid.If the given listener has not been previously registered (i.e. it was never added) then this method call is a no-op. If it had been previously added then it will be removed. If it had been added more than once, then only the first occurrence will be removed.
- Specified by:
removeListener
in interfaceObservable
- Parameters:
listener
- The listener to remove- See Also:
-
addListener
Description copied from interface:ObservableList
Add a listener to this observable list.- Specified by:
addListener
in interfaceObservableList<E>
- Parameters:
listener
- the listener for listening to the list changes
-
removeListener
Description copied from interface:ObservableList
Tries to removed a listener from this observable list. If the listener is not attached to this list, nothing happens.- Specified by:
removeListener
in interfaceObservableList<E>
- Parameters:
listener
- a listener to remove
-
fireChange
Notifies all listeners of a change- Parameters:
change
- an object representing the change that was done
-
hasListeners
protected final boolean hasListeners()Returns true if there are some listeners registered for this list.- Returns:
- true if there is a listener for this list
-
addAll
Description copied from interface:ObservableList
A convenient method for var-arg adding of elements.- Specified by:
addAll
in interfaceObservableList<E>
- Parameters:
elements
- the elements to add- Returns:
- true (as specified by Collection.add(E))
-
setAll
Description copied from interface:ObservableList
Clears the ObservableList and add all the elements passed as var-args.- Specified by:
setAll
in interfaceObservableList<E>
- Parameters:
elements
- the elements to set- Returns:
- true (as specified by Collection.add(E))
-
setAll
Description copied from interface:ObservableList
Clears the ObservableList and add all elements from the collection.- Specified by:
setAll
in interfaceObservableList<E>
- Parameters:
col
- the collection with elements that will be added to this observableArrayList- Returns:
- true (as specified by Collection.add(E))
-
removeAll
Description copied from interface:ObservableList
A convenient method for var-arg usage of removaAll method.- Specified by:
removeAll
in interfaceObservableList<E>
- Parameters:
elements
- the elements to be removed- Returns:
- true if list changed as a result of this call
-
retainAll
Description copied from interface:ObservableList
A convenient method for var-arg usage of retain method.- Specified by:
retainAll
in interfaceObservableList<E>
- Parameters:
elements
- the elements to be retained- Returns:
- true if list changed as a result of this call
-
remove
public void remove(int from, int to) Description copied from interface:ObservableList
Basically a shortcut to sublist(from, to).clear() As this is a common operation, ObservableList has this method for convenient usage.- Specified by:
remove
in interfaceObservableList<E>
- Parameters:
from
- the start of the range to remove (inclusive)to
- the end of the range to remove (exclusive)
-