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