Top Description Inners Fields Constructors Methods
org.apache.commons.collections4.list

public Class PredicatedList<E>

extends PredicatedCollection<E>
implements List<E>
Class Inheritance
All Implemented Interfaces
java.util.List, java.util.SequencedCollection, java.util.Collection, java.lang.Iterable
Imports
java.util.Collection, .List, .ListIterator, org.apache.commons.collections4.Predicate, org.apache.commons.collections4.collection.PredicatedCollection, org.apache.commons.collections4.iterators.AbstractListIteratorDecorator

Decorates another List to validate that all additions match a specified predicate.

This list exists to provide validation for the decorated list. It is normally created to decorate an empty list. If an object cannot be added to the list, an IllegalArgumentException is thrown.

One usage would be to ensure that no null entries are added to the list.

List<String> list =
  PredicatedList.predicatedList(new ArrayList<String>(), PredicateUtils.notNullPredicate());

This class is Serializable from Commons Collections 3.1.

Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
PredicatedList.PredicatedListIterator

Inner class Iterator for the PredicatedList

Field Summary

Modifier and TypeField and Description
private static final long
Inherited from org.apache.commons.collections4.collection.PredicatedCollection:
predicate

Constructor Summary

AccessConstructor and Description
protected
PredicatedList(final List<E>
the list to decorate, must not be null
list
,
final Predicate<? super E>
the predicate to use for validation, must not be null
predicate
)

Constructor that wraps (not copies).

Method Summary

Modifier and TypeMethod and Description
public void
add(final int
index at which the specified element is to be inserted
index
,
final E
element to be inserted
object
)

Implements java.util.List.add.

Inserts the specified element at the specified position in this list (optional operation).
public boolean
addAll(final int
index at which to insert the first element from the specified collection
index
,
final Collection<? extends E>
collection containing elements to be added to this list
coll
)

Implements java.util.List.addAll.

Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
protected List<E>

Returns:

the decorated list
decorated
()

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.decorated.

Gets the list being decorated.
public boolean
equals(final Object
the object to be compared for equality with this list
object
)

Overrides java.lang.Object.equals.

Implements java.util.List.equals, java.util.Collection.equals.

Compares the specified object with this list for equality.
public E
get(final int
index of the element to return
index
)

Implements java.util.List.get.

Returns the element at the specified position in this list.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.List.hashCode, java.util.Collection.hashCode.

Returns the hash code value for this list.
public int
indexOf(final Object
element to search for
object
)

Implements java.util.List.indexOf.

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
public int
lastIndexOf(final Object
element to search for
object
)

Implements java.util.List.lastIndexOf.

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
public ListIterator<E>
listIterator()

Implements java.util.List.listIterator.

Returns a list iterator over the elements in this list (in proper sequence).
public ListIterator<E>
listIterator(final int
index of the first element to be returned from the list iterator (by a call to next)
i
)

Implements java.util.List.listIterator.

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
public static <
the type of the elements in the list
T
>
PredicatedList<T>

Returns:

a new predicated list
predicatedList
(final List<T>
the list to decorate, must not be null
list
,
final Predicate<? super T>
the predicate to use for validation, must not be null
predicate
)

Factory method to create a predicated (validating) list.

public E
remove(final int
the index of the element to be removed
index
)

Implements java.util.List.remove.

Removes the element at the specified position in this list (optional operation).
public E
set(final int
index of the element to replace
index
,
final E
element to be stored at the specified position
object
)

Implements java.util.List.set.

Replaces the element at the specified position in this list with the specified element (optional operation).
public List<E>
subList(final int
low endpoint (inclusive) of the subList
fromIndex
,
final int
high endpoint (exclusive) of the subList
toIndex
)

Implements java.util.List.subList.

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
Inherited from org.apache.commons.collections4.collection.PredicatedCollection:
addaddAllbuildernotNullBuilderpredicatedCollectionvalidate

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides org.apache.commons.collections4.collection.PredicatedCollection.serialVersionUID.

Serialization version

Constructor Detail

PredicatedListback to summary
protected PredicatedList(final List<E> list, final Predicate<? super E> predicate)

Constructor that wraps (not copies).

If there are any elements already in the list being decorated, they are validated.

Parameters
list:List<E>

the list to decorate, must not be null

predicate:Predicate<? super E>

the predicate to use for validation, must not be null

Exceptions
NullPointerException:
if list or predicate is null
IllegalArgumentException:
if the list contains invalid elements

Method Detail

addback to summary
public void add(final int index, final E object)

Implements java.util.List.add.

Doc from java.util.List.add.

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).

Parameters
index:int

index at which the specified element is to be inserted

object:E

element to be inserted

Annotations
@Override
addAllback to summary
public boolean addAll(final int index, final Collection<? extends E> coll)

Implements java.util.List.addAll.

Doc from java.util.List.addAll.

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.)

Parameters
index:int

index at which to insert the first element from the specified collection

coll:Collection<? extends E>

collection containing elements to be added to this list

Returns:boolean

true if this list changed as a result of the call

Annotations
@Override
decoratedback to summary
protected List<E> decorated()

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.decorated.

Gets the list being decorated.

Returns:List<E>

the decorated list

Annotations
@Override
equalsback to summary
public boolean equals(final Object object)

Overrides java.lang.Object.equals.

Implements java.util.List.equals, java.util.Collection.equals.

Doc from java.util.List.equals.

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if Objects.equals(e1, e2).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

Parameters
object:Object

the object to be compared for equality with this list

Returns:boolean

true if the specified object is equal to this list

Annotations
@Override
getback to summary
public E get(final int index)

Implements java.util.List.get.

Doc from java.util.List.get.

Returns the element at the specified position in this list.

Parameters
index:int

index of the element to return

Returns:E

the element at the specified position in this list

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.List.hashCode, java.util.Collection.hashCode.

Doc from java.util.List.hashCode.

Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:

int hashCode = 1;
    for (E e : list)
        hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
This ensures that list1.equals(list2) implies that list1.hashCode()==list2.hashCode() for any two lists, list1 and list2, as required by the general contract of Object#hashCode.
Returns:int

the hash code value for this list

Annotations
@Override
indexOfback to summary
public int indexOf(final Object object)

Implements java.util.List.indexOf.

Doc from java.util.List.indexOf.

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.

Parameters
object:Object

element to search for

Returns:int

the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element

Annotations
@Override
lastIndexOfback to summary
public int lastIndexOf(final Object object)

Implements java.util.List.lastIndexOf.

Doc from java.util.List.lastIndexOf.

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.

Parameters
object:Object

element to search for

Returns:int

the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element

Annotations
@Override
listIteratorback to summary
public ListIterator<E> listIterator()

Implements java.util.List.listIterator.

Doc from java.util.List.listIterator.

Returns a list iterator over the elements in this list (in proper sequence).

Returns:ListIterator<E>

a list iterator over the elements in this list (in proper sequence)

Annotations
@Override
listIteratorback to summary
public ListIterator<E> listIterator(final int i)

Implements java.util.List.listIterator.

Doc from java.util.List.listIterator.

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.

Parameters
i:int

index of the first element to be returned from the list iterator (by a call to next)

Returns:ListIterator<E>

a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list

Annotations
@Override
predicatedListback to summary
public static <T> PredicatedList<T> predicatedList(final List<T> list, final Predicate<? super T> predicate)

Factory method to create a predicated (validating) list.

If there are any elements already in the list being decorated, they are validated.

Parameters
<T>
the type of the elements in the list
list:List<T>

the list to decorate, must not be null

predicate:Predicate<? super T>

the predicate to use for validation, must not be null

Returns:PredicatedList<T>

a new predicated list

Exceptions
NullPointerException:
if list or predicate is null
IllegalArgumentException:
if the list contains invalid elements
Since
4.0
removeback to summary
public E remove(final int index)

Implements java.util.List.remove.

Doc from java.util.List.remove.

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.

Parameters
index:int

the index of the element to be removed

Returns:E

the element previously at the specified position

Annotations
@Override
setback to summary
public E set(final int index, final E object)

Implements java.util.List.set.

Doc from java.util.List.set.

Replaces the element at the specified position in this list with the specified element (optional operation).

Parameters
index:int

index of the element to replace

object:E

element to be stored at the specified position

Returns:E

the element previously at the specified position

Annotations
@Override
subListback to summary
public List<E> subList(final int fromIndex, final int toIndex)

Implements java.util.List.subList.

Doc from java.util.List.subList.

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.)

Parameters
fromIndex:int

low endpoint (inclusive) of the subList

toIndex:int

high endpoint (exclusive) of the subList

Returns:List<E>

a view of the specified range within this list

Annotations
@Override
org.apache.commons.collections4.list back to summary

protected Class PredicatedList.PredicatedListIterator

extends AbstractListIteratorDecorator<E>
Class Inheritance

Inner class Iterator for the PredicatedList

Constructor Summary

AccessConstructor and Description
protected
PredicatedListIterator(final ListIterator<E>
the list iterator to decorate
iterator
)

Create a new predicated list iterator.

Method Summary

Modifier and TypeMethod and Description
public void
add(final E
the element to insert
object
)

Overrides org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.add.

Implements java.util.ListIterator.add.

Inserts the specified element into the list (optional operation).
public void
set(final E
the element with which to replace the last element returned by next or previous
object
)

Overrides org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.set.

Implements java.util.ListIterator.set.

Replaces the last element returned by next or previous with the specified element (optional operation).
Inherited from org.apache.commons.collections4.iterators.AbstractListIteratorDecorator:
getListIteratorhasNexthasPreviousnextnextIndexpreviouspreviousIndexremove

Constructor Detail

PredicatedListIteratorback to summary
protected PredicatedListIterator(final ListIterator<E> iterator)

Create a new predicated list iterator.

Parameters
iterator:ListIterator<E>

the list iterator to decorate

Method Detail

addback to summary
public void add(final E object)

Overrides org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.add.

Implements java.util.ListIterator.add.

Doc from org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.add.

Inserts the specified element into the list (optional operation). The element is inserted immediately before the element that would be returned by next, if any, and after the element that would be returned by previous, if any. (If the list contains no elements, the new element becomes the sole element on the list.) The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected, and a subsequent call to previous would return the new element. (This call increases by one the value that would be returned by a call to nextIndex or previousIndex.)

Parameters
object:E

Doc from java.util.ListIterator.add.

the element to insert

Annotations
@Override
setback to summary
public void set(final E object)

Overrides org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.set.

Implements java.util.ListIterator.set.

Doc from org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.set.

Replaces the last element returned by next or previous with the specified element (optional operation). This call can be made only if neither remove nor add have been called after the last call to next or previous.

Parameters
object:E

Doc from java.util.ListIterator.set.

the element with which to replace the last element returned by next or previous

Annotations
@Override