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.map;
18  
19  import java.util.Collection;
20  import java.util.Map;
21  import java.util.Objects;
22  import java.util.Set;
23  
24  /**
25   * Provides a base decorator that enables additional functionality to be added
26   * to a Map via decoration.
27   * <p>
28   * Methods are forwarded directly to the decorated map.
29   * </p>
30   * <p>
31   * This implementation does not perform any special processing with
32   * {@link #entrySet()}, {@link #keySet()} or {@link #values()}. Instead
33   * it simply returns the set/collection from the wrapped map. This may be
34   * undesirable, for example if you are trying to write a validating
35   * implementation it would provide a loophole around the validation.
36   * But, you might want that loophole, so this class is kept simple.
37   * </p>
38   *
39   * @param <K> The type of the keys in the map
40   * @param <V> The type of the values in the map
41   * @since 3.0
42   */
43  public abstract class AbstractMapDecorator<K, V> extends AbstractIterableMap<K, V> {
44  
45      /** The map to decorate */
46      transient Map<K, V> map;
47  
48      /**
49       * Constructor only used in deserialization, do not use otherwise.
50       *
51       * @since 3.1
52       */
53      protected AbstractMapDecorator() {
54      }
55  
56      /**
57       * Constructor that wraps (not copies).
58       *
59       * @param map  The map to decorate, must not be null
60       * @throws NullPointerException if the map is null
61       */
62      protected AbstractMapDecorator(final Map<K, V> map) {
63          this.map = Objects.requireNonNull(map, "map");
64      }
65  
66      @Override
67      public void clear() {
68          decorated().clear();
69      }
70  
71      @Override
72      public boolean containsKey(final Object key) {
73          return decorated().containsKey(key);
74      }
75  
76      @Override
77      public boolean containsValue(final Object value) {
78          return decorated().containsValue(value);
79      }
80  
81      /**
82       * Gets the map being decorated.
83       *
84       * @return The decorated map
85       */
86      protected Map<K, V> decorated() {
87          return map;
88      }
89  
90      @Override
91      public Set<Map.Entry<K, V>> entrySet() {
92          return decorated().entrySet();
93      }
94  
95      @Override
96      public boolean equals(final Object object) {
97          if (object == this) {
98              return true;
99          }
100         return decorated().equals(object);
101     }
102 
103     @Override
104     public V get(final Object key) {
105         return decorated().get(key);
106     }
107 
108     @Override
109     public int hashCode() {
110         return decorated().hashCode();
111     }
112 
113     @Override
114     public boolean isEmpty() {
115         return decorated().isEmpty();
116     }
117 
118     @Override
119     public Set<K> keySet() {
120         return decorated().keySet();
121     }
122 
123     @Override
124     public V put(final K key, final V value) {
125         return decorated().put(key, value);
126     }
127 
128     @Override
129     public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
130         decorated().putAll(mapToCopy);
131     }
132 
133     @Override
134     public V remove(final Object key) {
135         return decorated().remove(key);
136     }
137 
138     @Override
139     public int size() {
140         return decorated().size();
141     }
142 
143     @Override
144     public String toString() {
145         return decorated().toString();
146     }
147 
148     @Override
149     public Collection<V> values() {
150         return decorated().values();
151     }
152 
153 }