public class ObservableList
extends Object
implements List
List decorator that will trigger PropertyChangeEvents when a value changes.
An optional Closure may be specified and will work as a filter, if it returns true the property
will trigger an event (if the value indeed changed), otherwise it won't. The Closure may receive
1 or 2 parameters, the single one being the value, the other one both the key and value, for
example:
// skip all properties whose value is a closure
def map = new ObservableList( {!(it instanceof Closure)} )
// skip all properties whose name matches a regex
def map = new ObservableList( { name, value -> !(name =˜ /[A-Z+]/) } )
The current implementation will trigger specialized events in the following scenarios, you need
not register a different listener as those events extend from PropertyChangeEvent
Bound properties
| Modifiers | Name | Description |
|---|---|---|
enum |
ObservableList.ChangeType |
Enumerates the specialized list change events. |
static class |
ObservableList.ElementAddedEvent |
Event fired when one element is added. |
static class |
ObservableList.ElementClearedEvent |
Event fired when the list is cleared. |
static class |
ObservableList.ElementEvent |
Base event type for observable list content changes. |
static class |
ObservableList.ElementRemovedEvent |
Event fired when one element is removed. |
static class |
ObservableList.ElementUpdatedEvent |
Event fired when one element is updated. |
static class |
ObservableList.MultiElementAddedEvent |
Event fired when multiple elements are added. |
static class |
ObservableList.MultiElementRemovedEvent |
Event fired when multiple elements are removed. |
protected class |
ObservableList.ObservableIterator |
Iterator wrapper that reports removals as observable list events. |
protected class |
ObservableList.ObservableListIterator |
List iterator wrapper that routes mutations through ObservableList. |
| Modifiers | Name | Description |
|---|---|---|
static String |
CONTENT_PROPERTY |
Bound property name for list content changes. |
static String |
SIZE_PROPERTY |
Bound property name for list size changes. |
| Constructor and description |
|---|
ObservableList()Creates an observable list backed by an ArrayList. |
ObservableList(List delegate)Creates an observable list backed by the supplied delegate. |
ObservableList(Closure test)Creates an observable list backed by an ArrayList. |
ObservableList(List delegate, Closure test)Creates an observable list backed by the supplied delegate. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public void |
add(int index, Object element)Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
|
|
public boolean |
add(Object o)Appends the specified element to the end of this list (optional operation). Lists that support this operation may place limitations on what elements may be added to this list. In particular, some lists will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. List classes should clearly specify in their documentation any restrictions on what elements may be added.
|
|
public boolean |
addAll(Collection c)Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)
|
|
public boolean |
addAll(int index, Collection c)Inserts all of the elements in the specified collection into this list at the specified position (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that they are returned by the specified collection's iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)
|
|
public void |
addPropertyChangeListener(PropertyChangeListener listener)Registers a listener for all observable list events. |
|
public void |
addPropertyChangeListener(String propertyName, PropertyChangeListener listener)Registers a listener for a named bound property. |
|
public void |
clear()Removes all of the elements from this list (optional operation). The list will be empty after this call returns.
|
|
public boolean |
contains(Object o)Returns true if this list contains the specified element.
More formally, returns true if and only if this list contains
at least one element e such that
Objects.equals(o, e).
|
|
public boolean |
containsAll(Collection c)Returns true if this list contains all of the elements of the
specified collection.
|
|
public boolean |
equals(Object o)Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.
|
|
protected void |
fireElementAddedEvent(int index, Object element)Fires a single-element added event. |
|
protected void |
fireElementClearedEvent(List values)Fires a cleared event containing removed values. |
|
protected void |
fireElementEvent(ObservableList.ElementEvent event)Publishes an element event to registered listeners. |
|
protected void |
fireElementRemovedEvent(int index, Object element)Fires a single-element removed event. |
|
protected void |
fireElementUpdatedEvent(int index, Object oldValue, Object newValue)Fires a single-element updated event. |
|
protected void |
fireMultiElementAddedEvent(int index, List values)Fires a multi-element added event. |
|
protected void |
fireMultiElementRemovedEvent(List values)Fires a multi-element removed event. |
|
protected void |
fireSizeChangedEvent(int oldValue, int newValue)Fires the bound size change event. |
|
public Object |
get(int index)Returns the element at the specified position in this list.
|
|
public List |
getContent()Returns an unmodifiable snapshot view of the backing list. |
|
protected List |
getDelegateList()Returns the mutable backing list. |
|
public PropertyChangeListener[] |
getPropertyChangeListeners()Returns listeners registered for all properties. |
|
public PropertyChangeListener[] |
getPropertyChangeListeners(String propertyName)Returns listeners registered for a named property. |
|
public int |
getSize()Returns the current list size as a bound property value. |
|
protected Closure |
getTest()Returns the optional event filter closure. |
|
public boolean |
hasListeners(String propertyName)Reports whether listeners are registered for a named property. |
|
public int |
hashCode()Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.
The general contract of
|
|
public int |
indexOf(Object o)Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that
Objects.equals(o, get(i)),
or -1 if there is no such index.
|
|
public boolean |
isEmpty()Returns true if this list contains no elements.
|
|
public Iterator |
iterator()Returns an iterator over the elements in this list in proper sequence.
|
|
public int |
lastIndexOf(Object o)Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the highest index i such that
Objects.equals(o, get(i)),
or -1 if there is no such index.
|
|
public ListIterator |
listIterator()Returns a list iterator over the elements in this list (in proper sequence).
|
|
public ListIterator |
listIterator(int index)Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to previous would return the element with the specified index minus one.
|
|
public Object |
remove(int index)Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
|
|
public boolean |
remove(Object o)Removes the first occurrence of the specified element from this list, if it is present (optional operation). If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that
Objects.equals(o, get(i))
(if such an element exists). Returns true if this list
contained the specified element (or equivalently, if this list changed
as a result of the call).
|
|
public boolean |
removeAll(Collection c)Removes from this list all of its elements that are contained in the specified collection (optional operation).
|
|
public void |
removePropertyChangeListener(PropertyChangeListener listener)Removes a listener registered for all properties. |
|
public void |
removePropertyChangeListener(String propertyName, PropertyChangeListener listener)Removes a listener registered for a named property. |
|
public boolean |
retainAll(Collection c)Retains only the elements in this list that are contained in the specified collection (optional operation). In other words, removes from this list all of its elements that are not contained in the specified collection.
|
|
public Object |
set(int index, Object element)Replaces the element at the specified position in this list with the specified element (optional operation).
|
|
public int |
size()Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns
Integer.MAX_VALUE.
|
|
public List |
subList(int fromIndex, int toIndex)Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If
fromIndex and toIndex are equal, the returned list is
empty.) The returned list is backed by this list, so non-structural
changes in the returned list are reflected in this list, and vice-versa.
The returned list supports all of the optional list operations supported
by this list.This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:
Similar idioms may be constructed for indexOf and
lastIndexOf, and all of the algorithms in the
Collections class can be applied to a subList.The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)
|
|
public Object[] |
toArray()Returns an array containing all of the elements in this list in proper sequence (from first to last element). The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array even if this list is backed by an array). The caller is thus free to modify the returned array. This method acts as bridge between array-based and collection-based APIs.
|
|
public Object[] |
toArray(Object[] a)Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list. If the list fits in the specified array with room to spare (i.e.,
the array has more elements than the list), the element in the array
immediately following the end of the list is set to Like the toArray() method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs. Suppose
Note that toArray(new Object[0]) is identical in function to
toArray().
|
Bound property name for list content changes.
Bound property name for list size changes.
Creates an observable list backed by an ArrayList.
Creates an observable list backed by the supplied delegate.
delegate - the backing listCreates an observable list backed by an ArrayList.
test - optional event filterInserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
add operation
is not supported by this listindex < 0 || index > size())index - index at which the specified element is to be insertedelement - element to be insertedAppends the specified element to the end of this list (optional operation).
Lists that support this operation may place limitations on what elements may be added to this list. In particular, some lists will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. List classes should clearly specify in their documentation any restrictions on what elements may be added.
add operation
is not supported by this liste - element to be appended to this listtrue (as specified by Collection.add)Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)
addAll operation
is not supported by this listc - collection containing elements to be added to this listtrue if this list changed as a result of the callInserts all of the elements in the specified collection into this list at the specified position (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that they are returned by the specified collection's iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)
addAll operation
is not supported by this listindex < 0 || index > size())index - index at which to insert the first element from the
specified collectionc - collection containing elements to be added to this listtrue if this list changed as a result of the callRegisters a listener for all observable list events.
listener - the listener to addRegisters a listener for a named bound property.
propertyName - the property to observelistener - the listener to addRemoves all of the elements from this list (optional operation). The list will be empty after this call returns.
clear operation
is not supported by this list Returns true if this list contains the specified element.
More formally, returns true if and only if this list contains
at least one element e such that
Objects.equals(o, e).
o - element whose presence in this list is to be testedtrue if this list contains the specified element Returns true if this list contains all of the elements of the
specified collection.
c - collection to be checked for containment in this listtrue if this list contains all of the elements of the
specified collectionIndicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation
on non-null object references:
x, x.equals(x) should return
true.
x and y, x.equals(y)
should return true if and only if
y.equals(x) returns true.
x, y, and z, if
x.equals(y) returns true and
y.equals(z) returns true, then
x.equals(z) should return true.
x and y, multiple invocations of
x.equals(y) consistently return true
or consistently return false, provided no
information used in equals comparisons on the
objects is modified.
x,
x.equals(null) should return false.
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.
equals method for class Object implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x and
y, this method returns true if and only
if x and y refer to the same object
(x == y has the value true).
In other words, under the reference equality equivalence
relation, each equivalence class only has a single element.hashCode method, which states
that equal objects must have equal hash codes.obj - the reference object with which to compare.true if this object is the same as the obj
argument; false otherwise.Fires a single-element added event.
Fires a cleared event containing removed values.
Publishes an element event to registered listeners.
Fires a single-element removed event.
Fires a single-element updated event.
Fires a multi-element added event.
Fires a multi-element removed event.
Fires the bound size change event.
Returns the element at the specified position in this list.
index < 0 || index >= size())index - index of the element to returnReturns an unmodifiable snapshot view of the backing list.
Returns the mutable backing list.
Returns listeners registered for all properties.
Returns listeners registered for a named property.
propertyName - the observed property nameReturns the current list size as a bound property value.
Returns the optional event filter closure.
nullReports whether listeners are registered for a named property.
propertyName - the property name to inspecttrue if listeners are registeredReturns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.
The general contract of hashCode is:
hashCode method
must consistently return the same integer, provided no information
used in equals comparisons on the object is modified.
This integer need not remain consistent from one execution of an
application to another execution of the same application.
hashCode method on each of the two objects must produce the
same integer result.
hashCode method on each of the two objects
must produce distinct integer results. However, the programmer
should be aware that producing distinct integer results for
unequal objects may improve the performance of hash tables.
hashCode method defined
by class Object returns distinct integers for distinct objects. Returns the index of the first occurrence of the specified element
in this list, or -1 if this list does not contain the element.
More formally, returns the lowest index i such that
Objects.equals(o, get(i)),
or -1 if there is no such index.
o - element to search for Returns true if this list contains no elements.
true if this list contains no elementsReturns an iterator over the elements in this list in proper sequence.
Returns the index of the last occurrence of the specified element
in this list, or -1 if this list does not contain the element.
More formally, returns the highest index i such that
Objects.equals(o, get(i)),
or -1 if there is no such index.
o - element to search forReturns a list iterator over the elements in this list (in proper sequence).
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to previous would return the element with the specified index minus one.
index < 0 || index > size())index - index of the first element to be returned from the
list iterator (by a call to next)Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
remove operation
is not supported by this listindex < 0 || index >= size())index - the index of the element to be removed Removes the first occurrence of the specified element from this list,
if it is present (optional operation). If this list does not contain
the element, it is unchanged. More formally, removes the element with
the lowest index i such that
Objects.equals(o, get(i))
(if such an element exists). Returns true if this list
contained the specified element (or equivalently, if this list changed
as a result of the call).
remove operation
is not supported by this listo - element to be removed from this list, if presenttrue if this list contained the specified elementRemoves from this list all of its elements that are contained in the specified collection (optional operation).
removeAll operation
is not supported by this listc - collection containing elements to be removed from this listtrue if this list changed as a result of the callRemoves a listener registered for all properties.
listener - the listener to removeRemoves a listener registered for a named property.
propertyName - the observed property namelistener - the listener to removeRetains only the elements in this list that are contained in the specified collection (optional operation). In other words, removes from this list all of its elements that are not contained in the specified collection.
retainAll operation
is not supported by this listc - collection containing elements to be retained in this listtrue if this list changed as a result of the callReplaces the element at the specified position in this list with the specified element (optional operation).
set operation
is not supported by this listindex < 0 || index >= size())index - index of the element to replaceelement - element to be stored at the specified position Returns the number of elements in this list. If this list contains
more than Integer.MAX_VALUE elements, returns
Integer.MAX_VALUE.
Returns a view of the portion of this list between the specified
fromIndex, inclusive, and toIndex, exclusive. (If
fromIndex and toIndex are equal, the returned list is
empty.) The returned list is backed by this list, so non-structural
changes in the returned list are reflected in this list, and vice-versa.
The returned list supports all of the optional list operations supported
by this list.
This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:
list.subList(from, to).clear();
Similar idioms may be constructed for indexOf and
lastIndexOf, and all of the algorithms in the
Collections class can be applied to a subList.The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)
fromIndex < 0 || toIndex > size ||
fromIndex > toIndex)fromIndex - low endpoint (inclusive) of the subListtoIndex - high endpoint (exclusive) of the subListReturns an array containing all of the elements in this list in proper sequence (from first to last element).
The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array even if this list is backed by an array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.
If the list fits in the specified array with room to spare (i.e.,
the array has more elements than the list), the element in the array
immediately following the end of the list is set to null.
(This is useful in determining the length of the list only if
the caller knows that the list does not contain any null elements.)
Like the toArray() method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.
Suppose x is a list known to contain only strings.
The following code can be used to dump the list into a newly
allocated array of String:
String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to
toArray().a - the array into which the elements of this list are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose.Copyright © 2003-2026 The Apache Software Foundation. All rights reserved.