001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. 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.apache.commons.collections4.multimap; 018 019import java.io.IOException; 020import java.io.ObjectInputStream; 021import java.io.ObjectOutputStream; 022import java.io.Serializable; 023import java.util.LinkedHashMap; 024import java.util.LinkedHashSet; 025import java.util.Map; 026 027import org.apache.commons.collections4.MultiValuedMap; 028 029/** 030 * Implements a {@code SetValuedMap}, using a {@link LinkedHashMap} to provide data 031 * storage and {@link LinkedHashSet}s as value collections. This is the standard 032 * implementation of a SetValuedMap. 033 * <p> 034 * <strong>Note that LinkedHashSetValuedLinkedHashMap is not synchronized and is not 035 * thread-safe.</strong> If you wish to use this map from multiple threads 036 * concurrently, you must use appropriate synchronization. This class may throw 037 * exceptions when accessed by concurrent threads without synchronization. 038 * </p> 039 * 040 * @param <K> the type of the keys in this map 041 * @param <V> the type of the values in this map 042 * @since 4.5.0-M3 043 */ 044public class LinkedHashSetValuedLinkedHashMap<K, V> extends AbstractSetValuedMap<K, V> 045 implements Serializable { 046 047 /** Serialization Version */ 048 private static final long serialVersionUID = 20241020L; 049 050 /** 051 * The initial map capacity used when none specified in constructor. 052 */ 053 private static final int DEFAULT_INITIAL_MAP_CAPACITY = 16; 054 055 /** 056 * The initial set capacity when using none specified in constructor. 057 */ 058 private static final int DEFAULT_INITIAL_SET_CAPACITY = 3; 059 060 /** 061 * The initial list capacity when creating a new value collection. 062 */ 063 private final int initialSetCapacity; 064 065 /** 066 * Creates an empty LinkedHashSetValuedHashMap with the default initial 067 * map capacity (16) and the default initial set capacity (3). 068 */ 069 public LinkedHashSetValuedLinkedHashMap() { 070 this(DEFAULT_INITIAL_MAP_CAPACITY, DEFAULT_INITIAL_SET_CAPACITY); 071 } 072 073 /** 074 * Creates an empty LinkedHashSetValuedHashMap with the default initial 075 * map capacity (16) and the specified initial set capacity. 076 * 077 * @param initialSetCapacity the initial capacity used for value collections 078 */ 079 public LinkedHashSetValuedLinkedHashMap(final int initialSetCapacity) { 080 this(DEFAULT_INITIAL_MAP_CAPACITY, initialSetCapacity); 081 } 082 083 /** 084 * Creates an empty LinkedHashSetValuedHashMap with the specified initial 085 * map and list capacities. 086 * 087 * @param initialMapCapacity the initial hashmap capacity 088 * @param initialSetCapacity the initial capacity used for value collections 089 */ 090 public LinkedHashSetValuedLinkedHashMap(final int initialMapCapacity, final int initialSetCapacity) { 091 super(new LinkedHashMap<>(initialMapCapacity)); 092 this.initialSetCapacity = initialSetCapacity; 093 } 094 095 /** 096 * Creates an LinkedHashSetValuedHashMap copying all the mappings of the given map. 097 * 098 * @param map a {@code Map} to copy into this map 099 */ 100 public LinkedHashSetValuedLinkedHashMap(final Map<? extends K, ? extends V> map) { 101 this(map.size(), DEFAULT_INITIAL_SET_CAPACITY); 102 super.putAll(map); 103 } 104 105 /** 106 * Creates an LinkedHashSetValuedHashMap copying all the mappings of the given map. 107 * 108 * @param map a {@code MultiValuedMap} to copy into this map 109 */ 110 public LinkedHashSetValuedLinkedHashMap(final MultiValuedMap<? extends K, ? extends V> map) { 111 this(map.size(), DEFAULT_INITIAL_SET_CAPACITY); 112 super.putAll(map); 113 } 114 115 @Override 116 protected LinkedHashSet<V> createCollection() { 117 return new LinkedHashSet<>(initialSetCapacity); 118 } 119 120 /** 121 * Deserializes an instance from an ObjectInputStream. 122 * 123 * @param in The source ObjectInputStream. 124 * @throws IOException Any of the usual Input/Output related exceptions. 125 * @throws ClassNotFoundException A class of a serialized object cannot be found. 126 */ 127 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { 128 in.defaultReadObject(); 129 setMap(new LinkedHashMap<>()); 130 doReadObject(in); 131 } 132 133 /** 134 * Serializes this object to an ObjectOutputStream. 135 * 136 * @param out the target ObjectOutputStream. 137 * @throws IOException thrown when an I/O errors occur writing to the target stream. 138 */ 139 private void writeObject(final ObjectOutputStream out) throws IOException { 140 out.defaultWriteObject(); 141 doWriteObject(out); 142 } 143 144}