1. /*
  2. * Copyright 2003-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections.map;
  17. import java.io.IOException;
  18. import java.io.ObjectInputStream;
  19. import java.io.ObjectOutputStream;
  20. import java.io.Serializable;
  21. import java.util.Map;
  22. /**
  23. * A <code>Map</code> implementation that is a general purpose alternative
  24. * to <code>HashMap</code>.
  25. * <p>
  26. * This implementation improves on the JDK1.4 HashMap by adding the
  27. * {@link org.apache.commons.collections.MapIterator MapIterator}
  28. * functionality and many methods for subclassing.
  29. * <p>
  30. * @since Commons Collections 3.0
  31. * @version $Revision: 1.15 $ $Date: 2004/02/18 01:13:19 $
  32. *
  33. * @author Stephen Colebourne
  34. */
  35. public class HashedMap
  36. extends AbstractHashedMap implements Serializable, Cloneable {
  37. /** Serialisation version */
  38. private static final long serialVersionUID = -1788199231038721040L;
  39. /**
  40. * Constructs a new empty map with default size and load factor.
  41. */
  42. public HashedMap() {
  43. super(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_THRESHOLD);
  44. }
  45. /**
  46. * Constructs a new, empty map with the specified initial capacity.
  47. *
  48. * @param initialCapacity the initial capacity
  49. * @throws IllegalArgumentException if the initial capacity is less than one
  50. */
  51. public HashedMap(int initialCapacity) {
  52. super(initialCapacity);
  53. }
  54. /**
  55. * Constructs a new, empty map with the specified initial capacity and
  56. * load factor.
  57. *
  58. * @param initialCapacity the initial capacity
  59. * @param loadFactor the load factor
  60. * @throws IllegalArgumentException if the initial capacity is less than one
  61. * @throws IllegalArgumentException if the load factor is less than zero
  62. */
  63. public HashedMap(int initialCapacity, float loadFactor) {
  64. super(initialCapacity, loadFactor);
  65. }
  66. /**
  67. * Constructor copying elements from another map.
  68. *
  69. * @param map the map to copy
  70. * @throws NullPointerException if the map is null
  71. */
  72. public HashedMap(Map map) {
  73. super(map);
  74. }
  75. //-----------------------------------------------------------------------
  76. /**
  77. * Clones the map without cloning the keys or values.
  78. *
  79. * @return a shallow clone
  80. */
  81. public Object clone() {
  82. return super.clone();
  83. }
  84. /**
  85. * Write the map out using a custom routine.
  86. */
  87. private void writeObject(ObjectOutputStream out) throws IOException {
  88. out.defaultWriteObject();
  89. doWriteObject(out);
  90. }
  91. /**
  92. * Read the map in using a custom routine.
  93. */
  94. private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
  95. in.defaultReadObject();
  96. doReadObject(in);
  97. }
  98. }