ArrayListValuedHashMap.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.multimap;

  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.io.Serializable;
  22. import java.util.ArrayList;
  23. import java.util.Collection;
  24. import java.util.HashMap;
  25. import java.util.Map;

  26. import org.apache.commons.collections4.MultiValuedMap;

  27. /**
  28.  * Implements a {@code ListValuedMap}, using a {@link HashMap} to provide data
  29.  * storage and {@link ArrayList}s as value collections. This is the standard
  30.  * implementation of a ListValuedMap.
  31.  * <p>
  32.  * <strong>Note that ArrayListValuedHashMap is not synchronized and is not
  33.  * thread-safe.</strong> If you wish to use this map from multiple threads
  34.  * concurrently, you must use appropriate synchronization. This class may throw
  35.  * exceptions when accessed by concurrent threads without synchronization.
  36.  * </p>
  37.  *
  38.  * @param <K> the type of the keys in this map
  39.  * @param <V> the type of the values in this map
  40.  * @since 4.1
  41.  */
  42. public class ArrayListValuedHashMap<K, V> extends AbstractListValuedMap<K, V>
  43.     implements Serializable {

  44.     /** Serialization Version */
  45.     private static final long serialVersionUID = 20151118L;

  46.     /**
  47.      * The initial map capacity used when none specified in constructor.
  48.      */
  49.     private static final int DEFAULT_INITIAL_MAP_CAPACITY = 16;

  50.     /**
  51.      * The initial list capacity when using none specified in constructor.
  52.      */
  53.     private static final int DEFAULT_INITIAL_LIST_CAPACITY = 3;

  54.     /**
  55.      * The initial list capacity when creating a new value collection.
  56.      */
  57.     private final int initialListCapacity;

  58.     /**
  59.      * Creates an empty ArrayListValuedHashMap with the default initial
  60.      * map capacity (16) and the default initial list capacity (3).
  61.      */
  62.     public ArrayListValuedHashMap() {
  63.         this(DEFAULT_INITIAL_MAP_CAPACITY, DEFAULT_INITIAL_LIST_CAPACITY);
  64.     }

  65.     /**
  66.      * Creates an empty ArrayListValuedHashMap with the default initial
  67.      * map capacity (16) and the specified initial list capacity.
  68.      *
  69.      * @param initialListCapacity  the initial capacity used for value collections
  70.      */
  71.     public ArrayListValuedHashMap(final int initialListCapacity) {
  72.         this(DEFAULT_INITIAL_MAP_CAPACITY, initialListCapacity);
  73.     }

  74.     /**
  75.      * Creates an empty ArrayListValuedHashMap with the specified initial
  76.      * map and list capacities.
  77.      *
  78.      * @param initialMapCapacity  the initial hashmap capacity
  79.      * @param initialListCapacity  the initial capacity used for value collections
  80.      */
  81.     public ArrayListValuedHashMap(final int initialMapCapacity, final int initialListCapacity) {
  82.         super(new HashMap<>(initialMapCapacity));
  83.         this.initialListCapacity = initialListCapacity;
  84.     }

  85.     /**
  86.      * Creates an ArrayListValuedHashMap copying all the mappings of the given map.
  87.      *
  88.      * @param map a {@code Map} to copy into this map
  89.      */
  90.     public ArrayListValuedHashMap(final Map<? extends K, ? extends V> map) {
  91.         this(map.size(), DEFAULT_INITIAL_LIST_CAPACITY);
  92.         super.putAll(map);
  93.     }

  94.     /**
  95.      * Creates an ArrayListValuedHashMap copying all the mappings of the given map.
  96.      *
  97.      * @param map a {@code MultiValuedMap} to copy into this map
  98.      */
  99.     public ArrayListValuedHashMap(final MultiValuedMap<? extends K, ? extends V> map) {
  100.         this(map.size(), DEFAULT_INITIAL_LIST_CAPACITY);
  101.         super.putAll(map);
  102.     }

  103.     @Override
  104.     protected ArrayList<V> createCollection() {
  105.         return new ArrayList<>(initialListCapacity);
  106.     }

  107.     /**
  108.      * Deserializes an instance from an ObjectInputStream.
  109.      *
  110.      * @param in The source ObjectInputStream.
  111.      * @throws IOException            Any of the usual Input/Output related exceptions.
  112.      * @throws ClassNotFoundException A class of a serialized object cannot be found.
  113.      */
  114.     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
  115.         in.defaultReadObject();
  116.         setMap(new HashMap<>());
  117.         doReadObject(in);
  118.     }

  119.     /**
  120.      * Trims the capacity of all value collections to their current size.
  121.      */
  122.     public void trimToSize() {
  123.         for (final Collection<V> coll : getMap().values()) {
  124.             final ArrayList<V> list = (ArrayList<V>) coll;
  125.             list.trimToSize();
  126.         }
  127.     }

  128.     /**
  129.      * Serializes this object to an ObjectOutputStream.
  130.      *
  131.      * @param out the target ObjectOutputStream.
  132.      * @throws IOException thrown when an I/O errors occur writing to the target stream.
  133.      */
  134.     private void writeObject(final ObjectOutputStream out) throws IOException {
  135.         out.defaultWriteObject();
  136.         doWriteObject(out);
  137.     }

  138. }