HashSetValuedHashMap.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.HashMap;
  23. import java.util.HashSet;
  24. import java.util.Map;

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

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

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

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

  49.     /**
  50.      * The initial set capacity when using none specified in constructor.
  51.      */
  52.     private static final int DEFAULT_INITIAL_SET_CAPACITY = 3;

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

  57.     /**
  58.      * Creates an empty HashSetValuedHashMap with the default initial
  59.      * map capacity (16) and the default initial set capacity (3).
  60.      */
  61.     public HashSetValuedHashMap() {
  62.         this(DEFAULT_INITIAL_MAP_CAPACITY, DEFAULT_INITIAL_SET_CAPACITY);
  63.     }

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

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

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

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

  102.     @Override
  103.     protected HashSet<V> createCollection() {
  104.         return new HashSet<>(initialSetCapacity);
  105.     }

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

  118.     /**
  119.      * Serializes this object to an ObjectOutputStream.
  120.      *
  121.      * @param out the target ObjectOutputStream.
  122.      * @throws IOException thrown when an I/O errors occur writing to the target stream.
  123.      */
  124.     private void writeObject(final ObjectOutputStream out) throws IOException {
  125.         out.defaultWriteObject();
  126.         doWriteObject(out);
  127.     }

  128. }