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.map;
018
019import java.io.IOException;
020import java.io.ObjectInputStream;
021import java.io.ObjectOutputStream;
022import java.io.Serializable;
023import java.util.Map;
024
025/**
026 * A <code>Map</code> implementation that is a general purpose alternative
027 * to <code>HashMap</code>.
028 * <p>
029 * This implementation improves on the JDK1.4 HashMap by adding the
030 * {@link org.apache.commons.collections4.MapIterator MapIterator}
031 * functionality and many methods for subclassing.
032 * <p>
033 * <strong>Note that HashedMap is not synchronized and is not thread-safe.</strong>
034 * If you wish to use this map from multiple threads concurrently, you must use
035 * appropriate synchronization. The simplest approach is to wrap this map
036 * using {@link java.util.Collections#synchronizedMap(Map)}. This class may throw
037 * exceptions when accessed by concurrent threads without synchronization.
038 *
039 * @param <K> the type of the keys in this map
040 * @param <V> the type of the values in this map
041 * @since 3.0
042 */
043public class HashedMap<K, V>
044        extends AbstractHashedMap<K, V> implements Serializable, Cloneable {
045
046    /** Serialisation version */
047    private static final long serialVersionUID = -1788199231038721040L;
048
049    /**
050     * Constructs a new empty map with default size and load factor.
051     */
052    public HashedMap() {
053        super(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_THRESHOLD);
054    }
055
056    /**
057     * Constructs a new, empty map with the specified initial capacity.
058     *
059     * @param initialCapacity  the initial capacity
060     * @throws IllegalArgumentException if the initial capacity is negative
061     */
062    public HashedMap(final int initialCapacity) {
063        super(initialCapacity);
064    }
065
066    /**
067     * Constructs a new, empty map with the specified initial capacity and
068     * load factor.
069     *
070     * @param initialCapacity  the initial capacity
071     * @param loadFactor  the load factor
072     * @throws IllegalArgumentException if the initial capacity is negative
073     * @throws IllegalArgumentException if the load factor is less than zero
074     */
075    public HashedMap(final int initialCapacity, final float loadFactor) {
076        super(initialCapacity, loadFactor);
077    }
078
079    /**
080     * Constructor copying elements from another map.
081     *
082     * @param map  the map to copy
083     * @throws NullPointerException if the map is null
084     */
085    public HashedMap(final Map<? extends K, ? extends V> map) {
086        super(map);
087    }
088
089    //-----------------------------------------------------------------------
090    /**
091     * Clones the map without cloning the keys or values.
092     *
093     * @return a shallow clone
094     */
095    @Override
096    public HashedMap<K, V> clone() {
097        return (HashedMap<K, V>) super.clone();
098    }
099
100    /**
101     * Write the map out using a custom routine.
102     *
103     * @param out  the output stream
104     * @throws IOException if an error occurs while writing to the stream
105     */
106    private void writeObject(final ObjectOutputStream out) throws IOException {
107        out.defaultWriteObject();
108        doWriteObject(out);
109    }
110
111    /**
112     * Read the map in using a custom routine.
113     *
114     * @param in the input stream
115     * @throws IOException if an error occurs while reading from the stream
116     * @throws ClassNotFoundException if an object read from the stream can not be loaded
117     */
118    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
119        in.defaultReadObject();
120        doReadObject(in);
121    }
122
123}