-
Notifications
You must be signed in to change notification settings - Fork 104
/
CaseInsensitiveSet.java
245 lines (215 loc) · 5.71 KB
/
CaseInsensitiveSet.java
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
package com.cedarsoftware.util;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.ConcurrentSkipListSet;
import static com.cedarsoftware.util.StringUtilities.hashCodeIgnoreCase;
/**
* Implements a java.util.Set that will not utilize 'case' when comparing Strings
* contained within the Set. The set can be homogeneous or heterogeneous.
* If the CaseInsensitiveSet is iterated, when Strings are encountered, the original
* Strings are returned (retains case).
*
* @author John DeRegnaucourt ([email protected])
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
* 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
* <br><br>
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a>
* <br><br>
* 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.
*/
@SuppressWarnings("unchecked")
public class CaseInsensitiveSet<E> implements Set<E>
{
private final Map<E, Object> map;
public CaseInsensitiveSet() { map = new CaseInsensitiveMap<>(); }
public CaseInsensitiveSet(Collection<? extends E> collection)
{
if (collection instanceof ConcurrentSkipListSet || collection instanceof ConcurrentSet)
{
map = new CaseInsensitiveMap<>(new ConcurrentSkipListMap<>());
}
else if (collection instanceof SortedSet)
{
map = new CaseInsensitiveMap<>(new TreeMap<>());
}
else
{
map = new CaseInsensitiveMap<>(collection.size());
}
addAll(collection);
}
public CaseInsensitiveSet(Collection<? extends E> source, Map backingMap)
{
map = backingMap;
addAll(source);
}
public CaseInsensitiveSet(int initialCapacity)
{
map = new CaseInsensitiveMap<>(initialCapacity);
}
public CaseInsensitiveSet(int initialCapacity, float loadFactor)
{
map = new CaseInsensitiveMap<>(initialCapacity, loadFactor);
}
public int hashCode()
{
int hash = 0;
for (Object item : map.keySet())
{
if (item != null)
{
if (item instanceof String)
{
hash += hashCodeIgnoreCase((String)item);
}
else
{
hash += item.hashCode();
}
}
}
return hash;
}
public boolean equals(Object other)
{
if (other == this) return true;
if (!(other instanceof Set)) return false;
Set<E> that = (Set<E>) other;
return that.size()==size() && containsAll(that);
}
public int size()
{
return map.size();
}
public boolean isEmpty()
{
return map.isEmpty();
}
public boolean contains(Object o)
{
return map.containsKey(o);
}
public Iterator<E> iterator()
{
return map.keySet().iterator();
}
public Object[] toArray()
{
return map.keySet().toArray();
}
public <T> T[] toArray(T[] a)
{
return map.keySet().toArray(a);
}
public boolean add(E e)
{
int size = map.size();
map.put(e, e);
return map.size() != size;
}
public boolean remove(Object o)
{
int size = map.size();
map.remove(o);
return map.size() != size;
}
public boolean containsAll(Collection<?> c)
{
for (Object o : c)
{
if (!map.containsKey(o))
{
return false;
}
}
return true;
}
public boolean addAll(Collection<? extends E> c)
{
int size = map.size();
for (E elem : c)
{
map.put(elem, elem);
}
return map.size() != size;
}
public boolean retainAll(Collection<?> c)
{
Map other = new CaseInsensitiveMap();
for (Object o : c)
{
other.put(o, null);
}
Iterator<?> i = map.keySet().iterator();
int size = map.size();
while (i.hasNext())
{
Object elem = i.next();
if (!other.containsKey(elem))
{
i.remove();
}
}
return map.size() != size;
}
public boolean removeAll(Collection<?> c)
{
int size = map.size();
for (Object elem : c)
{
map.remove(elem);
}
return map.size() != size;
}
public void clear()
{
map.clear();
}
@Deprecated
public Set<E> minus(Iterable<E> removeMe)
{
for (Object me : removeMe)
{
remove(me);
}
return this;
}
@Deprecated
public Set<E> minus(E removeMe)
{
remove(removeMe);
return this;
}
@Deprecated
public Set<E> plus(Iterable<E> right)
{
for (E item : right)
{
add(item);
}
return this;
}
@Deprecated
public Set<E> plus(Object right)
{
add((E)right);
return this;
}
public String toString()
{
return map.keySet().toString();
}
}