-
Notifications
You must be signed in to change notification settings - Fork 585
/
ImmutableTreeMap.java
433 lines (381 loc) · 11.8 KB
/
ImmutableTreeMap.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
/*
* Copyright (c) 2016 Goldman Sachs.
* 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.map.sorted.immutable;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.SortedMap;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.factory.SortedMaps;
import org.eclipse.collections.api.factory.SortedSets;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.sorted.ImmutableSortedMap;
import org.eclipse.collections.api.map.sorted.MutableSortedMap;
import org.eclipse.collections.api.set.sorted.MutableSortedSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.factory.Comparators;
import org.eclipse.collections.impl.block.factory.Predicates2;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.set.sorted.mutable.TreeSortedSet;
import org.eclipse.collections.impl.tuple.ImmutableEntry;
import org.eclipse.collections.impl.utility.ArrayIterate;
import org.eclipse.collections.impl.utility.Iterate;
import org.eclipse.collections.impl.utility.MapIterate;
/**
* @see ImmutableSortedMap
*/
public class ImmutableTreeMap<K, V>
extends AbstractImmutableSortedMap<K, V>
implements Serializable
{
private final K[] keys;
private final V[] values;
private final Comparator<? super K> comparator;
public ImmutableTreeMap(SortedMap<K, V> sortedMap)
{
if (sortedMap == null)
{
throw new NullPointerException("Cannot convert null to ImmutableSortedMap");
}
this.comparator = sortedMap.comparator();
K[] keysCopy = (K[]) new Object[sortedMap.size()];
V[] valuesCopy = (V[]) new Object[sortedMap.size()];
int index = 0;
for (Entry<K, V> entry : sortedMap.entrySet())
{
keysCopy[index] = entry.getKey();
valuesCopy[index] = entry.getValue();
index++;
}
this.keys = keysCopy;
this.values = valuesCopy;
}
public static <K, V> ImmutableSortedMap<K, V> newMap(SortedMap<K, V> sortedMap)
{
return new ImmutableTreeMap<>(sortedMap);
}
@Override
public boolean equals(Object object)
{
if (this == object)
{
return true;
}
if (!(object instanceof Map))
{
return false;
}
Map<?, ?> other = (Map<?, ?>) object;
if (this.size() != other.size())
{
return false;
}
for (int index = 0; index < this.size(); index++)
{
if (!Objects.equals(this.values[index], other.get(this.keys[index])))
{
return false;
}
}
return true;
}
@Override
public int hashCode()
{
int length = this.keys.length;
int hashCode = 0;
for (int index = 0; index < length; index++)
{
K key = this.keys[index];
V value = this.values[index];
hashCode += (key == null ? 0 : key.hashCode())
^ (value == null ? 0 : value.hashCode());
}
return hashCode;
}
@Override
public String toString()
{
int length = this.keys.length;
if (length == 0)
{
return "{}";
}
StringBuilder sb = new StringBuilder();
sb.append('{');
for (int index = 0; index < length; index++)
{
sb.append(this.keys[index]);
sb.append('=');
sb.append(this.values[index]);
if (index < length - 1)
{
sb.append(',').append(' ');
}
}
return sb.append('}').toString();
}
@Override
public int size()
{
return this.keys.length;
}
@Override
public boolean containsKey(Object key)
{
return Arrays.binarySearch(this.keys, (K) key, this.comparator) >= 0;
}
@Override
public boolean containsValue(Object value)
{
return ArrayIterate.contains(this.values, value);
}
@Override
public V get(Object key)
{
int index = Arrays.binarySearch(this.keys, (K) key, this.comparator);
if (index >= 0)
{
return this.values[index];
}
return null;
}
@Override
public void forEachKeyValue(Procedure2<? super K, ? super V> procedure)
{
for (int index = 0; index < this.keys.length; index++)
{
procedure.value(this.keys[index], this.values[index]);
}
}
@Override
public ImmutableMap<V, K> flipUniqueValues()
{
return MapIterate.flipUniqueValues(this).toImmutable();
}
@Override
public RichIterable<K> keysView()
{
return FastList.newListWith(this.keys).asLazy();
}
@Override
public RichIterable<V> valuesView()
{
return FastList.newListWith(this.values).asLazy();
}
@Override
public RichIterable<Pair<K, V>> keyValuesView()
{
return FastList.newListWith(this.keys).asLazy().zip(FastList.newListWith(this.values));
}
@Override
public Comparator<? super K> comparator()
{
return this.comparator;
}
@Override
public K firstKey()
{
if (this.keys.length == 0)
{
throw new NoSuchElementException();
}
return this.keys[0];
}
@Override
public K lastKey()
{
if (this.keys.length == 0)
{
throw new NoSuchElementException();
}
return this.keys[this.keys.length - 1];
}
@Override
public Set<K> keySet()
{
return new ImmutableSortedMapKeySet();
}
@Override
public Collection<V> values()
{
return FastList.newListWith(this.values).asUnmodifiable();
}
@Override
public Set<Entry<K, V>> entrySet()
{
int length = this.keys.length;
MutableSortedSet<Entry<K, V>> entrySet = SortedSets.mutable.with(new EntryComparator<>(this.comparator));
for (int index = 0; index < length; index++)
{
entrySet.add(ImmutableEntry.of(this.keys[index], this.values[index]));
}
return entrySet.toImmutable().castToSortedSet();
}
protected Object writeReplace()
{
return new ImmutableSortedMapSerializationProxy<>(this);
}
@Override
public ImmutableSortedMap<K, V> take(int count)
{
if (count < 0)
{
throw new IllegalArgumentException("Count must be greater than zero, but was: " + count);
}
if (count == 0)
{
return SortedMaps.immutable.of(this.comparator());
}
if (count >= this.size())
{
return this;
}
MutableSortedMap<K, V> output = SortedMaps.mutable.of(this.comparator());
for (int i = 0; i < count; i++)
{
output.put(this.keys[i], this.values[i]);
}
return output.toImmutable();
}
@Override
public ImmutableSortedMap<K, V> drop(int count)
{
if (count < 0)
{
throw new IllegalArgumentException("Count must be greater than zero, but was: " + count);
}
if (count == 0)
{
return this;
}
if (count >= this.size())
{
return SortedMaps.immutable.of(this.comparator());
}
MutableSortedMap<K, V> output = SortedMaps.mutable.of(this.comparator());
for (int i = 0; i < this.size(); i++)
{
if (i >= count)
{
output.put(this.keys[i], this.values[i]);
}
}
return output.toImmutable();
}
private static final class EntryComparator<K, V> implements Comparator<Entry<K, V>>
{
private final Comparator<? super K> comparator;
private EntryComparator(Comparator<? super K> comparator)
{
if (comparator == null)
{
this.comparator = Comparators.naturalOrder();
}
else
{
this.comparator = comparator;
}
}
@Override
public int compare(Entry<K, V> o1, Entry<K, V> o2)
{
return this.comparator.compare(o1.getKey(), o2.getKey());
}
}
protected class ImmutableSortedMapKeySet extends AbstractSet<K> implements Serializable
{
private static final long serialVersionUID = 1L;
@Override
public boolean contains(Object o)
{
return ImmutableTreeMap.this.containsKey(o);
}
@Override
public boolean containsAll(Collection<?> collection)
{
return Iterate.allSatisfyWith(collection, Predicates2.in(), this);
}
@Override
public int size()
{
return ImmutableTreeMap.this.keys.length;
}
@Override
public Iterator<K> iterator()
{
return FastList.newListWith(ImmutableTreeMap.this.keys).asUnmodifiable().iterator();
}
@Override
public Object[] toArray()
{
int size = ImmutableTreeMap.this.keys.length;
Object[] result = new Object[size];
System.arraycopy(ImmutableTreeMap.this.keys, 0, result, 0, size);
return result;
}
@Override
public <T> T[] toArray(T[] a)
{
T[] result = a.length < this.size()
? (T[]) Array.newInstance(a.getClass().getComponentType(), this.size())
: a;
System.arraycopy(ImmutableTreeMap.this.keys, 0, result, 0, ImmutableTreeMap.this.keys.length);
if (result.length > this.size())
{
result[this.size()] = null;
}
return result;
}
@Override
public boolean add(K key)
{
throw new UnsupportedOperationException("Cannot call add() on " + this.getClass().getSimpleName());
}
@Override
public boolean addAll(Collection<? extends K> collection)
{
throw new UnsupportedOperationException("Cannot call addAll() on " + this.getClass().getSimpleName());
}
@Override
public boolean remove(Object key)
{
throw new UnsupportedOperationException("Cannot call remove() on " + this.getClass().getSimpleName());
}
@Override
public boolean removeAll(Collection<?> collection)
{
throw new UnsupportedOperationException("Cannot call removeAll() on " + this.getClass().getSimpleName());
}
@Override
public void clear()
{
throw new UnsupportedOperationException("Cannot call clear() on " + this.getClass().getSimpleName());
}
@Override
public boolean retainAll(Collection<?> collection)
{
throw new UnsupportedOperationException("Cannot call retainAll() on " + this.getClass().getSimpleName());
}
protected Object writeReplace()
{
return TreeSortedSet.newSetWith(ImmutableTreeMap.this.comparator, ImmutableTreeMap.this.keys).toImmutable();
}
}
}