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
51 public class PredicatedList<E> extends PredicatedCollection<E> implements List<E> {
52
53
54
55
56 protected class PredicatedListIterator extends AbstractListIteratorDecorator<E> {
57
58
59
60
61
62
63 protected PredicatedListIterator(final ListIterator<E> iterator) {
64 super(iterator);
65 }
66
67 @Override
68 public void add(final E object) {
69 validate(object);
70 getListIterator().add(object);
71 }
72
73 @Override
74 public void set(final E object) {
75 validate(object);
76 getListIterator().set(object);
77 }
78 }
79
80
81 private static final long serialVersionUID = -5722039223898659102L;
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 public static <T> PredicatedList<T> predicatedList(final List<T> list, final Predicate<? super T> predicate) {
98 return new PredicatedList<>(list, predicate);
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112 protected PredicatedList(final List<E> list, final Predicate<? super E> predicate) {
113 super(list, predicate);
114 }
115
116 @Override
117 public void add(final int index, final E object) {
118 validate(object);
119 decorated().add(index, object);
120 }
121
122 @Override
123 public boolean addAll(final int index, final Collection<? extends E> coll) {
124 for (final E aColl : coll) {
125 validate(aColl);
126 }
127 return decorated().addAll(index, coll);
128 }
129
130
131
132
133
134
135 @Override
136 protected List<E> decorated() {
137 return (List<E>) super.decorated();
138 }
139
140 @Override
141 public boolean equals(final Object object) {
142 return object == this || decorated().equals(object);
143 }
144
145 @Override
146 public E get(final int index) {
147 return decorated().get(index);
148 }
149
150 @Override
151 public int hashCode() {
152 return decorated().hashCode();
153 }
154
155 @Override
156 public int indexOf(final Object object) {
157 return decorated().indexOf(object);
158 }
159
160 @Override
161 public int lastIndexOf(final Object object) {
162 return decorated().lastIndexOf(object);
163 }
164
165 @Override
166 public ListIterator<E> listIterator() {
167 return listIterator(0);
168 }
169
170 @Override
171 public ListIterator<E> listIterator(final int i) {
172 return new PredicatedListIterator(decorated().listIterator(i));
173 }
174
175 @Override
176 public E remove(final int index) {
177 return decorated().remove(index);
178 }
179
180 @Override
181 public E set(final int index, final E object) {
182 validate(object);
183 return decorated().set(index, object);
184 }
185
186 @Override
187 public List<E> subList(final int fromIndex, final int toIndex) {
188 final List<E> sub = decorated().subList(fromIndex, toIndex);
189 return new PredicatedList<>(sub, predicate);
190 }
191
192 }