-
Notifications
You must be signed in to change notification settings - Fork 107
/
ConcurrentNavigableSetNullSafe.java
327 lines (280 loc) · 10.3 KB
/
ConcurrentNavigableSetNullSafe.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
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
320
321
322
323
324
325
326
327
package com.cedarsoftware.util;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NavigableSet;
import java.util.SortedSet;
import java.util.UUID;
import java.util.concurrent.ConcurrentSkipListSet;
/**
* ConcurrentNavigableSetNullSafe is a thread-safe implementation of NavigableSet
* that allows null elements by using a unique sentinel value internally.
*
* @param <E> The type of elements maintained by this set
*
* @author John DeRegnaucourt (jdereg@gmail.com)
* <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.
*/
public class ConcurrentNavigableSetNullSafe<E> extends AbstractSet<E> implements NavigableSet<E> {
private final NavigableSet<Object> internalSet;
private final Comparator<? super E> originalComparator;
private static final String NULL_ELEMENT_SENTINEL = "null_" + UUID.randomUUID();
/**
* Constructs a new, empty ConcurrentNavigableSetNullSafe with natural ordering of its elements.
* All elements inserted must implement the Comparable interface.
*/
public ConcurrentNavigableSetNullSafe() {
// Use natural ordering
this.originalComparator = null;
Comparator<Object> comp = wrapComparator(null);
this.internalSet = new ConcurrentSkipListSet<>(comp);
}
/**
* Constructs a new, empty ConcurrentNavigableSetNullSafe with the specified comparator.
*
* @param comparator the comparator that will be used to order this set. If null, the natural
* ordering of the elements will be used.
*/
public ConcurrentNavigableSetNullSafe(Comparator<? super E> comparator) {
this.originalComparator = comparator;
Comparator<Object> comp = wrapComparator(comparator);
this.internalSet = new ConcurrentSkipListSet<>(comp);
}
/**
* Constructs a new ConcurrentNavigableSetNullSafe containing the elements in the specified collection.
*
* @param c the collection whose elements are to be placed into this set
* @throws NullPointerException if the specified collection is null
*/
public ConcurrentNavigableSetNullSafe(Collection<? extends E> c) {
// Use natural ordering
this.originalComparator = null;
Comparator<Object> comp = wrapComparator(null);
this.internalSet = new ConcurrentSkipListSet<>(comp);
this.addAll(c); // Ensure masking of null elements
}
/**
* Constructs a new ConcurrentNavigableSetNullSafe containing the elements in the specified collection,
* ordered according to the provided comparator.
*
* @param c the collection whose elements are to be placed into this set
* @param comparator the comparator that will be used to order this set. If null, the natural
* ordering of the elements will be used.
* @throws NullPointerException if the specified collection is null
*/
public ConcurrentNavigableSetNullSafe(Collection<? extends E> c, Comparator<? super E> comparator) {
this.originalComparator = comparator;
Comparator<Object> comp = wrapComparator(comparator);
this.internalSet = new ConcurrentSkipListSet<>(comp);
this.addAll(c); // Ensure masking of null elements
}
private ConcurrentNavigableSetNullSafe(NavigableSet<Object> internalSet, Comparator<? super E> comparator) {
this.internalSet = internalSet;
this.originalComparator = comparator;
}
/**
* Masks null elements with a sentinel value.
*
* @param element the element to mask
* @return the masked element
*/
private Object maskNull(E element) {
return element == null ? NULL_ELEMENT_SENTINEL : element;
}
/**
* Unmasks elements, converting the sentinel value back to null.
*
* @param maskedElement the masked element
* @return the unmasked element
*/
@SuppressWarnings("unchecked")
private E unmaskNull(Object maskedElement) {
return maskedElement == NULL_ELEMENT_SENTINEL ? null : (E) maskedElement;
}
/**
* Wraps the user-provided comparator to handle the sentinel value and ensure proper ordering of null elements.
*
* @param comparator the user-provided comparator
* @return a comparator that handles the sentinel value
*/
private Comparator<Object> wrapComparator(Comparator<? super E> comparator) {
return (o1, o2) -> {
// Handle the sentinel values
boolean o1IsNullSentinel = NULL_ELEMENT_SENTINEL.equals(o1);
boolean o2IsNullSentinel = NULL_ELEMENT_SENTINEL.equals(o2);
// Unmask the sentinels back to null
E e1 = o1IsNullSentinel ? null : (E) o1;
E e2 = o2IsNullSentinel ? null : (E) o2;
// Use the custom comparator if provided
if (comparator != null) {
return comparator.compare(e1, e2);
}
// Handle nulls with natural ordering
if (e1 == null && e2 == null) {
return 0;
}
if (e1 == null) {
return 1; // Nulls are considered greater in natural ordering
}
if (e2 == null) {
return -1;
}
// Both elements are non-null
return ((Comparable<E>) e1).compareTo(e2);
};
}
@Override
public Comparator<? super E> comparator() {
return originalComparator;
}
// Implement NavigableSet methods
@Override
public E lower(E e) {
Object masked = internalSet.lower(maskNull(e));
return unmaskNull(masked);
}
@Override
public E floor(E e) {
Object masked = internalSet.floor(maskNull(e));
return unmaskNull(masked);
}
@Override
public E ceiling(E e) {
Object masked = internalSet.ceiling(maskNull(e));
return unmaskNull(masked);
}
@Override
public E higher(E e) {
Object masked = internalSet.higher(maskNull(e));
return unmaskNull(masked);
}
@Override
public E pollFirst() {
Object masked = internalSet.pollFirst();
return unmaskNull(masked);
}
@Override
public E pollLast() {
Object masked = internalSet.pollLast();
return unmaskNull(masked);
}
@Override
public Iterator<E> iterator() {
Iterator<Object> it = internalSet.iterator();
return new Iterator<E>() {
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public E next() {
return unmaskNull(it.next());
}
@Override
public void remove() {
it.remove();
}
};
}
@Override
public NavigableSet<E> descendingSet() {
NavigableSet<Object> descendingInternalSet = internalSet.descendingSet();
return new ConcurrentNavigableSetNullSafe<>(descendingInternalSet, originalComparator);
}
@Override
public Iterator<E> descendingIterator() {
Iterator<Object> it = internalSet.descendingIterator();
return new Iterator<E>() {
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public E next() {
return unmaskNull(it.next());
}
@Override
public void remove() {
it.remove();
}
};
}
@Override
public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
Object maskedFrom = maskNull(fromElement);
Object maskedTo = maskNull(toElement);
NavigableSet<Object> subInternal = internalSet.subSet(maskedFrom, fromInclusive, maskedTo, toInclusive);
return new ConcurrentNavigableSetNullSafe<>(subInternal, originalComparator);
}
@Override
public NavigableSet<E> headSet(E toElement, boolean inclusive) {
NavigableSet<Object> headInternal = internalSet.headSet(maskNull(toElement), inclusive);
return new ConcurrentNavigableSetNullSafe<>((Collection<E>)headInternal, originalComparator);
}
@Override
public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
NavigableSet<Object> tailInternal = internalSet.tailSet(maskNull(fromElement), inclusive);
return new ConcurrentNavigableSetNullSafe<>((Collection<E>)tailInternal, originalComparator);
}
@Override
public SortedSet<E> subSet(E fromElement, E toElement) {
return subSet(fromElement, true, toElement, false);
}
@Override
public SortedSet<E> headSet(E toElement) {
return headSet(toElement, false);
}
@Override
public SortedSet<E> tailSet(E fromElement) {
return tailSet(fromElement, true);
}
@Override
public E first() {
Object masked = internalSet.first();
return unmaskNull(masked);
}
@Override
public E last() {
Object masked = internalSet.last();
return unmaskNull(masked);
}
// Implement Set methods
@Override
public int size() {
return internalSet.size();
}
@Override
public boolean isEmpty() {
return internalSet.isEmpty();
}
@Override
public boolean contains(Object o) {
return internalSet.contains(maskNull((E) o));
}
@Override
public boolean add(E e) {
return internalSet.add(maskNull(e));
}
@Override
public boolean remove(Object o) {
return internalSet.remove(maskNull((E) o));
}
@Override
public void clear() {
internalSet.clear();
}
}