View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4;
18  
19  import java.util.Collection;
20  import java.util.Map;
21  import java.util.Objects;
22  import java.util.Set;
23  
24  import org.apache.commons.collections4.collection.UnmodifiableCollection;
25  import org.apache.commons.collections4.iterators.UnmodifiableMapIterator;
26  import org.apache.commons.collections4.map.EntrySetToMapIteratorAdapter;
27  import org.apache.commons.collections4.map.UnmodifiableEntrySet;
28  import org.apache.commons.collections4.set.UnmodifiableSet;
29  
30  /**
31   * Utilities for working with "split maps:" objects that implement {@link Put}
32   * and/or {@link Get} but not {@link Map}.
33   *
34   * @since 4.0
35   * @see Get
36   * @see Put
37   */
38  public class SplitMapUtils {
39  
40      private static final class WrappedGet<K, V> implements IterableMap<K, V>, Unmodifiable {
41          private final Get<K, V> get;
42  
43          private WrappedGet(final Get<K, V> get) {
44              this.get = get;
45          }
46  
47          /**
48           * Always throws {@link UnsupportedOperationException}.
49           *
50           * @throws UnsupportedOperationException Always thrown.
51           */
52          @Override
53          public void clear() {
54              throw new UnsupportedOperationException();
55          }
56  
57          @Override
58          public boolean containsKey(final Object key) {
59              return get.containsKey(key);
60          }
61  
62          @Override
63          public boolean containsValue(final Object value) {
64              return get.containsValue(value);
65          }
66  
67          @Override
68          public Set<Map.Entry<K, V>> entrySet() {
69              return UnmodifiableEntrySet.unmodifiableEntrySet(get.entrySet());
70          }
71  
72          @Override
73          public boolean equals(final Object arg0) {
74              if (arg0 == this) {
75                  return true;
76              }
77              return arg0 instanceof WrappedGet && ((WrappedGet<?, ?>) arg0).get.equals(get);
78          }
79  
80          @Override
81          public V get(final Object key) {
82              return get.get(key);
83          }
84  
85          @Override
86          public int hashCode() {
87              return "WrappedGet".hashCode() << 4 | get.hashCode();
88          }
89  
90          @Override
91          public boolean isEmpty() {
92              return get.isEmpty();
93          }
94  
95          @Override
96          public Set<K> keySet() {
97              return UnmodifiableSet.unmodifiableSet(get.keySet());
98          }
99  
100         @Override
101         public MapIterator<K, V> mapIterator() {
102             final MapIterator<K, V> it;
103             if (get instanceof IterableGet) {
104                 it = ((IterableGet<K, V>) get).mapIterator();
105             } else {
106                 it = new EntrySetToMapIteratorAdapter<>(get.entrySet());
107             }
108             return UnmodifiableMapIterator.unmodifiableMapIterator(it);
109         }
110 
111         /**
112          * Always throws {@link UnsupportedOperationException}.
113          *
114          * @param key Ignored.
115          * @param value Ignored.
116          * @throws UnsupportedOperationException Always thrown.
117          */
118         @Override
119         public V put(final K key, final V value) {
120             throw new UnsupportedOperationException();
121         }
122 
123         /**
124          * Always throws {@link UnsupportedOperationException}.
125          *
126          * @param map Ignored.
127          * @throws UnsupportedOperationException Always thrown.
128          */
129         @Override
130         public void putAll(final Map<? extends K, ? extends V> map) {
131             throw new UnsupportedOperationException();
132         }
133 
134         /**
135          * Always throws {@link UnsupportedOperationException}.
136          *
137          * @param key The key whose mapping would be removed.
138          * @throws UnsupportedOperationException Always thrown.
139          */
140         @Override
141         public V remove(final Object key) {
142             throw new UnsupportedOperationException();
143         }
144 
145         @Override
146         public int size() {
147             return get.size();
148         }
149 
150         @Override
151         public Collection<V> values() {
152             return UnmodifiableCollection.unmodifiableCollection(get.values());
153         }
154     }
155 
156     private static final class WrappedPut<K, V> implements Map<K, V>, Put<K, V> {
157         private final Put<K, V> put;
158 
159         private WrappedPut(final Put<K, V> put) {
160             this.put = put;
161         }
162 
163         @Override
164         public void clear() {
165             put.clear();
166         }
167 
168         /**
169          * Always throws {@link UnsupportedOperationException}.
170          *
171          * @param key Ignored.
172          * @throws UnsupportedOperationException Always thrown.
173          */
174         @Override
175         public boolean containsKey(final Object key) {
176             throw new UnsupportedOperationException();
177         }
178 
179         /**
180          * Always throws {@link UnsupportedOperationException}.
181          *
182          * @param value Ignored.
183          * @throws UnsupportedOperationException Always thrown.
184          */
185         @Override
186         public boolean containsValue(final Object value) {
187             throw new UnsupportedOperationException();
188         }
189 
190         /**
191          * Always throws {@link UnsupportedOperationException}.
192          *
193          * @throws UnsupportedOperationException Always thrown.
194          */
195         @Override
196         public Set<Map.Entry<K, V>> entrySet() {
197             throw new UnsupportedOperationException();
198         }
199 
200         @Override
201         public boolean equals(final Object obj) {
202             if (obj == this) {
203                 return true;
204             }
205             return obj instanceof WrappedPut && ((WrappedPut<?, ?>) obj).put.equals(put);
206         }
207 
208         /**
209          * Always throws {@link UnsupportedOperationException}.
210          *
211          * @param key Ignored.
212          * @throws UnsupportedOperationException Always thrown.
213          */
214         @Override
215         public V get(final Object key) {
216             throw new UnsupportedOperationException();
217         }
218 
219         @Override
220         public int hashCode() {
221             return "WrappedPut".hashCode() << 4 | put.hashCode();
222         }
223 
224         /**
225          * Always throws {@link UnsupportedOperationException}.
226          *
227          * @throws UnsupportedOperationException Always thrown.
228          */
229         @Override
230         public boolean isEmpty() {
231             throw new UnsupportedOperationException();
232         }
233 
234         /**
235          * Always throws {@link UnsupportedOperationException}.
236          *
237          * @throws UnsupportedOperationException Always thrown.
238          */
239         @Override
240         public Set<K> keySet() {
241             throw new UnsupportedOperationException();
242         }
243 
244         @Override
245         @SuppressWarnings("unchecked")
246         public V put(final K key, final V value) {
247             return (V) put.put(key, value);
248         }
249 
250         @Override
251         public void putAll(final Map<? extends K, ? extends V> t) {
252             put.putAll(t);
253         }
254 
255         /**
256          * Always throws {@link UnsupportedOperationException}.
257          *
258          * @param key Ignored.
259          * @throws UnsupportedOperationException Always thrown.
260          */
261         @Override
262         public V remove(final Object key) {
263             throw new UnsupportedOperationException();
264         }
265 
266         /**
267          * Always throws {@link UnsupportedOperationException}.
268          *
269          * @throws UnsupportedOperationException Always thrown.
270          */
271         @Override
272         public int size() {
273             throw new UnsupportedOperationException();
274         }
275 
276         /**
277          * Always throws {@link UnsupportedOperationException}.
278          *
279          * @throws UnsupportedOperationException Always thrown.
280          */
281         @Override
282         public Collection<V> values() {
283             throw new UnsupportedOperationException();
284         }
285     }
286 
287     /**
288      * Gets the specified {@link Get} as an instance of {@link IterableMap}.
289      * If {@code get} implements {@link IterableMap} directly, no conversion will take place.
290      * If {@code get} implements {@link Map} but not {@link IterableMap} it will be decorated.
291      * Otherwise, an {@link Unmodifiable} {@link IterableMap} will be returned.
292      *
293      * @param <K> The key type
294      * @param <V> The value type
295      * @param get to wrap, must not be null
296      * @return {@link IterableMap}
297      * @throws NullPointerException if the argument is null
298      */
299     @SuppressWarnings("unchecked")
300     public static <K, V> IterableMap<K, V> readableMap(final Get<K, V> get) {
301         Objects.requireNonNull(get, "get");
302         if (get instanceof Map) {
303             return get instanceof IterableMap ?
304                     (IterableMap<K, V>) get :
305                     MapUtils.iterableMap((Map<K, V>) get);
306         }
307         return new WrappedGet<>(get);
308     }
309 
310     /**
311      * Gets the specified {@link Put} as an instanceof {@link Map}.
312      * If {@code put} implements {@link Map} directly, no conversion will take place.
313      * Otherwise, a <em>write-only</em> {@link Map} will be returned.  On such a {@link Map}
314      * it is recommended that the result of #put(K, V) be discarded as it likely will not
315      * match {@code V} at runtime.
316      *
317      * @param <K> The key type
318      * @param <V> The element type
319      * @param put to wrap, must not be null
320      * @return {@link Map}
321      * @throws NullPointerException if the argument is null
322      */
323     @SuppressWarnings("unchecked")
324     public static <K, V> Map<K, V> writableMap(final Put<K, V> put) {
325         Objects.requireNonNull(put, "put");
326         if (put instanceof Map) {
327             return (Map<K, V>) put;
328         }
329         return new WrappedPut<>(put);
330     }
331 
332     /**
333      * Don't allow instances.
334      */
335     private SplitMapUtils() {
336         // empty
337     }
338 
339 }