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
|
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.data.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
/**
* ListSet is an internal Vaadin class which implements a combination of a List
* and a Set. The main purpose of this class is to provide a list with a fast
* {@link #contains(Object)} method. Each inserted object must by unique (as
* specified by {@link #equals(Object)}). The {@link #set(int, Object)} method
* allows duplicates because of the way {@link Collections#sort(java.util.List)}
* works.
*
* This class is subject to change and should not be used outside Vaadin core.
*/
public class ListSet<E> extends ArrayList<E> {
private HashSet<E> itemSet = null;
/**
* Contains a map from an element to the number of duplicates it has. Used
* to temporarily allow duplicates in the list.
*/
private HashMap<E, Integer> duplicates = new HashMap<E, Integer>();
public ListSet() {
super();
itemSet = new HashSet<E>();
}
public ListSet(Collection<? extends E> c) {
super(c);
itemSet = new HashSet<E>(c.size());
itemSet.addAll(c);
}
public ListSet(int initialCapacity) {
super(initialCapacity);
itemSet = new HashSet<E>(initialCapacity);
}
// Delegate contains operations to the set
@Override
public boolean contains(Object o) {
return itemSet.contains(o);
}
@Override
public boolean containsAll(Collection<?> c) {
return itemSet.containsAll(c);
}
// Methods for updating the set when the list is updated.
@Override
public boolean add(E e) {
if (contains(e)) {
// Duplicates are not allowed
return false;
}
if (super.add(e)) {
itemSet.add(e);
return true;
} else {
return false;
}
}
/**
* Works as java.util.ArrayList#add(int, java.lang.Object) but returns
* immediately if the element is already in the ListSet.
*/
@Override
public void add(int index, E element) {
if (contains(element)) {
// Duplicates are not allowed
return;
}
super.add(index, element);
itemSet.add(element);
}
@Override
public boolean addAll(Collection<? extends E> c) {
boolean modified = false;
Iterator<? extends E> i = c.iterator();
while (i.hasNext()) {
E e = i.next();
if (contains(e)) {
continue;
}
if (add(e)) {
itemSet.add(e);
modified = true;
}
}
return modified;
}
@Override
public boolean addAll(int index, Collection<? extends E> c) {
ensureCapacity(size() + c.size());
boolean modified = false;
Iterator<? extends E> i = c.iterator();
while (i.hasNext()) {
E e = i.next();
if (contains(e)) {
continue;
}
add(index++, e);
itemSet.add(e);
modified = true;
}
return modified;
}
@Override
public void clear() {
super.clear();
itemSet.clear();
}
@Override
public int indexOf(Object o) {
if (!contains(o)) {
return -1;
}
return super.indexOf(o);
}
@Override
public int lastIndexOf(Object o) {
if (!contains(o)) {
return -1;
}
return super.lastIndexOf(o);
}
@Override
public E remove(int index) {
E e = super.remove(index);
if (e != null) {
itemSet.remove(e);
}
return e;
}
@Override
public boolean remove(Object o) {
if (super.remove(o)) {
itemSet.remove(o);
return true;
} else {
return false;
}
}
@Override
protected void removeRange(int fromIndex, int toIndex) {
HashSet<E> toRemove = new HashSet<E>();
for (int idx = fromIndex; idx < toIndex; idx++) {
toRemove.add(get(idx));
}
super.removeRange(fromIndex, toIndex);
itemSet.removeAll(toRemove);
}
@Override
public E set(int index, E element) {
if (contains(element)) {
// Element already exist in the list
if (get(index) == element) {
// At the same position, nothing to be done
return element;
} else {
// Adding at another position. We assume this is a sort
// operation and temporarily allow it.
// We could just remove (null) the old element and keep the list
// unique. This would require finding the index of the old
// element (indexOf(element)) which is not a fast operation in a
// list. So we instead allow duplicates temporarily.
addDuplicate(element);
}
}
E old = super.set(index, element);
removeFromSet(old);
itemSet.add(element);
return old;
}
/**
* Removes "e" from the set if it no longer exists in the list.
*
* @param e
*/
private void removeFromSet(E e) {
Integer dupl = duplicates.get(e);
if (dupl != null) {
// A duplicate was present so we only decrement the duplicate count
// and continue
if (dupl == 1) {
// This is what always should happen. A sort sets the items one
// by one, temporarily breaking the uniqueness requirement.
duplicates.remove(e);
} else {
duplicates.put(e, dupl - 1);
}
} else {
// The "old" value is no longer in the list.
itemSet.remove(e);
}
}
/**
* Marks the "element" can be found more than once from the list. Allowed in
* {@link #set(int, Object)} to make sorting work.
*
* @param element
*/
private void addDuplicate(E element) {
Integer nr = duplicates.get(element);
if (nr == null) {
nr = 1;
} else {
nr++;
}
/*
* Store the number of duplicates of this element so we know later on if
* we should remove an element from the set or if it was a duplicate (in
* removeFromSet)
*/
duplicates.put(element, nr);
}
@SuppressWarnings("unchecked")
@Override
public Object clone() {
ListSet<E> v = (ListSet<E>) super.clone();
v.itemSet = new HashSet<E>(itemSet);
return v;
}
}
|