View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at:
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package io.netty.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   * Utilities for byte-based primitive collections.
26   */
27  public final class ByteCollections {
28  
29      private static final ByteObjectMap<Object> EMPTY_MAP = new EmptyMap();
30  
31      private ByteCollections() {
32      }
33  
34      /**
35       * Returns an unmodifiable empty {@link ByteObjectMap}.
36       */
37      @SuppressWarnings("unchecked")
38      public static <V> ByteObjectMap<V> emptyMap() {
39          return (ByteObjectMap<V>) EMPTY_MAP;
40      }
41  
42      /**
43       * Creates an unmodifiable wrapper around the given map.
44       */
45      public static <V> ByteObjectMap<V> unmodifiableMap(final ByteObjectMap<V> map) {
46          return new UnmodifiableMap<V>(map);
47      }
48  
49      /**
50       * An empty map. All operations that attempt to modify the map are unsupported.
51       */
52      private static final class EmptyMap implements ByteObjectMap<Object> {
53          @Override
54          public Object get(byte key) {
55              return null;
56          }
57  
58          @Override
59          public Object put(byte key, Object value) {
60              throw new UnsupportedOperationException("put");
61          }
62  
63          @Override
64          public Object remove(byte 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              // Do nothing.
86          }
87  
88          @Override
89          public Set<Byte> keySet() {
90              return Collections.emptySet();
91          }
92  
93          @Override
94          public boolean containsKey(byte 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(Byte 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 Byte, ?> 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<Byte, Object>> entrySet() {
135             return Collections.emptySet();
136         }
137     }
138 
139     /**
140      * An unmodifiable wrapper around a {@link ByteObjectMap}.
141      *
142      * @param <V> the value type stored in the map.
143      */
144     private static final class UnmodifiableMap<V> implements ByteObjectMap<V> {
145         private final ByteObjectMap<V> map;
146         private Set<Byte> keySet;
147         private Set<Entry<Byte, V>> entrySet;
148         private Collection<V> values;
149         private Iterable<PrimitiveEntry<V>> entries;
150 
151         UnmodifiableMap(ByteObjectMap<V> map) {
152             this.map = map;
153         }
154 
155         @Override
156         public V get(byte key) {
157             return map.get(key);
158         }
159 
160         @Override
161         public V put(byte key, V value) {
162             throw new UnsupportedOperationException("put");
163         }
164 
165         @Override
166         public V remove(byte 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(byte 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(Byte 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 Byte, ? 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<Byte> keySet() {
236             if (keySet == null) {
237                 keySet = Collections.unmodifiableSet(map.keySet());
238             }
239             return keySet;
240         }
241 
242         @Override
243         public Set<Entry<Byte, 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          * Unmodifiable wrapper for an iterator.
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          * Unmodifiable wrapper for an entry.
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 byte 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 }