My favorites
▼
|
Sign in
data-integrity-check
Runs testcases against databases to find data violating the integrity
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
src
/
main
/
java
/
de
/
fmaul
/
common
/
collect
/
IndexedList.java
‹r27
r41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package de.fmaul.common.collect;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.google.common.base.Function;
import com.google.common.collect.ForwardingList;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
/**
* This class provides a wrapper around a {@link List} and additionally provides
* an non unqiue index over the lists elements to provide constant access times
* like a Map or HashMap would. To achieve this a {@link HashMultimap} is
* automatically maintained whenever the list is altered. For each index key
* multiple elements (with the same key) can be retrieved.
*
* The Index is created by an index function with has to be provided and enables
* the index to operate on any field so the index is independent of the
* {@link #equals(Object)} and {@link #hashCode()} implementations.
*
* @author Florian Maul
* @param <E>
* the type of the list elements.
* @param <K>
* the type that will be used as index keys.
*/
public class IndexedList<E, K> extends ForwardingList<E> {
private final List<E> delegateList;
private final Function<E, K> indexFunction;
private final Multimap<K, E> indexMap = HashMultimap.create();
private final Multimap<K, E> readOnlyIndexMap = Multimaps
.unmodifiableMultimap(indexMap);
/**
* Creates a new {@link IndexedList} for a list and an index function.
*
* @param delegate
* The list all calls are forwarded to. After creating the
* {@link IndexedList} this should not be used directly anymore.
* @param indexFunction
* The function which converts list elements in index keys.
*/
public IndexedList(List<E> delegate, Function<E, K> indexFunction) {
delegateList = delegate;
this.indexFunction = indexFunction;
rebuildIndex();
}
/*
* (non-Javadoc)
*
* @see com.google.common.collect.ForwardingList#delegate()
*/
@Override
protected List<E> delegate() {
return delegateList;
}
/*
* (non-Javadoc)
*
* @see com.google.common.collect.ForwardingCollection#add(java.lang.Object)
*/
@Override
public boolean add(E e) {
boolean changed = super.add(e);
if (changed) {
indexElement(e);
}
return changed;
};
/**
* Puts the list element e into the index.
*
* @param e
* An element of the list.
*/
private void indexElement(E e) {
indexMap.put(indexFunction.apply(e), e);
}
/**
* Puts all elements of a collection into the index.
*
* @param collection
* A collection of list elements.
*/
private void indexElements(Collection<? extends E> collection) {
for (E element : collection) {
indexElement(element);
}
}
/*
* (non-Javadoc)
*
* @see
* com.google.common.collect.ForwardingCollection#addAll(java.util.Collection
* )
*/
@Override
public boolean addAll(Collection<? extends E> collection) {
boolean changed = super.addAll(collection);
if (changed) {
indexElements(collection);
}
return changed;
}
/*
* (non-Javadoc)
*
* @see com.google.common.collect.ForwardingList#add(int, java.lang.Object)
*/
public void add(int index, E element) {
super.add(index, element);
indexElement(element);
};
/*
* (non-Javadoc)
*
* @see com.google.common.collect.ForwardingList#addAll(int,
* java.util.Collection)
*/
@Override
public boolean addAll(int index, Collection<? extends E> elements) {
boolean changed = super.addAll(index, elements);
if (changed) {
indexElements(elements);
}
return changed;
}
/*
* (non-Javadoc)
*
* @see com.google.common.collect.ForwardingList#set(int, java.lang.Object)
*/
@Override
public E set(int index, E element) {
E oldElement = super.set(index, element);
if (oldElement != null && oldElement != element) {
removeIndex(oldElement);
indexElement(element);
}
return oldElement;
};
/**
* Removes an element from the index.
*
* @param element
* The element that is to be removed form the index.
*/
private void removeIndex(E element) {
indexMap.remove(indexFunction.apply(element), element);
}
/*
* (non-Javadoc)
*
* @see com.google.common.collect.ForwardingCollection#clear()
*/
@Override
public void clear() {
super.clear();
indexMap.clear();
}
/*
* (non-Javadoc)
*
* @see
* com.google.common.collect.ForwardingCollection#remove(java.lang.Object)
*/
@Override
@SuppressWarnings(value = "unchecked")
public boolean remove(Object object) {
boolean removed = super.remove(object);
if (removed) {
removeIndex((E) object);
}
return removed;
}
/*
* (non-Javadoc)
*
* @see
* com.google.common.collect.ForwardingCollection#removeAll(java.util.Collection
* )
*/
@Override
public boolean removeAll(Collection<?> collection) {
boolean removed = super.removeAll(collection);
if (removed) {
rebuildIndex();
}
return removed;
}
/**
* Clears the index and rebuilds it.
*/
private void rebuildIndex() {
indexMap.clear();
indexElements(delegateList);
}
/*
* (non-Javadoc)
*
* @see
* com.google.common.collect.ForwardingCollection#retainAll(java.util.Collection
* )
*/
@Override
public boolean retainAll(Collection<?> collection) {
boolean changed = super.retainAll(collection);
if (changed) {
rebuildIndex();
}
return changed;
}
/*
* (non-Javadoc)
*
* @see com.google.common.collect.ForwardingList#remove(int)
*/
@Override
public E remove(int index) {
E elementRemoved = super.remove(index);
if (elementRemoved != null) {
removeIndex(elementRemoved);
}
return elementRemoved;
}
/**
* Retrieves a collection of elements from the index for the specified key
*
* @param key
* The key that will be used to retrieve the elements from the
* index.
* @return a Collection of list elements for the index.
*/
public Collection<E> getByIndex(K indexKey) {
return indexMap.get(indexKey);
}
/**
* Checks whether the key is present in the index.
*
* @param key
* The key that is searched for in the index.
* @return true, if the key exists, else false.
*/
public boolean containsByIndex(K indexKey) {
return indexMap.containsKey(indexKey);
}
/**
* Returns the whole index as a {@link Multimap}. This map is a unmodifyable
* map, i.e. it is read only.
*
* @return A {@link Multimap} with the current index.
*/
public Multimap<K, E> getIndex() {
return readOnlyIndexMap;
}
/**
* A static factory as convenience method to easily create an
* {@link IndexedList} for a {@link List}.
*
* @param <E>
* The type of the list elements.
* @param <K>
* The type of the key that is used for the index.
* @param delegate
* The list all the calls are delegated to.
* @param indexFunction
* The index function that is used to generate the keys for the
* index.
* @return A new {@link IndexedList} that forwards all operations to the
* delegate list and maintains an index.
*/
public static <E, K> IndexedList<E, K> create(List<E> delegate,
Function<E, K> indexFunction) {
return new IndexedList<E, K>(delegate, indexFunction);
}
/**
* A static factory as convenience method to easily create a new and empty
* {@link IndexedList}.
*
* @param <E>
* The type of the list elements.
* @param <K>
* The type of the key that is used for the index.
* @param indexFunction
* The index function that is used to generate the keys for the
* index.
* @return A new {@link IndexedList} that internally is linked to a new
* empty {@link ArrayList} for which an index is maintained.
*/
public static <E, K> IndexedList<E, K> create(Function<E, K> indexFunction) {
List<E> newList = Lists.newArrayList();
return new IndexedList<E, K>(newList, indexFunction);
}
}
Show details
Hide details
Change log
r34
by florian.maul on Jan 15, 2010
Diff
update to latest libraries
Go to:
/trunk/pom.xml
.../common/collect/IndexedList.java
Project members,
sign in
to write a code review
Older revisions
r27
by Florian.Maul on May 12, 2009
Diff
IndexedList implementations
All revisions of this file
File info
Size: 9419 bytes, 319 lines
View raw file
Powered by
Google Project Hosting