001    /**
002     * Copyright (c) 2000-2012 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            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            @Override
054            public Set<V> getAll(Object key) {
055                    return _map.get(key);
056            }
057    
058            public boolean isEmpty() {
059                    return _map.isEmpty();
060            }
061    
062            public Set<K> keySet() {
063                    return _map.keySet();
064            }
065    
066            public V put(K key, V value) {
067                    Set<V> values = _map.get(key);
068    
069                    if (values == null) {
070                            values = new HashSet<V>();
071                    }
072    
073                    values.add(value);
074    
075                    _map.put(key, values);
076    
077                    return value;
078            }
079    
080            @Override
081            public Set<V> putAll(K key, Collection<? extends V> values) {
082                    Set<V> oldValues = _map.get(key);
083    
084                    if (oldValues == null) {
085                            oldValues = new HashSet<V>();
086                    }
087    
088                    oldValues.addAll(values);
089    
090                    _map.put(key, oldValues);
091    
092                    return oldValues;
093            }
094    
095            public V remove(Object key) {
096                    V value = null;
097    
098                    Set<V> values = _map.remove(key);
099    
100                    if ((values != null) && !values.isEmpty()) {
101                            value = values.iterator().next();
102                    }
103    
104                    return value;
105            }
106    
107            private Map<K, Set<V>> _map = new HashMap<K, Set<V>>();
108    
109    }