001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 The ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package org.conqat.lib.commons.collections;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.List;
022
023import org.conqat.lib.commons.assertion.CCSMAssert;
024
025/**
026 * Manages a map of lists, i.e. each key can store multiple elements.
027 * 
028 * @param <K>
029 *            the key type.
030 * @param <V>
031 *            the value type (i.e. the values stored in the collections).
032 */
033public class ListMap<K, V> extends CollectionMap<K, V, List<V>> {
034
035        private static final long serialVersionUID = 1L;
036
037        /** Create new hashed list map. */
038        public ListMap() {
039                // Nothing to do
040        }
041
042        /** Copy constructor. */
043        public ListMap(ListMap<K, V> other) {
044                this();
045                addAll(other);
046        }
047
048        /** Adds the value only if the key and value are not null */
049        public boolean addIfNotNull(K key, V value) {
050                if (key != null && value != null) {
051                        return add(key, value);
052                }
053                return true;
054        }
055
056        /**
057         * Creates a list map from a string array provided it's length is even
058         */
059        public static ListMap<String, String> of(String... keysAndValues) {
060                CCSMAssert.isTrue(keysAndValues.length % 2 == 0, "Expecting even number of arguments.");
061                ListMap<String, String> listMap = new ListMap<>();
062                for (int i = 0; i < keysAndValues.length; i += 2) {
063                        listMap.add(keysAndValues[i], keysAndValues[i + 1]);
064                }
065
066                return listMap;
067        }
068
069        /**
070         * Creates a new ListMap of the given key/values.
071         */
072        public static <K, V> ListMap<K, V> of(K key, Collection<? extends V> values) {
073                ListMap<K, V> listMap = new ListMap<>();
074                listMap.addAll(key, values);
075                return listMap;
076        }
077
078        /** {@inheritDoc} */
079        @Override
080        protected List<V> createNewCollection() {
081                return new ArrayList<>();
082        }
083
084}