E
- the type of the elements contained in the Listpublic abstract class ObservableListBase<E> extends AbstractList<E> implements ObservableList<E>
ObservableList
implementations.
The base class provides two functionalities for the implementing classes.
addListener
and removeListener
methods.
fireChange(javafx.collections.ListChangeListener.Change)
method is provided
for notifying the listeners with a Change
object.
ListChangeListener.Change
object. There are various methods called
next*
, like nextAdd(int, int)
for new items in the lists or nextRemove(int, java.lang.Object)
for
an item being removed from the list.
These methods must be always enclosed in beginChange()
and endChange()
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.
ObservableList
,
ListChangeListener.Change
,
ModifiableObservableListBase
modCount
Constructor and Description |
---|
ObservableListBase() |
Modifier and Type | Method and Description |
---|---|
boolean |
addAll(E... elements)
A convenient method for var-arg adding of elements.
|
void |
addListener(InvalidationListener listener)
Adds an
InvalidationListener which will be notified whenever the
Observable becomes invalid. |
void |
addListener(ListChangeListener<? super E> listener)
Add a listener to this observable list.
|
protected void |
beginChange()
Begins a change block.
|
protected void |
endChange()
Ends the change block.
|
protected void |
fireChange(ListChangeListener.Change<? extends E> change)
Notifies all listeners of a change
|
protected boolean |
hasListeners()
Returns true if there are some listeners registered for this list.
|
protected void |
nextAdd(int from,
int to)
Adds a new add operation to the change.
|
protected void |
nextPermutation(int from,
int to,
int[] perm)
Adds a new permutation operation to the change.
|
protected void |
nextRemove(int idx,
E removed)
Adds a new remove operation to the change with single item removed.
|
protected void |
nextRemove(int idx,
List<? extends E> removed)
Adds a new remove operation to the change with multiple items removed.
|
protected void |
nextReplace(int from,
int to,
List<? extends E> removed)
Adds a new replace operation to the change.
|
protected void |
nextSet(int idx,
E old)
Adds a new set operation to the change.
|
protected 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 |
removeAll(E... elements)
A convenient method for var-arg usage of removaAll method.
|
void |
removeListener(InvalidationListener listener)
Removes the given listener from the list of listeners, that are notified
whenever the value of the
Observable becomes invalid. |
void |
removeListener(ListChangeListener<? super E> listener)
Tries to removed a listener from this observable list.
|
boolean |
retainAll(E... elements)
A convenient method for var-arg usage of retain method.
|
boolean |
setAll(Collection<? extends E> col)
Clears the ObservableList and add all elements from the collection.
|
boolean |
setAll(E... elements)
Clears the ObservableList and add all the elements passed as var-args.
|
add, add, addAll, clear, equals, get, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subList
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, size, toArray, toArray, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
filtered, sorted, sorted
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
parallelStream, removeIf, stream
protected final void nextUpdate(int pos)
Note: needs to be called inside beginChange()
/ endChange()
block.
Note: needs to reflect the current state of the list.
pos
- the position in the list where the updated element resides.protected final void nextSet(int idx, E old)
nextRemove(idx); nextAdd(idx, idx + 1);
.
Note: needs to be called inside beginChange()
/ endChange()
block.
Note: needs to reflect the current state of the list.
idx
- the index of the item that was setold
- the old value at the idx
position.protected final void nextReplace(int from, int to, List<? extends E> removed)
nextRemove(from, removed); nextAdd(from, to);
Note: needs to be called inside beginChange()
/ endChange()
block.
Note: needs to reflect the current state of the list.
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 removedprotected final void nextRemove(int idx, List<? extends E> removed)
Note: needs to be called inside beginChange()
/ endChange()
block.
Note: needs to reflect the current state of the list.
idx
- the index where the items were removedremoved
- the list of items that were removedprotected final void nextRemove(int idx, E removed)
Note: needs to be called inside beginChange()
/ endChange()
block.
Note: needs to reflect the current state of the list.
idx
- the index where the item was removedremoved
- the item that was removedprotected final void nextPermutation(int from, int to, int[] perm)
"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.
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 if from != 0
, the array should
contain the indexes of the list. Therefore, such permutation would not contain indexes of range (0, from)
protected final void nextAdd(int from, int to)
Note: needs to be called inside beginChange()
/ endChange()
block.
Note: needs to reflect the current state of the list.
from
- marks the beginning (inclusive) of the range that was addedto
- marks the end (exclusive) of the range that was addedprotected final void beginChange()
next*
methods is called.
For every beginChange()
, there must be a corresponding endChange()
call.
beginChange()
calls can be nested in a beginChange()
/endChange()
block.
endChange()
protected final void endChange()
ObservableList
, the
Change
is constructed and all listeners are notified.
Ending a nested block doesn't fire a notification.
beginChange()
public final void addListener(InvalidationListener listener)
Observable
InvalidationListener
which will be notified whenever the
Observable
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 different Observables
.
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 calling removeListener
after use or to use an instance of
WeakInvalidationListener
avoid this situation.
addListener
in interface Observable
listener
- The listener to registerObservable.removeListener(InvalidationListener)
public final void removeListener(InvalidationListener listener)
Observable
Observable
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.
removeListener
in interface Observable
listener
- The listener to removeObservable.addListener(InvalidationListener)
public final void addListener(ListChangeListener<? super E> listener)
ObservableList
addListener
in interface ObservableList<E>
listener
- the listener for listening to the list changespublic final void removeListener(ListChangeListener<? super E> listener)
ObservableList
removeListener
in interface ObservableList<E>
listener
- a listener to removeprotected final void fireChange(ListChangeListener.Change<? extends E> change)
change
- protected final boolean hasListeners()
public boolean addAll(E... elements)
ObservableList
addAll
in interface ObservableList<E>
elements
- the elements to addpublic boolean setAll(E... elements)
ObservableList
setAll
in interface ObservableList<E>
elements
- the elements to setpublic boolean setAll(Collection<? extends E> col)
ObservableList
setAll
in interface ObservableList<E>
col
- the collection with elements that will be added to this observableArrayListpublic boolean removeAll(E... elements)
ObservableList
removeAll
in interface ObservableList<E>
elements
- the elements to be removedpublic boolean retainAll(E... elements)
ObservableList
retainAll
in interface ObservableList<E>
elements
- the elements to be retainedpublic void remove(int from, int to)
ObservableList
remove
in interface ObservableList<E>
from
- the start of the range to remove (inclusive)to
- the end of the range to remove (exclusive)Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 2008, 2020, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.