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.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            public void clear() {
034                    _map.clear();
035            }
036    
037            public boolean containsKey(Object key) {
038                    return _map.containsKey(key);
039            }
040    
041            public boolean containsValue(Object value) {
042                    for (Map.Entry<K, Set<V>> entry : _map.entrySet()) {
043                            Set<V> values = entry.getValue();
044    
045                            if (values.contains(value)) {
046                                    return true;
047                            }
048                    }
049    
050                    return false;
051            }
052    
053            public Set<V> getAll(Object key) {
054                    return _map.get(key);
055            }
056    
057            public boolean isEmpty() {
058                    return _map.isEmpty();
059            }
060    
061            public Set<K> keySet() {
062                    return _map.keySet();
063            }
064    
065            public V put(K key, V value) {
066                    Set<V> values = _map.get(key);
067    
068                    if (values == null) {
069                            values = new HashSet<V>();
070                    }
071    
072                    values.add(value);
073    
074                    _map.put(key, values);
075    
076                    return value;
077            }
078    
079            public Set<V> putAll(K key, Collection<? extends V> values) {
080                    Set<V> oldValues = _map.get(key);
081    
082                    if (oldValues == null) {
083                            oldValues = new HashSet<V>();
084                    }
085    
086                    oldValues.addAll(values);
087    
088                    _map.put(key, oldValues);
089    
090                    return oldValues;
091            }
092    
093            public V remove(Object key) {
094                    V value = null;
095    
096                    Set<V> values = _map.remove(key);
097    
098                    if ((values != null) && !values.isEmpty()) {
099                            value = values.iterator().next();
100                    }
101    
102                    return value;
103            }
104    
105            private Map<K, Set<V>> _map = new HashMap<K, Set<V>>();
106    
107    }