001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.HashMap;
018    import java.util.Map;
019    
020    /**
021     * @author Michael Young
022     * @author Connor McKay
023     */
024    public class InheritableMap<K, V> extends HashMap<K, V> {
025    
026            public InheritableMap() {
027                    super();
028            }
029    
030            public InheritableMap(Map<? extends K, ? extends V> map) {
031                    super(map);
032            }
033    
034            public void clear() {
035                    super.clear();
036                    _parentMap = null;
037            }
038    
039            public boolean containsKey(Object key) {
040                    if (_parentMap != null && _parentMap.containsKey(key)) {
041                            return true;
042                    }
043                    else {
044                            return super.containsKey(key);
045                    }
046            }
047    
048            public boolean containsValue(Object value) {
049                    if ((_parentMap != null) && _parentMap.containsValue(value)) {
050                            return true;
051                    }
052                    else {
053                            return super.containsValue(value);
054                    }
055            }
056    
057            public V get(Object key) {
058                    if (super.containsKey(key)) {
059                            return super.get(key);
060                    }
061                    else if (_parentMap != null) {
062                            return _parentMap.get(key);
063                    }
064    
065                    return null;
066            }
067    
068            public Map<K, V> getParentMap() {
069                    return _parentMap;
070            }
071    
072            public V remove(Object key) {
073                    if (super.containsKey(key)) {
074                            return super.remove(key);
075                    }
076                    else if (_parentMap != null) {
077                            return _parentMap.remove(key);
078                    }
079    
080                    return null;
081            }
082    
083            public void setParentMap(Map<? extends K, ? extends V> parentMap) {
084                    _parentMap = (Map<K, V>)parentMap;
085            }
086    
087            private Map<K, V> _parentMap;
088    
089    }