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 * https://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
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.Serializable;
23 import java.util.LinkedHashMap;
24 import java.util.LinkedHashSet;
25 import java.util.Map;
26
27 import org.apache.commons.collections4.MultiMapUtils;
28 import org.apache.commons.collections4.MultiValuedMap;
29
30 /**
31 * Implements a {@code SetValuedMap}, using a {@link LinkedHashMap} to provide data
32 * storage and {@link LinkedHashSet}s as value collections. This is the standard
33 * implementation of a SetValuedMap.
34 * <p>
35 * <strong>Note that LinkedHashSetValuedLinkedHashMap is not synchronized and is not
36 * thread-safe.</strong> If you wish to use this map from multiple threads
37 * concurrently, you must use appropriate synchronization. This class may throw
38 * exceptions when accessed by concurrent threads without synchronization.
39 * </p>
40 *
41 * @param <K> The type of the keys in this map
42 * @param <V> The type of the values in this map
43 * @since 4.5.0-M3
44 */
45 public class LinkedHashSetValuedLinkedHashMap<K, V> extends AbstractSetValuedMap<K, V>
46 implements Serializable {
47
48 /** Serialization Version */
49 private static final long serialVersionUID = 20241020L;
50
51 /**
52 * The initial map capacity used when none specified in constructor.
53 */
54 private static final int DEFAULT_INITIAL_MAP_CAPACITY = 16;
55
56 /**
57 * The initial set capacity when using none specified in constructor.
58 */
59 private static final int DEFAULT_INITIAL_SET_CAPACITY = 3;
60
61 /**
62 * The initial list capacity when creating a new value collection.
63 */
64 private final int initialSetCapacity;
65
66 /**
67 * Creates an empty LinkedHashSetValuedHashMap with the default initial
68 * map capacity (16) and the default initial set capacity (3).
69 */
70 public LinkedHashSetValuedLinkedHashMap() {
71 this(DEFAULT_INITIAL_MAP_CAPACITY, DEFAULT_INITIAL_SET_CAPACITY);
72 }
73
74 /**
75 * Creates an empty LinkedHashSetValuedHashMap with the default initial
76 * map capacity (16) and the specified initial set capacity.
77 *
78 * @param initialSetCapacity The initial capacity used for value collections
79 */
80 public LinkedHashSetValuedLinkedHashMap(final int initialSetCapacity) {
81 this(DEFAULT_INITIAL_MAP_CAPACITY, initialSetCapacity);
82 }
83
84 /**
85 * Creates an empty LinkedHashSetValuedHashMap with the specified initial
86 * map and list capacities.
87 *
88 * @param initialMapCapacity The initial hashmap capacity
89 * @param initialSetCapacity The initial capacity used for value collections
90 */
91 public LinkedHashSetValuedLinkedHashMap(final int initialMapCapacity, final int initialSetCapacity) {
92 super(new LinkedHashMap<>(initialMapCapacity));
93 this.initialSetCapacity = initialSetCapacity;
94 }
95
96 /**
97 * Creates an LinkedHashSetValuedHashMap copying all the mappings of the given map.
98 *
99 * @param map A {@code Map} to copy into this map
100 */
101 public LinkedHashSetValuedLinkedHashMap(final Map<? extends K, ? extends V> map) {
102 this(map.size(), DEFAULT_INITIAL_SET_CAPACITY);
103 super.putAll(map);
104 }
105
106 /**
107 * Creates an LinkedHashSetValuedHashMap copying all the mappings of the given map.
108 *
109 * @param map A {@code MultiValuedMap} to copy into this map
110 */
111 public LinkedHashSetValuedLinkedHashMap(final MultiValuedMap<? extends K, ? extends V> map) {
112 this(map.size(), DEFAULT_INITIAL_SET_CAPACITY);
113 super.putAll(map);
114 }
115
116 @Override
117 protected LinkedHashSet<V> createCollection() {
118 return new LinkedHashSet<>(initialSetCapacity);
119 }
120
121 @Override
122 public LinkedHashSetValuedLinkedHashMap<V, K> inverted() {
123 return MultiMapUtils.invert(this, new LinkedHashSetValuedLinkedHashMap<V, K>());
124 }
125
126 /**
127 * Deserializes an instance from an ObjectInputStream.
128 *
129 * @param in The source ObjectInputStream.
130 * @throws IOException Any of the usual Input/Output related exceptions.
131 * @throws ClassNotFoundException A class of a serialized object cannot be found.
132 */
133 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
134 in.defaultReadObject();
135 setMap(new LinkedHashMap<>());
136 doReadObject(in);
137 }
138
139 /**
140 * Serializes this object to an ObjectOutputStream.
141 *
142 * @param out The target ObjectOutputStream.
143 * @throws IOException thrown when an I/O errors occur writing to the target stream.
144 */
145 private void writeObject(final ObjectOutputStream out) throws IOException {
146 out.defaultWriteObject();
147 doWriteObject(out);
148 }
149
150 }