E
- the list element typepublic abstract static class ListChangeListener.Change<E> extends Object
next()
method.
Each change must be one of the following:
wasPermutated()
returns true in this case.
The permutation happened at range between from
(inclusive) and to
(exclusive) and
can be queried by calling getPermutation(int)
method.
wasAdded()
, wasRemoved()
returns true.
If both methods return true, wasReplaced()
will also return true.
The getRemoved()
method returns a list of elements that have been
replaced or removed from the list.
The range between from
(inclusive) and to
(exclusive)
denotes the sublist of the list that contain new elements. Note that this is a half-open
interval, so if no elements were added, getFrom()
is equal to getTo()
.
It is possible to get a list of added elements by calling getAddedSubList().
Note that in order to maintain correct indexes of the separate add/remove changes, these changes
must be sorted by their from
index.
wasUpdated()
return true on an update change.
All elements between from
(inclusive) and to
(exclusive) were updated.
next()
method before calling
any other method of Change
. The same applies after calling reset()
.
The only methods that works at any time is getList()
.
Typical usage is to observe changes on an ObservableList in order
to hook or unhook (or add or remove a listener) or in order to maintain
some invariant on every element in that ObservableList. A common code
pattern for doing this looks something like the following:
ObservableList- theList = ...; theList.addListener(new ListChangeListener<Item>() { public void onChanged(Change<tem> c) { while (c.next()) { if (c.wasPermutated()) { for (int i = c.getFrom(); i < c.getTo(); ++i) { //permutate } } else if (c.wasUpdated()) { //update item } else { for (Item remitem : c.getRemoved()) { remitem.remove(Outer.this); } for (Item additem : c.getAddedSubList()) { additem.add(Outer.this); } } } } }); }
Warning: This class directly accesses the source list to acquire information about the changes.
This effectively makes the Change object invalid when another change occurs on the list.
For this reason it is not safe to use this class on a different thread.
It also means the source list cannot be modified inside the listener since that would invalidate this Change object
for all subsequent listeners.
Note: in case the change contains multiple changes of different type, these changes must be in the following order: permutation change(s), add or remove changes, update changes This is because permutation changes cannot go after add/remove changes as they would change the position of added elements. And on the other hand, update changes must go after add/remove changes because they refer with their indexes to the current state of the list, which means with all add/remove changes applied.
Constructor and Description |
---|
Change(ObservableList<E> list)
Constructs a new change done to a list.
|
Modifier and Type | Method and Description |
---|---|
int |
getAddedSize()
Size of the interval that was added.
|
List<E> |
getAddedSubList()
To get a subList view of the list that contains only the elements
added, use getAddedSubList() method.
|
abstract int |
getFrom()
If wasAdded is true, the interval contains all the values that were added.
|
ObservableList<E> |
getList()
The source list of the change.
|
protected abstract int[] |
getPermutation()
If this change is an permutation, it returns an integer array
that describes the permutation.
|
int |
getPermutation(int i)
By calling these method, you can observe the permutation that happened.
|
abstract List<E> |
getRemoved()
An immutable list of removed/replaced elements.
|
int |
getRemovedSize()
Size of getRemoved() list.
|
abstract int |
getTo()
The end of the change interval.
|
abstract boolean |
next()
Go to the next change.
|
abstract void |
reset()
Reset to the initial stage.
|
boolean |
wasAdded()
Indicates if elements were added during this change
|
boolean |
wasPermutated()
Indicates if the change was only a permutation.
|
boolean |
wasRemoved()
Indicates if elements were removed during this change.
|
boolean |
wasReplaced()
Indicates if elements were replaced during this change.
|
boolean |
wasUpdated()
Indicates that the elements between getFrom() (inclusive)
to getTo() exclusive has changed.
|
public Change(ObservableList<E> list)
list
- that was changedpublic abstract boolean next()
public abstract void reset()
public ObservableList<E> getList()
public abstract int getFrom()
IllegalStateException
- if this Change is in initial statepublic abstract int getTo()
IllegalStateException
- if this Change is in initial stategetFrom()
public abstract List<E> getRemoved()
IllegalStateException
- if this Change is in initial statepublic boolean wasPermutated()
IllegalStateException
- if this Change is in initial statepublic boolean wasAdded()
IllegalStateException
- if this Change is in initial statepublic boolean wasRemoved()
wasReplaced()
.IllegalStateException
- if this Change is in initial statepublic boolean wasReplaced()
Usually, it's not necessary to use this method directly.
Handling remove operation and then add operation, as in the example above
,
will effectively handle also set operation.
as wasAdded() && wasRemoved()
IllegalStateException
- if this Change is in initial statepublic boolean wasUpdated()
public List<E> getAddedSubList()
c.getList().subList(c.getFrom(), c.getTo());
for (Node n : change.getAddedSubList()) {
// do something
}
IllegalStateException
- if this Change is in initial statepublic int getRemovedSize()
IllegalStateException
- if this Change is in initial statepublic int getAddedSize()
IllegalStateException
- if this Change is in initial stateprotected abstract int[] getPermutation()
getFrom()
of the list. The same applies
for the last index and getTo()
.
The method is used by wasPermutated()
and getPermutation(int)
methods.IllegalStateException
- if this Change is in initial statepublic int getPermutation(int i)
change.getPermutation(oldIndex);Note: default implementation of this method takes the information from
getPermutation()
method. You don't have to override this method.i
- the old index that contained the element prior to this changeIndexOutOfBoundsException
- if i is out of the bounds of the listIllegalStateException
- if this is not a permutation changeSubmit 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.