001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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     * @author Shuyang Zhou
024     */
025    public class InheritableMap<K, V> extends HashMap<K, V> {
026    
027            public InheritableMap() {
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                    V value = super.get(key);
064    
065                    if (value != null) {
066                            return value;
067                    }
068                    else if (_parentMap != null) {
069                            return _parentMap.get(key);
070                    }
071    
072                    return null;
073            }
074    
075            public Map<K, V> getParentMap() {
076                    return _parentMap;
077            }
078    
079            @Override
080            public V remove(Object key) {
081                    V value = super.remove(key);
082    
083                    if (value != null) {
084                            return value;
085                    }
086                    else if (_parentMap != null) {
087                            return _parentMap.remove(key);
088                    }
089    
090                    return null;
091            }
092    
093            public void setParentMap(Map<? extends K, ? extends V> parentMap) {
094                    _parentMap = (Map<K, V>)parentMap;
095            }
096    
097            @Override
098            public String toString() {
099                    String string = super.toString();
100    
101                    String parentString = "{}";
102    
103                    if (_parentMap != null) {
104                            parentString = _parentMap.toString();
105                    }
106    
107                    if (string.length() <= 2) {
108                            return parentString;
109                    }
110    
111                    StringBundler sb = new StringBundler(3);
112    
113                    sb.append(string.substring(0, string.length() - 1));
114                    sb.append(StringPool.COMMA_AND_SPACE);
115                    sb.append(parentString.substring(1));
116    
117                    return sb.toString();
118            }
119    
120            private Map<K, V> _parentMap;
121    
122    }