001    /**
002     * Copyright (c) 2000-2011 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            @Override
035            public void clear() {
036                    super.clear();
037    
038                    _parentMap = null;
039            }
040    
041            @Override
042            public boolean containsKey(Object key) {
043                    if (_parentMap != null && _parentMap.containsKey(key)) {
044                            return true;
045                    }
046                    else {
047                            return super.containsKey(key);
048                    }
049            }
050    
051            @Override
052            public boolean containsValue(Object value) {
053                    if ((_parentMap != null) && _parentMap.containsValue(value)) {
054                            return true;
055                    }
056                    else {
057                            return super.containsValue(value);
058                    }
059            }
060    
061            @Override
062            public V get(Object key) {
063                    if (super.containsKey(key)) {
064                            return super.get(key);
065                    }
066                    else if (_parentMap != null) {
067                            return _parentMap.get(key);
068                    }
069    
070                    return null;
071            }
072    
073            public Map<K, V> getParentMap() {
074                    return _parentMap;
075            }
076    
077            @Override
078            public V remove(Object key) {
079                    if (super.containsKey(key)) {
080                            return super.remove(key);
081                    }
082                    else if (_parentMap != null) {
083                            return _parentMap.remove(key);
084                    }
085    
086                    return null;
087            }
088    
089            public void setParentMap(Map<? extends K, ? extends V> parentMap) {
090                    _parentMap = (Map<K, V>)parentMap;
091            }
092    
093            private Map<K, V> _parentMap;
094    
095    }