001    /**
002     * Copyright (c) 2000-2010 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.io.Serializable;
018    
019    import java.util.Collection;
020    import java.util.HashSet;
021    import java.util.Map;
022    import java.util.Set;
023    
024    /**
025     * @author Alexander Chow
026     */
027    public abstract class MultiValueMap
028            <K extends Serializable, V extends Serializable> implements Map<K, V> {
029    
030            public Set<Map.Entry<K, V>> entrySet() {
031                    throw new UnsupportedOperationException();
032            }
033    
034            public V get(Object key) {
035                    throw new UnsupportedOperationException();
036            }
037    
038            public abstract Set<V> getAll(Object key);
039    
040            public abstract Set<V> putAll(K key, Collection<? extends V> values);
041    
042            public void putAll(Map<? extends K, ? extends V> map) {
043                    MultiValueMap<? extends K, ? extends V> multiValueMap = null;
044    
045                    if (map instanceof MultiValueMap<?, ?>) {
046                            multiValueMap = (MultiValueMap<? extends K, ? extends V>)map;
047                    }
048    
049                    for (K key : map.keySet()) {
050                            if (multiValueMap != null) {
051                                    putAll(key, multiValueMap.getAll(key));
052                            }
053                            else {
054                                    put(key, map.get(key));
055                            }
056                    }
057            }
058    
059            public int size() {
060                    int size = 0;
061    
062                    for (K key : keySet()) {
063                            size += size(key);
064                    }
065    
066                    return size;
067            }
068    
069            public int size(Object key) {
070                    int size = 0;
071    
072                    Collection<V> values = getAll(key);
073    
074                    if (values != null) {
075                            size = values.size();
076                    }
077    
078                    return size;
079            }
080    
081            public Collection<V> values() {
082                    Set<V> values = new HashSet<V>();
083    
084                    Set<K> keys = keySet();
085    
086                    for (K key : keys) {
087                            values.addAll(getAll(key));
088                    }
089    
090                    return values;
091            }
092    
093    }