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 *      https://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.MultiMapUtils;
028import org.apache.commons.collections4.MultiValuedMap;
029
030/**
031 * Implements a {@code SetValuedMap}, using a {@link LinkedHashMap} to provide data
032 * storage and {@link LinkedHashSet}s as value collections. This is the standard
033 * implementation of a SetValuedMap.
034 * <p>
035 * <strong>Note that LinkedHashSetValuedLinkedHashMap is not synchronized and is not
036 * thread-safe.</strong> If you wish to use this map from multiple threads
037 * concurrently, you must use appropriate synchronization. This class may throw
038 * exceptions when accessed by concurrent threads without synchronization.
039 * </p>
040 *
041 * @param <K> The type of the keys in this map
042 * @param <V> The type of the values in this map
043 * @since 4.5.0-M3
044 */
045public class LinkedHashSetValuedLinkedHashMap<K, V> extends AbstractSetValuedMap<K, V>
046    implements Serializable {
047
048    /** Serialization Version */
049    private static final long serialVersionUID = 20241020L;
050
051    /**
052     * The initial map capacity used when none specified in constructor.
053     */
054    private static final int DEFAULT_INITIAL_MAP_CAPACITY = 16;
055
056    /**
057     * The initial set capacity when using none specified in constructor.
058     */
059    private static final int DEFAULT_INITIAL_SET_CAPACITY = 3;
060
061    /**
062     * The initial list capacity when creating a new value collection.
063     */
064    private final int initialSetCapacity;
065
066    /**
067     * Creates an empty LinkedHashSetValuedHashMap with the default initial
068     * map capacity (16) and the default initial set capacity (3).
069     */
070    public LinkedHashSetValuedLinkedHashMap() {
071        this(DEFAULT_INITIAL_MAP_CAPACITY, DEFAULT_INITIAL_SET_CAPACITY);
072    }
073
074    /**
075     * Creates an empty LinkedHashSetValuedHashMap with the default initial
076     * map capacity (16) and the specified initial set capacity.
077     *
078     * @param initialSetCapacity  The initial capacity used for value collections
079     */
080    public LinkedHashSetValuedLinkedHashMap(final int initialSetCapacity) {
081        this(DEFAULT_INITIAL_MAP_CAPACITY, initialSetCapacity);
082    }
083
084    /**
085     * Creates an empty LinkedHashSetValuedHashMap with the specified initial
086     * map and list capacities.
087     *
088     * @param initialMapCapacity  The initial hashmap capacity
089     * @param initialSetCapacity  The initial capacity used for value collections
090     */
091    public LinkedHashSetValuedLinkedHashMap(final int initialMapCapacity, final int initialSetCapacity) {
092        super(new LinkedHashMap<>(initialMapCapacity));
093        this.initialSetCapacity = initialSetCapacity;
094    }
095
096    /**
097     * Creates an LinkedHashSetValuedHashMap copying all the mappings of the given map.
098     *
099     * @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}