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.util;
016    
017    import com.liferay.portal.kernel.util.MultiValueMap;
018    
019    import java.io.Serializable;
020    
021    import java.util.Collection;
022    import java.util.HashMap;
023    import java.util.HashSet;
024    import java.util.Map;
025    import java.util.Set;
026    
027    /**
028     * @author Alexander Chow
029     */
030    public class MemoryMultiValueMap<K extends Serializable, V extends Serializable>
031            extends MultiValueMap<K, V> {
032    
033            @Override
034            public void clear() {
035                    _map.clear();
036            }
037    
038            @Override
039            public boolean containsKey(Object key) {
040                    return _map.containsKey(key);
041            }
042    
043            @Override
044            public boolean containsValue(Object value) {
045                    for (Map.Entry<K, Set<V>> entry : _map.entrySet()) {
046                            Set<V> values = entry.getValue();
047    
048                            if (values.contains(value)) {
049                                    return true;
050                            }
051                    }
052    
053                    return false;
054            }
055    
056            @Override
057            public Set<V> getAll(Object key) {
058                    return _map.get(key);
059            }
060    
061            @Override
062            public boolean isEmpty() {
063                    return _map.isEmpty();
064            }
065    
066            @Override
067            public Set<K> keySet() {
068                    return _map.keySet();
069            }
070    
071            @Override
072            public V put(K key, V value) {
073                    Set<V> values = _map.get(key);
074    
075                    if (values == null) {
076                            values = new HashSet<V>();
077                    }
078    
079                    values.add(value);
080    
081                    _map.put(key, values);
082    
083                    return value;
084            }
085    
086            @Override
087            public Set<V> putAll(K key, Collection<? extends V> values) {
088                    Set<V> oldValues = _map.get(key);
089    
090                    if (oldValues == null) {
091                            oldValues = new HashSet<V>();
092                    }
093    
094                    oldValues.addAll(values);
095    
096                    _map.put(key, oldValues);
097    
098                    return oldValues;
099            }
100    
101            @Override
102            public V remove(Object key) {
103                    V value = null;
104    
105                    Set<V> values = _map.remove(key);
106    
107                    if ((values != null) && !values.isEmpty()) {
108                            value = values.iterator().next();
109                    }
110    
111                    return value;
112            }
113    
114            private Map<K, Set<V>> _map = new HashMap<K, Set<V>>();
115    
116    }