/
SingletonMap.java
267 lines (230 loc) · 6.83 KB
/
SingletonMap.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
/*
* 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.fixed;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.map.FixedSizeMap;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.factory.Comparators;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.Maps;
import org.eclipse.collections.impl.factory.Sets;
import org.eclipse.collections.impl.tuple.ImmutableEntry;
import org.eclipse.collections.impl.tuple.Tuples;
final class SingletonMap<K, V>
extends AbstractMemoryEfficientMutableMap<K, V>
implements Externalizable
{
private static final long serialVersionUID = 1L;
private K key1;
private V value1;
@SuppressWarnings("UnusedDeclaration")
public SingletonMap()
{
// For Externalizable use only
}
SingletonMap(K key1, V value1)
{
this.key1 = key1;
this.value1 = value1;
}
@Override
public int size()
{
return 1;
}
K getKey1()
{
return this.key1;
}
@Override
public MutableMap<K, V> withKeyValue(K addKey, V addValue)
{
// Map behavior specifies that if you put in a duplicate key, you replace the value
if (Comparators.nullSafeEquals(this.key1, addKey))
{
this.value1 = addValue;
return this;
}
return new DoubletonMap<>(this.key1, this.value1, addKey, addValue);
}
@Override
public MutableMap<K, V> withoutKey(K key)
{
if (Comparators.nullSafeEquals(key, this.key1))
{
return new EmptyMap<>();
}
return this;
}
// Weird implementation of clone() is ok on final classes
@Override
public SingletonMap<K, V> clone()
{
return new SingletonMap<>(this.key1, this.value1);
}
@Override
public ImmutableMap<K, V> toImmutable()
{
return Maps.immutable.with(this.key1, this.value1);
}
@Override
public boolean containsKey(Object key)
{
return Comparators.nullSafeEquals(this.key1, key);
}
@Override
public boolean containsValue(Object value)
{
return Comparators.nullSafeEquals(this.value1, value);
}
@Override
public V get(Object key)
{
if (Comparators.nullSafeEquals(this.key1, key))
{
return this.value1;
}
return null;
}
@Override
public Set<K> keySet()
{
return Sets.fixedSize.of(this.key1);
}
@Override
public Collection<V> values()
{
return Lists.fixedSize.of(this.value1);
}
@Override
public MutableSet<Entry<K, V>> entrySet()
{
return Sets.fixedSize.of(new ImmutableEntry<>(this.key1, this.value1));
}
@Override
public String toString()
{
return '{' + String.valueOf(this.key1) + '=' + this.value1 + '}';
}
@Override
public int hashCode()
{
return this.keyAndValueHashCode(this.key1, this.value1);
}
@Override
public boolean equals(Object other)
{
if (!(other instanceof Map))
{
return false;
}
Map<K, V> that = (Map<K, V>) other;
return that.size() == this.size() && this.keyAndValueEquals(this.key1, this.value1, that);
}
@Override
public MutableMap<V, K> flipUniqueValues()
{
return Maps.fixedSize.with(this.value1, this.key1);
}
@Override
public void forEachKeyValue(Procedure2<? super K, ? super V> procedure)
{
procedure.value(this.key1, this.value1);
}
@Override
public void forEachKey(Procedure<? super K> procedure)
{
procedure.value(this.key1);
}
@Override
public void forEachValue(Procedure<? super V> procedure)
{
procedure.value(this.value1);
}
@Override
public void forEachWithIndex(ObjectIntProcedure<? super V> objectIntProcedure)
{
objectIntProcedure.value(this.value1, 0);
}
@Override
public <P> void forEachWith(Procedure2<? super V, ? super P> procedure, P parameter)
{
procedure.value(this.value1, parameter);
}
@Override
public void writeExternal(ObjectOutput out) throws IOException
{
out.writeObject(this.key1);
out.writeObject(this.value1);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
this.key1 = (K) in.readObject();
this.value1 = (V) in.readObject();
}
@Override
public FixedSizeMap<K, V> select(Predicate2<? super K, ? super V> predicate)
{
if (predicate.accept(this.key1, this.value1))
{
return this.clone();
}
return Maps.fixedSize.of();
}
@Override
public <R> FixedSizeMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function)
{
return Maps.fixedSize.of(this.key1, function.value(this.key1, this.value1));
}
@Override
public <K2, V2> FixedSizeMap<K2, V2> collect(Function2<? super K, ? super V, Pair<K2, V2>> function)
{
Pair<K2, V2> pair1 = function.value(this.key1, this.value1);
return Maps.fixedSize.of(pair1.getOne(), pair1.getTwo());
}
@Override
public FixedSizeMap<K, V> reject(Predicate2<? super K, ? super V> predicate)
{
if (predicate.accept(this.key1, this.value1))
{
return Maps.fixedSize.of();
}
return this.clone();
}
@Override
public Pair<K, V> detect(Predicate2<? super K, ? super V> predicate)
{
if (predicate.accept(this.key1, this.value1))
{
return Tuples.pair(this.key1, this.value1);
}
return null;
}
@Override
public V getOnly()
{
return this.value1;
}
}