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.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            @Override
031            public Set<Map.Entry<K, V>> entrySet() {
032                    throw new UnsupportedOperationException();
033            }
034    
035            @Override
036            public V get(Object key) {
037                    throw new UnsupportedOperationException();
038            }
039    
040            public abstract Set<V> getAll(Object key);
041    
042            public abstract Set<V> putAll(K key, Collection<? extends V> values);
043    
044            @Override
045            public void putAll(Map<? extends K, ? extends V> map) {
046                    MultiValueMap<? extends K, ? extends V> multiValueMap = null;
047    
048                    if (map instanceof MultiValueMap<?, ?>) {
049                            multiValueMap = (MultiValueMap<? extends K, ? extends V>)map;
050                    }
051    
052                    for (K key : map.keySet()) {
053                            if (multiValueMap != null) {
054                                    putAll(key, multiValueMap.getAll(key));
055                            }
056                            else {
057                                    put(key, map.get(key));
058                            }
059                    }
060            }
061    
062            @Override
063            public int size() {
064                    int size = 0;
065    
066                    for (K key : keySet()) {
067                            size += size(key);
068                    }
069    
070                    return size;
071            }
072    
073            public int size(Object key) {
074                    int size = 0;
075    
076                    Collection<V> values = getAll(key);
077    
078                    if (values != null) {
079                            size = values.size();
080                    }
081    
082                    return size;
083            }
084    
085            @Override
086            public Collection<V> values() {
087                    Set<V> values = new HashSet<V>();
088    
089                    Set<K> keys = keySet();
090    
091                    for (K key : keys) {
092                            values.addAll(getAll(key));
093                    }
094    
095                    return values;
096            }
097    
098    }