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