1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package io.netty5.util.collection;
16
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.Iterator;
20 import java.util.Map;
21 import java.util.NoSuchElementException;
22 import java.util.Set;
23
24
25
26
27 public final class LongCollections {
28
29 private static final LongObjectMap<Object> EMPTY_MAP = new EmptyMap();
30
31 private LongCollections() {
32 }
33
34
35
36
37 @SuppressWarnings("unchecked")
38 public static <V> LongObjectMap<V> emptyMap() {
39 return (LongObjectMap<V>) EMPTY_MAP;
40 }
41
42
43
44
45 public static <V> LongObjectMap<V> unmodifiableMap(final LongObjectMap<V> map) {
46 return new UnmodifiableMap<V>(map);
47 }
48
49
50
51
52 private static final class EmptyMap implements LongObjectMap<Object> {
53 @Override
54 public Object get(long key) {
55 return null;
56 }
57
58 @Override
59 public Object put(long key, Object value) {
60 throw new UnsupportedOperationException("put");
61 }
62
63 @Override
64 public Object remove(long key) {
65 return null;
66 }
67
68 @Override
69 public int size() {
70 return 0;
71 }
72
73 @Override
74 public boolean isEmpty() {
75 return true;
76 }
77
78 @Override
79 public boolean containsKey(Object key) {
80 return false;
81 }
82
83 @Override
84 public void clear() {
85
86 }
87
88 @Override
89 public Set<Long> keySet() {
90 return Collections.emptySet();
91 }
92
93 @Override
94 public boolean containsKey(long key) {
95 return false;
96 }
97
98 @Override
99 public boolean containsValue(Object value) {
100 return false;
101 }
102
103 @Override
104 public Iterable<PrimitiveEntry<Object>> entries() {
105 return Collections.emptySet();
106 }
107
108 @Override
109 public Object get(Object key) {
110 return null;
111 }
112
113 @Override
114 public Object put(Long key, Object value) {
115 throw new UnsupportedOperationException();
116 }
117
118 @Override
119 public Object remove(Object key) {
120 return null;
121 }
122
123 @Override
124 public void putAll(Map<? extends Long, ?> m) {
125 throw new UnsupportedOperationException();
126 }
127
128 @Override
129 public Collection<Object> values() {
130 return Collections.emptyList();
131 }
132
133 @Override
134 public Set<Entry<Long, Object>> entrySet() {
135 return Collections.emptySet();
136 }
137 }
138
139
140
141
142
143
144 private static final class UnmodifiableMap<V> implements LongObjectMap<V> {
145 private final LongObjectMap<V> map;
146 private Set<Long> keySet;
147 private Set<Entry<Long, V>> entrySet;
148 private Collection<V> values;
149 private Iterable<PrimitiveEntry<V>> entries;
150
151 UnmodifiableMap(LongObjectMap<V> map) {
152 this.map = map;
153 }
154
155 @Override
156 public V get(long key) {
157 return map.get(key);
158 }
159
160 @Override
161 public V put(long key, V value) {
162 throw new UnsupportedOperationException("put");
163 }
164
165 @Override
166 public V remove(long key) {
167 throw new UnsupportedOperationException("remove");
168 }
169
170 @Override
171 public int size() {
172 return map.size();
173 }
174
175 @Override
176 public boolean isEmpty() {
177 return map.isEmpty();
178 }
179
180 @Override
181 public void clear() {
182 throw new UnsupportedOperationException("clear");
183 }
184
185 @Override
186 public boolean containsKey(long key) {
187 return map.containsKey(key);
188 }
189
190 @Override
191 public boolean containsValue(Object value) {
192 return map.containsValue(value);
193 }
194
195 @Override
196 public boolean containsKey(Object key) {
197 return map.containsKey(key);
198 }
199
200 @Override
201 public V get(Object key) {
202 return map.get(key);
203 }
204
205 @Override
206 public V put(Long key, V value) {
207 throw new UnsupportedOperationException("put");
208 }
209
210 @Override
211 public V remove(Object key) {
212 throw new UnsupportedOperationException("remove");
213 }
214
215 @Override
216 public void putAll(Map<? extends Long, ? extends V> m) {
217 throw new UnsupportedOperationException("putAll");
218 }
219
220 @Override
221 public Iterable<PrimitiveEntry<V>> entries() {
222 if (entries == null) {
223 entries = new Iterable<PrimitiveEntry<V>>() {
224 @Override
225 public Iterator<PrimitiveEntry<V>> iterator() {
226 return new IteratorImpl(map.entries().iterator());
227 }
228 };
229 }
230
231 return entries;
232 }
233
234 @Override
235 public Set<Long> keySet() {
236 if (keySet == null) {
237 keySet = Collections.unmodifiableSet(map.keySet());
238 }
239 return keySet;
240 }
241
242 @Override
243 public Set<Entry<Long, V>> entrySet() {
244 if (entrySet == null) {
245 entrySet = Collections.unmodifiableSet(map.entrySet());
246 }
247 return entrySet;
248 }
249
250 @Override
251 public Collection<V> values() {
252 if (values == null) {
253 values = Collections.unmodifiableCollection(map.values());
254 }
255 return values;
256 }
257
258
259
260
261 private class IteratorImpl implements Iterator<PrimitiveEntry<V>> {
262 final Iterator<PrimitiveEntry<V>> iter;
263
264 IteratorImpl(Iterator<PrimitiveEntry<V>> iter) {
265 this.iter = iter;
266 }
267
268 @Override
269 public boolean hasNext() {
270 return iter.hasNext();
271 }
272
273 @Override
274 public PrimitiveEntry<V> next() {
275 if (!hasNext()) {
276 throw new NoSuchElementException();
277 }
278 return new EntryImpl(iter.next());
279 }
280
281 @Override
282 public void remove() {
283 throw new UnsupportedOperationException("remove");
284 }
285 }
286
287
288
289
290 private class EntryImpl implements PrimitiveEntry<V> {
291 private final PrimitiveEntry<V> entry;
292
293 EntryImpl(PrimitiveEntry<V> entry) {
294 this.entry = entry;
295 }
296
297 @Override
298 public long key() {
299 return entry.key();
300 }
301
302 @Override
303 public V value() {
304 return entry.value();
305 }
306
307 @Override
308 public void setValue(V value) {
309 throw new UnsupportedOperationException("setValue");
310 }
311 }
312 }
313 }