001    /**
002     * Copyright (c) 2000-2013 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                    super();
029            }
030    
031            public InheritableMap(Map<? extends K, ? extends V> map) {
032                    super(map);
033            }
034    
035            @Override
036            public void clear() {
037                    super.clear();
038    
039                    _parentMap = null;
040            }
041    
042            @Override
043            public boolean containsKey(Object key) {
044                    if ((_parentMap != null) && _parentMap.containsKey(key)) {
045                            return true;
046                    }
047                    else {
048                            return super.containsKey(key);
049                    }
050            }
051    
052            @Override
053            public boolean containsValue(Object value) {
054                    if ((_parentMap != null) && _parentMap.containsValue(value)) {
055                            return true;
056                    }
057                    else {
058                            return super.containsValue(value);
059                    }
060            }
061    
062            @Override
063            public V get(Object key) {
064                    V value = super.get(key);
065    
066                    if (value != null) {
067                            return value;
068                    }
069                    else if (_parentMap != null) {
070                            return _parentMap.get(key);
071                    }
072    
073                    return null;
074            }
075    
076            public Map<K, V> getParentMap() {
077                    return _parentMap;
078            }
079    
080            @Override
081            public V remove(Object key) {
082                    V value = super.remove(key);
083    
084                    if (value != null) {
085                            return value;
086                    }
087                    else if (_parentMap != null) {
088                            return _parentMap.remove(key);
089                    }
090    
091                    return null;
092            }
093    
094            public void setParentMap(Map<? extends K, ? extends V> parentMap) {
095                    _parentMap = (Map<K, V>)parentMap;
096            }
097    
098            @Override
099            public String toString() {
100                    String string = super.toString();
101    
102                    String parentString = "{}";
103    
104                    if (_parentMap != null) {
105                            parentString = _parentMap.toString();
106                    }
107    
108                    if (string.length() <= 2) {
109                            return parentString;
110                    }
111    
112                    StringBundler sb = new StringBundler(3);
113    
114                    sb.append(string.substring(0, string.length() - 1));
115                    sb.append(StringPool.COMMA_AND_SPACE);
116                    sb.append(parentString.substring(1));
117    
118                    return sb.toString();
119            }
120    
121            private Map<K, V> _parentMap;
122    
123    }