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    *      http://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.splitmap;
18  
19  import java.util.Collection;
20  import java.util.Map;
21  import java.util.Set;
22  
23  import org.apache.commons.collections4.IterableGet;
24  import org.apache.commons.collections4.MapIterator;
25  import org.apache.commons.collections4.map.EntrySetToMapIteratorAdapter;
26  
27  /**
28   * {@link IterableGet} that uses a {@link Map}<K, V> for the
29   * {@link org.apache.commons.collections4.Get Get}<K, V> implementation.
30   *
31   * @param <K> the type of the keys in this map
32   * @param <V> the type of the values in this map
33   * @since 4.0
34   */
35  public class AbstractIterableGetMapDecorator<K, V> implements IterableGet<K, V> {
36  
37      /** The map to decorate */
38      transient Map<K, V> map;
39  
40      /**
41       * Create a new AbstractSplitMapDecorator.
42       * @param map the map to decorate, must not be null
43       * @throws NullPointerException if map is null
44       */
45      public AbstractIterableGetMapDecorator(final Map<K, V> map) {
46          if (map == null) {
47              throw new NullPointerException("Map must not be null.");
48          }
49          this.map = map;
50      }
51  
52      /**
53       * Constructor only used in deserialization, do not use otherwise.
54       */
55      protected AbstractIterableGetMapDecorator() {
56          super();
57      }
58  
59      /**
60       * Gets the map being decorated.
61       *
62       * @return the decorated map
63       */
64      protected Map<K, V> decorated() {
65          return map;
66      }
67  
68      @Override
69      public boolean containsKey(final Object key) {
70          return decorated().containsKey(key);
71      }
72  
73      @Override
74      public boolean containsValue(final Object value) {
75          return decorated().containsValue(value);
76      }
77  
78      @Override
79      public Set<Map.Entry<K, V>> entrySet() {
80          return decorated().entrySet();
81      }
82  
83      @Override
84      public V get(final Object key) {
85          return decorated().get(key);
86      }
87  
88      @Override
89      public V remove(final Object key) {
90          return decorated().remove(key);
91      }
92  
93      @Override
94      public boolean isEmpty() {
95          return decorated().isEmpty();
96      }
97  
98      @Override
99      public Set<K> keySet() {
100         return decorated().keySet();
101     }
102 
103     @Override
104     public int size() {
105         return decorated().size();
106     }
107 
108     @Override
109     public Collection<V> values() {
110         return decorated().values();
111     }
112 
113     /**
114      * Get a MapIterator over this Get.
115      * @return MapIterator&lt;K, V&gt;
116      */
117     @Override
118     public MapIterator<K, V> mapIterator() {
119         return new EntrySetToMapIteratorAdapter<>(entrySet());
120     }
121 
122     @Override
123     public boolean equals(final Object object) {
124         if (object == this) {
125             return true;
126         }
127         return decorated().equals(object);
128     }
129 
130     @Override
131     public int hashCode() {
132         return decorated().hashCode();
133     }
134 
135     @Override
136     public String toString() {
137         return decorated().toString();
138     }
139 
140 }