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.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       * @since 3.1
51       */
52      protected AbstractMapDecorator() {
53      }
54  
55      /**
56       * Constructor that wraps (not copies).
57       *
58       * @param map  the map to decorate, must not be null
59       * @throws NullPointerException if the map is null
60       */
61      protected AbstractMapDecorator(final Map<K, V> map) {
62          this.map = Objects.requireNonNull(map, "map");
63      }
64  
65      @Override
66      public void clear() {
67          decorated().clear();
68      }
69  
70      @Override
71      public boolean containsKey(final Object key) {
72          return decorated().containsKey(key);
73      }
74  
75      @Override
76      public boolean containsValue(final Object value) {
77          return decorated().containsValue(value);
78      }
79  
80      /**
81       * Gets the map being decorated.
82       *
83       * @return the decorated map
84       */
85      protected Map<K, V> decorated() {
86          return map;
87      }
88  
89      @Override
90      public Set<Map.Entry<K, V>> entrySet() {
91          return decorated().entrySet();
92      }
93  
94      @Override
95      public boolean equals(final Object object) {
96          if (object == this) {
97              return true;
98          }
99          return decorated().equals(object);
100     }
101 
102     @Override
103     public V get(final Object key) {
104         return decorated().get(key);
105     }
106 
107     @Override
108     public int hashCode() {
109         return decorated().hashCode();
110     }
111 
112     @Override
113     public boolean isEmpty() {
114         return decorated().isEmpty();
115     }
116 
117     @Override
118     public Set<K> keySet() {
119         return decorated().keySet();
120     }
121 
122     @Override
123     public V put(final K key, final V value) {
124         return decorated().put(key, value);
125     }
126 
127     @Override
128     public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
129         decorated().putAll(mapToCopy);
130     }
131 
132     @Override
133     public V remove(final Object key) {
134         return decorated().remove(key);
135     }
136 
137     @Override
138     public int size() {
139         return decorated().size();
140     }
141 
142     @Override
143     public String toString() {
144         return decorated().toString();
145     }
146 
147     @Override
148     public Collection<V> values() {
149         return decorated().values();
150     }
151 
152 }