-
Notifications
You must be signed in to change notification settings - Fork 584
/
AbstractImmutableSortedSet.java
512 lines (451 loc) · 19.4 KB
/
AbstractImmutableSortedSet.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.set.sorted.immutable;
import java.util.Collection;
import java.util.Comparator;
import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;
import java.util.concurrent.ExecutorService;
import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.primitive.BooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ByteFunction;
import org.eclipse.collections.api.block.function.primitive.CharFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.function.primitive.ShortFunction;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.collection.MutableCollection;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.factory.SortedSets;
import org.eclipse.collections.api.factory.primitive.BooleanLists;
import org.eclipse.collections.api.factory.primitive.ByteLists;
import org.eclipse.collections.api.factory.primitive.CharLists;
import org.eclipse.collections.api.factory.primitive.DoubleLists;
import org.eclipse.collections.api.factory.primitive.FloatLists;
import org.eclipse.collections.api.factory.primitive.IntLists;
import org.eclipse.collections.api.factory.primitive.LongLists;
import org.eclipse.collections.api.factory.primitive.ShortLists;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.list.primitive.ImmutableBooleanList;
import org.eclipse.collections.api.list.primitive.ImmutableByteList;
import org.eclipse.collections.api.list.primitive.ImmutableCharList;
import org.eclipse.collections.api.list.primitive.ImmutableDoubleList;
import org.eclipse.collections.api.list.primitive.ImmutableFloatList;
import org.eclipse.collections.api.list.primitive.ImmutableIntList;
import org.eclipse.collections.api.list.primitive.ImmutableLongList;
import org.eclipse.collections.api.list.primitive.ImmutableShortList;
import org.eclipse.collections.api.list.primitive.MutableBooleanList;
import org.eclipse.collections.api.list.primitive.MutableByteList;
import org.eclipse.collections.api.list.primitive.MutableCharList;
import org.eclipse.collections.api.list.primitive.MutableDoubleList;
import org.eclipse.collections.api.list.primitive.MutableFloatList;
import org.eclipse.collections.api.list.primitive.MutableIntList;
import org.eclipse.collections.api.list.primitive.MutableLongList;
import org.eclipse.collections.api.list.primitive.MutableShortList;
import org.eclipse.collections.api.multimap.sortedset.ImmutableSortedSetMultimap;
import org.eclipse.collections.api.ordered.OrderedIterable;
import org.eclipse.collections.api.partition.set.sorted.PartitionImmutableSortedSet;
import org.eclipse.collections.api.partition.set.sorted.PartitionMutableSortedSet;
import org.eclipse.collections.api.set.SetIterable;
import org.eclipse.collections.api.set.sorted.ImmutableSortedSet;
import org.eclipse.collections.api.set.sorted.MutableSortedSet;
import org.eclipse.collections.api.set.sorted.ParallelSortedSetIterable;
import org.eclipse.collections.api.set.sorted.SortedSetIterable;
import org.eclipse.collections.api.stack.MutableStack;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.factory.Comparators;
import org.eclipse.collections.impl.block.factory.Functions;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.block.procedure.CollectIfProcedure;
import org.eclipse.collections.impl.block.procedure.CollectProcedure;
import org.eclipse.collections.impl.block.procedure.FlatCollectProcedure;
import org.eclipse.collections.impl.block.procedure.PartitionPredicate2Procedure;
import org.eclipse.collections.impl.block.procedure.PartitionProcedure;
import org.eclipse.collections.impl.block.procedure.RejectProcedure;
import org.eclipse.collections.impl.block.procedure.SelectInstancesOfProcedure;
import org.eclipse.collections.impl.block.procedure.SelectProcedure;
import org.eclipse.collections.impl.collection.immutable.AbstractImmutableCollection;
import org.eclipse.collections.impl.lazy.parallel.set.sorted.NonParallelSortedSetIterable;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.multimap.set.sorted.TreeSortedSetMultimap;
import org.eclipse.collections.impl.partition.set.sorted.PartitionTreeSortedSet;
import org.eclipse.collections.impl.set.sorted.mutable.TreeSortedSet;
import org.eclipse.collections.impl.stack.mutable.ArrayStack;
import org.eclipse.collections.impl.utility.Iterate;
import org.eclipse.collections.impl.utility.OrderedIterate;
import org.eclipse.collections.impl.utility.internal.IterableIterate;
import org.eclipse.collections.impl.utility.internal.SetIterables;
import org.eclipse.collections.impl.utility.internal.SortedSetIterables;
/**
* This class is the parent class for all ImmutableSortedSets. All implementations of ImmutableSortedSet must implement the SortedSet
* interface so an TreeSet.equals(anImmutableSortedSet) can return true when the contents are the same.
*/
abstract class AbstractImmutableSortedSet<T> extends AbstractImmutableCollection<T>
implements ImmutableSortedSet<T>, SortedSet<T>
{
@Override
public SortedSet<T> castToSortedSet()
{
return this;
}
@Override
public ImmutableSortedSet<T> newWith(T element)
{
if (!this.contains(element))
{
return SortedSets.mutable.withAll(this).with(element).toImmutable();
}
return this;
}
@Override
public ImmutableSortedSet<T> newWithout(T element)
{
if (this.contains(element))
{
TreeSortedSet<T> result = TreeSortedSet.newSet(this);
result.remove(element);
return result.toImmutable();
}
return this;
}
@Override
public ImmutableSortedSet<T> newWithAll(Iterable<? extends T> elements)
{
TreeSortedSet<T> result = TreeSortedSet.newSet(this);
result.addAllIterable(elements);
return result.toImmutable();
}
@Override
public ImmutableSortedSet<T> newWithoutAll(Iterable<? extends T> elements)
{
TreeSortedSet<T> result = TreeSortedSet.newSet(this);
this.removeAllFrom(elements, result);
return result.toImmutable();
}
@Override
public T getFirst()
{
return this.first();
}
@Override
public T getLast()
{
return this.last();
}
@Override
protected MutableCollection<T> newMutable(int size)
{
return TreeSortedSet.newSet(this.comparator());
}
@Override
public ImmutableBooleanList collectBoolean(BooleanFunction<? super T> booleanFunction)
{
MutableBooleanList result = BooleanLists.mutable.withInitialCapacity(this.size());
return this.collectBoolean(booleanFunction, result).toImmutable();
}
@Override
public ImmutableByteList collectByte(ByteFunction<? super T> byteFunction)
{
MutableByteList result = ByteLists.mutable.withInitialCapacity(this.size());
return this.collectByte(byteFunction, result).toImmutable();
}
@Override
public ImmutableCharList collectChar(CharFunction<? super T> charFunction)
{
MutableCharList result = CharLists.mutable.withInitialCapacity(this.size());
return this.collectChar(charFunction, result).toImmutable();
}
@Override
public ImmutableDoubleList collectDouble(DoubleFunction<? super T> doubleFunction)
{
MutableDoubleList result = DoubleLists.mutable.withInitialCapacity(this.size());
return this.collectDouble(doubleFunction, result).toImmutable();
}
@Override
public ImmutableFloatList collectFloat(FloatFunction<? super T> floatFunction)
{
MutableFloatList result = FloatLists.mutable.withInitialCapacity(this.size());
return this.collectFloat(floatFunction, result).toImmutable();
}
@Override
public ImmutableIntList collectInt(IntFunction<? super T> intFunction)
{
MutableIntList result = IntLists.mutable.withInitialCapacity(this.size());
return this.collectInt(intFunction, result).toImmutable();
}
@Override
public ImmutableLongList collectLong(LongFunction<? super T> longFunction)
{
MutableLongList result = LongLists.mutable.withInitialCapacity(this.size());
return this.collectLong(longFunction, result).toImmutable();
}
@Override
public ImmutableShortList collectShort(ShortFunction<? super T> shortFunction)
{
MutableShortList result = ShortLists.mutable.withInitialCapacity(this.size());
return this.collectShort(shortFunction, result).toImmutable();
}
@Override
public ImmutableSortedSet<T> tap(Procedure<? super T> procedure)
{
this.forEach(procedure);
return this;
}
@Override
public <S> boolean corresponds(OrderedIterable<S> other, Predicate2<? super T, ? super S> predicate)
{
return OrderedIterate.corresponds(this, other, predicate);
}
@Override
public ImmutableSortedSet<T> select(Predicate<? super T> predicate)
{
TreeSortedSet<T> result = TreeSortedSet.newSet(this.comparator());
this.forEach(new SelectProcedure<>(predicate, result));
return result.toImmutable();
}
@Override
public <P> ImmutableSortedSet<T> selectWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return this.select(Predicates.bind(predicate, parameter));
}
@Override
public ImmutableSortedSet<T> reject(Predicate<? super T> predicate)
{
TreeSortedSet<T> result = TreeSortedSet.newSet(this.comparator());
this.forEach(new RejectProcedure<>(predicate, result));
return result.toImmutable();
}
@Override
public <P> ImmutableSortedSet<T> rejectWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
return this.reject(Predicates.bind(predicate, parameter));
}
@Override
public PartitionImmutableSortedSet<T> partition(Predicate<? super T> predicate)
{
PartitionMutableSortedSet<T> partitionTreeSortedSet = new PartitionTreeSortedSet<>(this.comparator());
this.forEach(new PartitionProcedure<>(predicate, partitionTreeSortedSet));
return partitionTreeSortedSet.toImmutable();
}
@Override
public <P> PartitionImmutableSortedSet<T> partitionWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
PartitionMutableSortedSet<T> partitionTreeSortedSet = new PartitionTreeSortedSet<>(this.comparator());
this.forEach(new PartitionPredicate2Procedure<>(predicate, parameter, partitionTreeSortedSet));
return partitionTreeSortedSet.toImmutable();
}
@Override
public PartitionImmutableSortedSet<T> partitionWhile(Predicate<? super T> predicate)
{
PartitionTreeSortedSet<T> result = new PartitionTreeSortedSet<>(this.comparator());
return IterableIterate.partitionWhile(this, predicate, result).toImmutable();
}
@Override
public <S> ImmutableSortedSet<S> selectInstancesOf(Class<S> clazz)
{
TreeSortedSet<S> result = TreeSortedSet.newSet((Comparator<? super S>) this.comparator());
this.forEach(new SelectInstancesOfProcedure<>(clazz, result));
return result.toImmutable();
}
@Override
public <V> ImmutableList<V> collect(Function<? super T, ? extends V> function)
{
MutableList<V> result = Lists.mutable.empty();
this.forEach(new CollectProcedure<>(function, result));
return result.toImmutable();
}
@Override
public <P, V> ImmutableList<V> collectWith(Function2<? super T, ? super P, ? extends V> function, P parameter)
{
return this.collect(Functions.bind(function, parameter));
}
@Override
public <V> ImmutableList<V> collectIf(Predicate<? super T> predicate, Function<? super T, ? extends V> function)
{
MutableList<V> result = Lists.mutable.empty();
this.forEach(new CollectIfProcedure<>(result, function, predicate));
return result.toImmutable();
}
@Override
public <V> ImmutableList<V> flatCollect(Function<? super T, ? extends Iterable<V>> function)
{
MutableList<V> result = Lists.mutable.empty();
this.forEach(new FlatCollectProcedure<>(function, result));
return result.toImmutable();
}
@Override
public int detectIndex(Predicate<? super T> predicate)
{
return Iterate.detectIndex(this, predicate);
}
@Override
public <V> ImmutableSortedSetMultimap<V, T> groupBy(Function<? super T, ? extends V> function)
{
return this.groupBy(function, TreeSortedSetMultimap.<V, T>newMultimap(this.comparator())).toImmutable();
}
@Override
public <V> ImmutableSortedSetMultimap<V, T> groupByEach(Function<? super T, ? extends Iterable<V>> function)
{
return this.groupByEach(function, TreeSortedSetMultimap.newMultimap(this.comparator())).toImmutable();
}
@Override
public <S> ImmutableList<Pair<T, S>> zip(Iterable<S> that)
{
if (that instanceof Collection || that instanceof RichIterable)
{
int thatSize = Iterate.sizeOf(that);
FastList<Pair<T, S>> target = FastList.newList(Math.min(this.size(), thatSize));
return Iterate.zip(this, that, target).toImmutable();
}
return Iterate.zip(this, that, FastList.newList()).toImmutable();
}
@Override
public ImmutableSortedSet<Pair<T, Integer>> zipWithIndex()
{
Comparator<? super T> comparator = this.comparator();
if (comparator == null)
{
TreeSortedSet<Pair<T, Integer>> pairs = TreeSortedSet.newSet(Comparators.byFunction(Functions.firstOfPair(), Comparators.naturalOrder()));
return Iterate.zipWithIndex(this, pairs).toImmutable();
}
return Iterate.zipWithIndex(this, TreeSortedSet.newSet(Comparators.byFirstOfPair(comparator))).toImmutable();
}
@Override
public ImmutableSortedSet<T> distinct()
{
return this;
}
@Override
public ImmutableSortedSet<T> takeWhile(Predicate<? super T> predicate)
{
MutableSortedSet<T> result = TreeSortedSet.newSet(this.comparator());
return IterableIterate.takeWhile(this, predicate, result).toImmutable();
}
@Override
public ImmutableSortedSet<T> dropWhile(Predicate<? super T> predicate)
{
MutableSortedSet<T> result = TreeSortedSet.newSet(this.comparator());
return IterableIterate.dropWhile(this, predicate, result).toImmutable();
}
@Override
public MutableStack<T> toStack()
{
return ArrayStack.newStack(this);
}
@Override
public ImmutableSortedSet<T> union(SetIterable<? extends T> set)
{
return SetIterables.unionInto(this, set, TreeSortedSet.newSet(this.comparator())).toImmutable();
}
@Override
public <R extends Set<T>> R unionInto(SetIterable<? extends T> set, R targetSet)
{
return SetIterables.unionInto(this, set, targetSet);
}
@Override
public ImmutableSortedSet<T> intersect(SetIterable<? extends T> set)
{
return SetIterables.intersectInto(this, set, TreeSortedSet.newSet(this.comparator())).toImmutable();
}
@Override
public <R extends Set<T>> R intersectInto(SetIterable<? extends T> set, R targetSet)
{
return SetIterables.intersectInto(this, set, targetSet);
}
@Override
public ImmutableSortedSet<T> difference(SetIterable<? extends T> subtrahendSet)
{
return SetIterables.differenceInto(this, subtrahendSet, TreeSortedSet.newSet(this.comparator())).toImmutable();
}
@Override
public <R extends Set<T>> R differenceInto(SetIterable<? extends T> subtrahendSet, R targetSet)
{
return SetIterables.differenceInto(this, subtrahendSet, targetSet);
}
@Override
public ImmutableSortedSet<T> symmetricDifference(SetIterable<? extends T> setB)
{
return SetIterables.symmetricDifferenceInto(this, setB, TreeSortedSet.newSet(this.comparator())).toImmutable();
}
@Override
public <R extends Set<T>> R symmetricDifferenceInto(SetIterable<? extends T> set, R targetSet)
{
return SetIterables.symmetricDifferenceInto(this, set, targetSet);
}
@Override
public boolean isSubsetOf(SetIterable<? extends T> candidateSuperset)
{
return SetIterables.isSubsetOf(this, candidateSuperset);
}
@Override
public boolean isProperSubsetOf(SetIterable<? extends T> candidateSuperset)
{
return SetIterables.isProperSubsetOf(this, candidateSuperset);
}
@Override
public ImmutableSortedSet<SortedSetIterable<T>> powerSet()
{
return (ImmutableSortedSet<SortedSetIterable<T>>) (ImmutableSortedSet<?>) SortedSetIterables.immutablePowerSet(this);
}
@Override
public <B> LazyIterable<Pair<T, B>> cartesianProduct(SetIterable<B> set)
{
return SetIterables.cartesianProduct(this, set);
}
@Override
public SortedSet<T> subSet(T fromElement, T toElement)
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".subSet() not implemented yet");
}
@Override
public SortedSet<T> headSet(T toElement)
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".headSet() not implemented yet");
}
@Override
public SortedSet<T> tailSet(T fromElement)
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".tailSet() not implemented yet");
}
@Override
public ImmutableSortedSet<T> toImmutable()
{
return this;
}
@Override
public ParallelSortedSetIterable<T> asParallel(ExecutorService executorService, int batchSize)
{
Objects.requireNonNull(executorService);
if (batchSize < 1)
{
throw new IllegalArgumentException("batchSize must be greater than zero, but was: " + batchSize);
}
return new NonParallelSortedSetIterable<>(this);
}
@Override
public ImmutableSortedSet<T> toReversed()
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".toReversed() not implemented yet");
}
@Override
public int detectLastIndex(Predicate<? super T> predicate)
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".detectLastIndex() not implemented yet");
}
}