KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > list > PredicatedList


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.list;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.ListIterator JavaDoc;
22
23 import org.apache.commons.collections.Predicate;
24 import org.apache.commons.collections.collection.PredicatedCollection;
25 import org.apache.commons.collections.iterators.AbstractListIteratorDecorator;
26
27 /**
28  * Decorates another <code>List</code> to validate that all additions
29  * match a specified predicate.
30  * <p>
31  * This list exists to provide validation for the decorated list.
32  * It is normally created to decorate an empty list.
33  * If an object cannot be added to the list, an IllegalArgumentException is thrown.
34  * <p>
35  * One usage would be to ensure that no null entries are added to the list.
36  * <pre>List list = PredicatedList.decorate(new ArrayList(), NotNullPredicate.INSTANCE);</pre>
37  * <p>
38  * This class is Serializable from Commons Collections 3.1.
39  *
40  * @since Commons Collections 3.0
41  * @version $Revision: 1.6 $ $Date: 2004/06/03 22:02:13 $
42  *
43  * @author Stephen Colebourne
44  * @author Paul Jack
45  */

46 public class PredicatedList extends PredicatedCollection implements List JavaDoc {
47
48     /** Serialization version */
49     private static final long serialVersionUID = -5722039223898659102L;
50
51     /**
52      * Factory method to create a predicated (validating) list.
53      * <p>
54      * If there are any elements already in the list being decorated, they
55      * are validated.
56      *
57      * @param list the list to decorate, must not be null
58      * @param predicate the predicate to use for validation, must not be null
59      * @throws IllegalArgumentException if list or predicate is null
60      * @throws IllegalArgumentException if the list contains invalid elements
61      */

62     public static List JavaDoc decorate(List JavaDoc list, Predicate predicate) {
63         return new PredicatedList(list, predicate);
64     }
65
66     //-----------------------------------------------------------------------
67
/**
68      * Constructor that wraps (not copies).
69      * <p>
70      * If there are any elements already in the list being decorated, they
71      * are validated.
72      *
73      * @param list the list to decorate, must not be null
74      * @param predicate the predicate to use for validation, must not be null
75      * @throws IllegalArgumentException if list or predicate is null
76      * @throws IllegalArgumentException if the list contains invalid elements
77      */

78     protected PredicatedList(List JavaDoc list, Predicate predicate) {
79         super(list, predicate);
80     }
81
82     /**
83      * Gets the list being decorated.
84      *
85      * @return the decorated list
86      */

87     protected List JavaDoc getList() {
88         return (List JavaDoc) getCollection();
89     }
90
91     //-----------------------------------------------------------------------
92
public Object JavaDoc get(int index) {
93         return getList().get(index);
94     }
95
96     public int indexOf(Object JavaDoc object) {
97         return getList().indexOf(object);
98     }
99
100     public int lastIndexOf(Object JavaDoc object) {
101         return getList().lastIndexOf(object);
102     }
103
104     public Object JavaDoc remove(int index) {
105         return getList().remove(index);
106     }
107
108     //-----------------------------------------------------------------------
109
public void add(int index, Object JavaDoc object) {
110         validate(object);
111         getList().add(index, object);
112     }
113
114     public boolean addAll(int index, Collection JavaDoc coll) {
115         for (Iterator JavaDoc it = coll.iterator(); it.hasNext(); ) {
116             validate(it.next());
117         }
118         return getList().addAll(index, coll);
119     }
120
121     public ListIterator JavaDoc listIterator() {
122         return listIterator(0);
123     }
124
125     public ListIterator JavaDoc listIterator(int i) {
126         return new PredicatedListIterator(getList().listIterator(i));
127     }
128
129     public Object JavaDoc set(int index, Object JavaDoc object) {
130         validate(object);
131         return getList().set(index, object);
132     }
133
134     public List JavaDoc subList(int fromIndex, int toIndex) {
135         List JavaDoc sub = getList().subList(fromIndex, toIndex);
136         return new PredicatedList(sub, predicate);
137     }
138
139     /**
140      * Inner class Iterator for the PredicatedList
141      */

142     protected class PredicatedListIterator extends AbstractListIteratorDecorator {
143         
144         protected PredicatedListIterator(ListIterator JavaDoc iterator) {
145             super(iterator);
146         }
147         
148         public void add(Object JavaDoc object) {
149             validate(object);
150             iterator.add(object);
151         }
152         
153         public void set(Object JavaDoc object) {
154             validate(object);
155             iterator.set(object);
156         }
157     }
158
159 }
160
Popular Tags