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 io.netty.util.internal.EmptyArrays;
18  
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.Iterator;
22  import java.util.NoSuchElementException;
23  
24  /**
25   * Utility methods for primitive collections.
26   */
27  public final class PrimitiveCollections {
28  
29      private static final IntObjectMap<Object> EMPTY_INT_OBJECT_MAP = new EmptyIntObjectMap();
30  
31      private PrimitiveCollections() {
32      }
33  
34      /**
35       * Returns an unmodifiable empty {@link IntObjectMap}.
36       */
37      @SuppressWarnings("unchecked")
38      public static <V> IntObjectMap<V> emptyIntObjectMap() {
39          return (IntObjectMap<V>) EMPTY_INT_OBJECT_MAP;
40      }
41  
42      /**
43       * Creates an unmodifiable wrapper around the given map.
44       */
45      public static <V> IntObjectMap<V> unmodifiableIntObjectMap(final IntObjectMap<V> map) {
46          return new UnmodifiableIntObjectMap<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 EmptyIntObjectMap implements IntObjectMap<Object> {
53  
54          @Override
55          public Object get(int key) {
56              return null;
57          }
58  
59          @Override
60          public Object put(int key, Object value) {
61              throw new UnsupportedOperationException("put");
62          }
63  
64          @Override
65          public void putAll(IntObjectMap<Object> sourceMap) {
66              throw new UnsupportedOperationException("putAll");
67          }
68  
69          @Override
70          public Object remove(int key) {
71              throw new UnsupportedOperationException("remove");
72          }
73  
74          @Override
75          public int size() {
76              return 0;
77          }
78  
79          @Override
80          public boolean isEmpty() {
81              return true;
82          }
83  
84          @Override
85          public void clear() {
86              // Do nothing.
87          }
88  
89          @Override
90          public boolean containsKey(int key) {
91              return false;
92          }
93  
94          @Override
95          public boolean containsValue(Object value) {
96              return false;
97          }
98  
99          @Override
100         public Iterable<Entry<Object>> entries() {
101             return Collections.emptySet();
102         }
103 
104         @Override
105         public int[] keys() {
106             return EmptyArrays.EMPTY_INTS;
107         }
108 
109         @Override
110         public Object[] values(Class<Object> clazz) {
111             return EmptyArrays.EMPTY_OBJECTS;
112         }
113 
114         @Override
115         public Collection<Object> values() {
116             return Collections.emptyList();
117         }
118     }
119 
120     /**
121      * An unmodifiable wrapper around a {@link IntObjectMap}.
122      *
123      * @param <V> the value type stored in the map.
124      */
125     private static final class UnmodifiableIntObjectMap<V> implements IntObjectMap<V>,
126             Iterable<IntObjectMap.Entry<V>> {
127         final IntObjectMap<V> map;
128 
129         UnmodifiableIntObjectMap(IntObjectMap<V> map) {
130             this.map = map;
131         }
132 
133         @Override
134         public V get(int key) {
135             return map.get(key);
136         }
137 
138         @Override
139         public V put(int key, V value) {
140             throw new UnsupportedOperationException("put");
141         }
142 
143         @Override
144         public void putAll(IntObjectMap<V> sourceMap) {
145             throw new UnsupportedOperationException("putAll");
146         }
147 
148         @Override
149         public V remove(int key) {
150             throw new UnsupportedOperationException("remove");
151         }
152 
153         @Override
154         public int size() {
155             return map.size();
156         }
157 
158         @Override
159         public boolean isEmpty() {
160             return map.isEmpty();
161         }
162 
163         @Override
164         public void clear() {
165             throw new UnsupportedOperationException("clear");
166         }
167 
168         @Override
169         public boolean containsKey(int key) {
170             return map.containsKey(key);
171         }
172 
173         @Override
174         public boolean containsValue(V value) {
175             return map.containsValue(value);
176         }
177 
178         @Override
179         public Iterable<Entry<V>> entries() {
180             return this;
181         }
182 
183         @Override
184         public Iterator<Entry<V>> iterator() {
185             return new IteratorImpl(map.entries().iterator());
186         }
187 
188         @Override
189         public int[] keys() {
190             return map.keys();
191         }
192 
193         @Override
194         public V[] values(Class<V> clazz) {
195             return map.values(clazz);
196         }
197 
198         @Override
199         public Collection<V> values() {
200             return map.values();
201         }
202 
203         /**
204          * Unmodifiable wrapper for an iterator.
205          */
206         private class IteratorImpl implements Iterator<Entry<V>> {
207             final Iterator<Entry<V>> iter;
208 
209             IteratorImpl(Iterator<Entry<V>> iter) {
210                 this.iter = iter;
211             }
212 
213             @Override
214             public boolean hasNext() {
215                 return iter.hasNext();
216             }
217 
218             @Override
219             public Entry<V> next() {
220                 if (!hasNext()) {
221                     throw new NoSuchElementException();
222                 }
223                 return new EntryImpl(iter.next());
224             }
225 
226             @Override
227             public void remove() {
228                 throw new UnsupportedOperationException("remove");
229             }
230         }
231 
232         /**
233          * Unmodifiable wrapper for an entry.
234          */
235         private class EntryImpl implements Entry<V> {
236             final Entry<V> entry;
237 
238             EntryImpl(Entry<V> entry) {
239                 this.entry = entry;
240             }
241 
242             @Override
243             public int key() {
244                 return entry.key();
245             }
246 
247             @Override
248             public V value() {
249                 return entry.value();
250             }
251 
252             @Override
253             public void setValue(V value) {
254                 throw new UnsupportedOperationException("setValue");
255             }
256         }
257     }
258 }