View Javadoc

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 net.sf.collections15.list;
17  
18  import java.util.Collection;
19  import java.util.List;
20  import java.util.ListIterator;
21  
22  import net.sf.collections15.Predicate;
23  import net.sf.collections15.collection.PredicatedCollection;
24  import net.sf.collections15.iterators.AbstractListIteratorDecorator;
25  
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<String> list =
37   *        PredicatedList.<String>decorate(new ArrayList<String>(),
38   *                                        NotNullPredicate.<String>getInstance());</pre>
39   * <p>
40   * This class is Serializable from Commons Collections 3.1.
41   *
42   * @since Commons Collections 3.0
43   * @version $Revision: 1.2 $ $Date: 2005/05/25 21:25:25 $
44   * 
45   * @author Stephen Colebourne
46   * @author Paul Jack
47   */
48  public class PredicatedList<E> extends PredicatedCollection<E> implements List<E> {
49  
50      /*** Serialization version */
51  	private static final long serialVersionUID = 3256441391449258296L;
52  
53  	/***
54       * Factory method to create a predicated (validating) list.
55       * <p>
56       * If there are any elements already in the list being decorated, they
57       * are validated.
58       * 
59       * @param list  the list to decorate, must not be null
60       * @param predicate  the predicate to use for validation, must not be null
61       * @throws IllegalArgumentException if list or predicate is null
62       * @throws IllegalArgumentException if the list contains invalid elements
63       */
64      public static <T> List<T> decorate(List<T> list, Predicate<? super T> predicate) {
65          return new PredicatedList<T>(list, predicate);
66      }
67  
68      //-----------------------------------------------------------------------
69      /***
70       * Constructor that wraps (not copies).
71       * <p>
72       * If there are any elements already in the list being decorated, they
73       * are validated.
74       * 
75       * @param list  the list to decorate, must not be null
76       * @param predicate  the predicate to use for validation, must not be null
77       * @throws IllegalArgumentException if list or predicate is null
78       * @throws IllegalArgumentException if the list contains invalid elements
79       */
80      protected PredicatedList(List<E> list, Predicate<? super E> predicate) {
81          super(list, predicate);
82      }
83  
84      /***
85       * Gets the list being decorated.
86       * 
87       * @return the decorated list
88       */
89      protected List<E> getList() {
90          return (List<E>) getCollection();
91      }
92  
93      //-----------------------------------------------------------------------
94      public E get(int index) {
95          return getList().get(index);
96      }
97  
98      public int indexOf(Object object) {
99          return getList().indexOf(object);
100     }
101 
102     public int lastIndexOf(Object object) {
103         return getList().lastIndexOf(object);
104     }
105 
106     public E remove(int index) {
107         return getList().remove(index);
108     }
109 
110     //-----------------------------------------------------------------------
111     public void add(int index, E object) {
112         validate(object);
113         getList().add(index, object);
114     }
115 
116     public boolean addAll(int index, Collection<? extends E> coll) {
117         for( E object : coll ) {
118             validate(object);
119         }
120         return getList().addAll(index, coll);
121     }
122 
123     public ListIterator<E> listIterator() {
124         return listIterator(0);
125     }
126 
127     public ListIterator<E> listIterator(int i) {
128         return new PredicatedListIterator(getList().listIterator(i));
129     }
130 
131     public E set(int index, E object) {
132         validate(object);
133         return getList().set(index, object);
134     }
135 
136     public List<E> subList(int fromIndex, int toIndex) {
137         List<E> sub = getList().subList(fromIndex, toIndex);
138         return new PredicatedList<E>(sub, predicate);
139     }
140 
141     /***
142      * Inner class Iterator for the PredicatedList
143      */
144     protected class PredicatedListIterator extends AbstractListIteratorDecorator<E> {
145         
146         protected PredicatedListIterator(ListIterator<E> iterator) {
147             super(iterator);
148         }
149         
150         public void add(E object) {
151 			validate(object);
152             iterator.add(object);
153         }
154         
155         public void set(E object) {
156             validate(object);
157             iterator.set(object);
158         }
159     }
160 
161 }