001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.list;
017:
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.ListIterator;
022:
023: import org.apache.commons.collections.Predicate;
024: import org.apache.commons.collections.collection.PredicatedCollection;
025: import org.apache.commons.collections.iterators.AbstractListIteratorDecorator;
026:
027: /**
028: * Decorates another <code>List</code> to validate that all additions
029: * match a specified predicate.
030: * <p>
031: * This list exists to provide validation for the decorated list.
032: * It is normally created to decorate an empty list.
033: * If an object cannot be added to the list, an IllegalArgumentException is thrown.
034: * <p>
035: * One usage would be to ensure that no null entries are added to the list.
036: * <pre>List list = PredicatedList.decorate(new ArrayList(), NotNullPredicate.INSTANCE);</pre>
037: * <p>
038: * This class is Serializable from Commons Collections 3.1.
039: *
040: * @since Commons Collections 3.0
041: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
042: *
043: * @author Stephen Colebourne
044: * @author Paul Jack
045: */
046: public class PredicatedList extends PredicatedCollection implements
047: List {
048:
049: /** Serialization version */
050: private static final long serialVersionUID = -5722039223898659102L;
051:
052: /**
053: * Factory method to create a predicated (validating) list.
054: * <p>
055: * If there are any elements already in the list being decorated, they
056: * are validated.
057: *
058: * @param list the list to decorate, must not be null
059: * @param predicate the predicate to use for validation, must not be null
060: * @throws IllegalArgumentException if list or predicate is null
061: * @throws IllegalArgumentException if the list contains invalid elements
062: */
063: public static List decorate(List list, Predicate predicate) {
064: return new PredicatedList(list, predicate);
065: }
066:
067: //-----------------------------------------------------------------------
068: /**
069: * Constructor that wraps (not copies).
070: * <p>
071: * If there are any elements already in the list being decorated, they
072: * are validated.
073: *
074: * @param list the list to decorate, must not be null
075: * @param predicate the predicate to use for validation, must not be null
076: * @throws IllegalArgumentException if list or predicate is null
077: * @throws IllegalArgumentException if the list contains invalid elements
078: */
079: protected PredicatedList(List list, Predicate predicate) {
080: super (list, predicate);
081: }
082:
083: /**
084: * Gets the list being decorated.
085: *
086: * @return the decorated list
087: */
088: protected List getList() {
089: return (List) getCollection();
090: }
091:
092: //-----------------------------------------------------------------------
093: public Object get(int index) {
094: return getList().get(index);
095: }
096:
097: public int indexOf(Object object) {
098: return getList().indexOf(object);
099: }
100:
101: public int lastIndexOf(Object object) {
102: return getList().lastIndexOf(object);
103: }
104:
105: public Object remove(int index) {
106: return getList().remove(index);
107: }
108:
109: //-----------------------------------------------------------------------
110: public void add(int index, Object object) {
111: validate(object);
112: getList().add(index, object);
113: }
114:
115: public boolean addAll(int index, Collection coll) {
116: for (Iterator it = coll.iterator(); it.hasNext();) {
117: validate(it.next());
118: }
119: return getList().addAll(index, coll);
120: }
121:
122: public ListIterator listIterator() {
123: return listIterator(0);
124: }
125:
126: public ListIterator listIterator(int i) {
127: return new PredicatedListIterator(getList().listIterator(i));
128: }
129:
130: public Object set(int index, Object object) {
131: validate(object);
132: return getList().set(index, object);
133: }
134:
135: public List subList(int fromIndex, int toIndex) {
136: List sub = getList().subList(fromIndex, toIndex);
137: return new PredicatedList(sub, predicate);
138: }
139:
140: /**
141: * Inner class Iterator for the PredicatedList
142: */
143: protected class PredicatedListIterator extends
144: AbstractListIteratorDecorator {
145:
146: protected PredicatedListIterator(ListIterator iterator) {
147: super (iterator);
148: }
149:
150: public void add(Object object) {
151: validate(object);
152: iterator.add(object);
153: }
154:
155: public void set(Object object) {
156: validate(object);
157: iterator.set(object);
158: }
159: }
160:
161: }
|