1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.list;
18
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.ListIterator;
22
23 import org.apache.commons.collections4.Predicate;
24 import org.apache.commons.collections4.collection.PredicatedCollection;
25 import org.apache.commons.collections4.iterators.AbstractListIteratorDecorator;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class PredicatedList<E> extends PredicatedCollection<E> implements List<E> {
51
52
53
54
55 protected class PredicatedListIterator extends AbstractListIteratorDecorator<E> {
56
57
58
59
60
61
62 protected PredicatedListIterator(final ListIterator<E> iterator) {
63 super(iterator);
64 }
65
66 @Override
67 public void add(final E object) {
68 validate(object);
69 getListIterator().add(object);
70 }
71
72 @Override
73 public void set(final E object) {
74 validate(object);
75 getListIterator().set(object);
76 }
77 }
78
79
80 private static final long serialVersionUID = -5722039223898659102L;
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 public static <T> PredicatedList<T> predicatedList(final List<T> list, final Predicate<? super T> predicate) {
97 return new PredicatedList<>(list, predicate);
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111 protected PredicatedList(final List<E> list, final Predicate<? super E> predicate) {
112 super(list, predicate);
113 }
114
115 @Override
116 public void add(final int index, final E object) {
117 validate(object);
118 decorated().add(index, object);
119 }
120
121 @Override
122 public boolean addAll(final int index, final Collection<? extends E> coll) {
123 for (final E aColl : coll) {
124 validate(aColl);
125 }
126 return decorated().addAll(index, coll);
127 }
128
129
130
131
132
133
134 @Override
135 protected List<E> decorated() {
136 return (List<E>) super.decorated();
137 }
138
139 @Override
140 public boolean equals(final Object object) {
141 return object == this || decorated().equals(object);
142 }
143
144 @Override
145 public E get(final int index) {
146 return decorated().get(index);
147 }
148
149 @Override
150 public int hashCode() {
151 return decorated().hashCode();
152 }
153
154 @Override
155 public int indexOf(final Object object) {
156 return decorated().indexOf(object);
157 }
158
159 @Override
160 public int lastIndexOf(final Object object) {
161 return decorated().lastIndexOf(object);
162 }
163
164 @Override
165 public ListIterator<E> listIterator() {
166 return listIterator(0);
167 }
168
169 @Override
170 public ListIterator<E> listIterator(final int i) {
171 return new PredicatedListIterator(decorated().listIterator(i));
172 }
173
174 @Override
175 public E remove(final int index) {
176 return decorated().remove(index);
177 }
178
179 @Override
180 public E set(final int index, final E object) {
181 validate(object);
182 return decorated().set(index, object);
183 }
184
185 @Override
186 public List<E> subList(final int fromIndex, final int toIndex) {
187 final List<E> sub = decorated().subList(fromIndex, toIndex);
188 return new PredicatedList<>(sub, predicate);
189 }
190
191 }