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