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.ArrayList; 019import java.util.Collection; 020import java.util.Collections; 021import java.util.HashMap; 022import java.util.List; 023 024/** 025 * A {@link Multimap} implementation that uses an {@link ArrayList} to store the values associated with a key. This implementation 026 * allows duplicates and the values are ordered. 027 * 028 * @param <K> the key type 029 * @param <V> the value type 030 */ 031public class ArrayListMultimap<K, V> extends AbstractMultimap<K, V> implements ListMultimap<K, V> { 032 033 /** 034 * Creates a new, empty {@code LinkedHashMultimap} (that allows no duplicates) with the default initial capacity. 035 * 036 * @param <K> the key type 037 * @param <V> the value type 038 * @return the new linked-hash multimap; never null 039 */ 040 public static <K, V> ArrayListMultimap<K, V> create() { 041 return new ArrayListMultimap<K, V>(); 042 } 043 044 protected ArrayListMultimap() { 045 super(new HashMap<K, Collection<V>>()); 046 } 047 048 @Override 049 protected Collection<V> createCollection() { 050 return new ArrayList<V>(); 051 } 052 053 @Override 054 protected Collection<V> createUnmodifiableEmptyCollection() { 055 return Collections.emptyList(); 056 } 057 058 @Override 059 public List<V> get( K key ) { 060 return (List<V>)super.get(key); 061 } 062}