001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.commons.collections.map;
018    
019    import java.util.Collection;
020    import java.util.Map;
021    import java.util.Set;
022    
023    /**
024     * Provides a base decorator that enables additional functionality to be added
025     * to a Map via decoration.
026     * <p>
027     * Methods are forwarded directly to the decorated map.
028     * <p>
029     * This implementation does not perform any special processing with
030     * {@link #entrySet()}, {@link #keySet()} or {@link #values()}. Instead
031     * it simply returns the set/collection from the wrapped map. This may be
032     * undesirable, for example if you are trying to write a validating
033     * implementation it would provide a loophole around the validation.
034     * But, you might want that loophole, so this class is kept simple.
035     *
036     * @since Commons Collections 3.0
037     * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
038     * 
039     * @author Daniel Rall
040     * @author Stephen Colebourne
041     */
042    public abstract class AbstractMapDecorator implements Map {
043    
044        /** The map to decorate */
045        protected transient Map map;
046    
047        /**
048         * Constructor only used in deserialization, do not use otherwise.
049         * @since Commons Collections 3.1
050         */
051        protected AbstractMapDecorator() {
052            super();
053        }
054    
055        /**
056         * Constructor that wraps (not copies).
057         *
058         * @param map  the map to decorate, must not be null
059         * @throws IllegalArgumentException if the collection is null
060         */
061        public AbstractMapDecorator(Map map) {
062            if (map == null) {
063                throw new IllegalArgumentException("Map must not be null");
064            }
065            this.map = map;
066        }
067    
068        /**
069         * Gets the map being decorated.
070         * 
071         * @return the decorated map
072         */
073        protected Map getMap() {
074            return map;
075        }
076    
077        //-----------------------------------------------------------------------
078        public void clear() {
079            map.clear();
080        }
081    
082        public boolean containsKey(Object key) {
083            return map.containsKey(key);
084        }
085    
086        public boolean containsValue(Object value) {
087            return map.containsValue(value);
088        }
089    
090        public Set entrySet() {
091            return map.entrySet();
092        }
093    
094        public Object get(Object key) {
095            return map.get(key);
096        }
097    
098        public boolean isEmpty() {
099            return map.isEmpty();
100        }
101    
102        public Set keySet() {
103            return map.keySet();
104        }
105    
106        public Object put(Object key, Object value) {
107            return map.put(key, value);
108        }
109    
110        public void putAll(Map mapToCopy) {
111            map.putAll(mapToCopy);
112        }
113    
114        public Object remove(Object key) {
115            return map.remove(key);
116        }
117    
118        public int size() {
119            return map.size();
120        }
121    
122        public Collection values() {
123            return map.values();
124        }
125       
126        public boolean equals(Object object) {
127            if (object == this) {
128                return true;
129            }
130            return map.equals(object);
131        }
132    
133        public int hashCode() {
134            return map.hashCode();
135        }
136    
137        public String toString() {
138            return map.toString();
139        }
140    
141    }