001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.common.collection;
017
018import java.util.Collection;
019import java.util.Collections;
020import java.util.LinkedHashMap;
021import java.util.LinkedHashSet;
022
023/**
024 * A {@link Multimap} implementation that uses an {@link LinkedHashSet} to store the values associated with a key. This
025 * implementation does not allow duplicates and the values are ordered.
026 * 
027 * @param <K> the key type
028 * @param <V> the value type
029 */
030public class LinkedHashMultimap<K, V> extends AbstractMultimap<K, V> {
031
032    /**
033     * Creates a new, empty {@code LinkedHashMultimap} (that allows no duplicates) with the default initial capacity.
034     * 
035     * @param <K> the key type
036     * @param <V> the value type
037     * @return the new linked-hash multimap; never null
038     */
039    public static <K, V> LinkedHashMultimap<K, V> create() {
040        return new LinkedHashMultimap<K, V>();
041    }
042
043    protected LinkedHashMultimap() {
044        // Use a LinkedHashMap to maintain insertion order of the keys ...
045        super(new LinkedHashMap<K, Collection<V>>());
046    }
047
048    @Override
049    protected Collection<V> createCollection() {
050        return new LinkedHashSet<V>();
051    }
052
053    @Override
054    protected Collection<V> createUnmodifiableEmptyCollection() {
055        return Collections.emptyList();
056    }
057}