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 * @since 3.0
040 * @version $Id: HashedMap.html 887892 2013-11-24 13:43:45Z tn $
041 */
042public class HashedMap<K, V>
043        extends AbstractHashedMap<K, V> implements Serializable, Cloneable {
044
045    /** Serialisation version */
046    private static final long serialVersionUID = -1788199231038721040L;
047
048    /**
049     * Constructs a new empty map with default size and load factor.
050     */
051    public HashedMap() {
052        super(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_THRESHOLD);
053    }
054
055    /**
056     * Constructs a new, empty map with the specified initial capacity.
057     *
058     * @param initialCapacity  the initial capacity
059     * @throws IllegalArgumentException if the initial capacity is negative
060     */
061    public HashedMap(final int initialCapacity) {
062        super(initialCapacity);
063    }
064
065    /**
066     * Constructs a new, empty map with the specified initial capacity and
067     * load factor.
068     *
069     * @param initialCapacity  the initial capacity
070     * @param loadFactor  the load factor
071     * @throws IllegalArgumentException if the initial capacity is negative
072     * @throws IllegalArgumentException if the load factor is less than zero
073     */
074    public HashedMap(final int initialCapacity, final float loadFactor) {
075        super(initialCapacity, loadFactor);
076    }
077
078    /**
079     * Constructor copying elements from another map.
080     *
081     * @param map  the map to copy
082     * @throws NullPointerException if the map is null
083     */
084    public HashedMap(final Map<? extends K, ? extends V> map) {
085        super(map);
086    }
087
088    //-----------------------------------------------------------------------
089    /**
090     * Clones the map without cloning the keys or values.
091     *
092     * @return a shallow clone
093     */
094    @Override
095    public HashedMap<K, V> clone() {
096        return (HashedMap<K, V>) super.clone();
097    }
098
099    /**
100     * Write the map out using a custom routine.
101     */
102    private void writeObject(final ObjectOutputStream out) throws IOException {
103        out.defaultWriteObject();
104        doWriteObject(out);
105    }
106
107    /**
108     * Read the map in using a custom routine.
109     */
110    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
111        in.defaultReadObject();
112        doReadObject(in);
113    }
114
115}