1. /*
  2. * Copyright 2002-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.bag;
  17. import java.io.IOException;
  18. import java.io.ObjectInputStream;
  19. import java.io.ObjectOutputStream;
  20. import java.io.Serializable;
  21. import java.util.Collection;
  22. import java.util.HashMap;
  23. import org.apache.commons.collections.Bag;
  24. /**
  25. * Implements <code>Bag</code>, using a <code>HashMap</code> to provide the
  26. * data storage. This is the standard implementation of a bag.
  27. * <p>
  28. * A <code>Bag</code> stores each object in the collection together with a
  29. * count of occurrences. Extra methods on the interface allow multiple copies
  30. * of an object to be added or removed at once. It is important to read the
  31. * interface javadoc carefully as several methods violate the
  32. * <code>Collection</code> interface specification.
  33. *
  34. * @since Commons Collections 3.0 (previously in main package v2.0)
  35. * @version $Revision: 1.8 $ $Date: 2004/02/18 00:56:25 $
  36. *
  37. * @author Chuck Burdick
  38. * @author Stephen Colebourne
  39. */
  40. public class HashBag
  41. extends AbstractMapBag implements Bag, Serializable {
  42. /** Serial version lock */
  43. static final long serialVersionUID = -6561115435802554013L;
  44. /**
  45. * Constructs an empty <code>HashBag</code>.
  46. */
  47. public HashBag() {
  48. super(new HashMap());
  49. }
  50. /**
  51. * Constructs a bag containing all the members of the given collection.
  52. *
  53. * @param coll a collection to copy into this bag
  54. */
  55. public HashBag(Collection coll) {
  56. this();
  57. addAll(coll);
  58. }
  59. //-----------------------------------------------------------------------
  60. /**
  61. * Write the bag out using a custom routine.
  62. */
  63. private void writeObject(ObjectOutputStream out) throws IOException {
  64. out.defaultWriteObject();
  65. super.doWriteObject(out);
  66. }
  67. /**
  68. * Read the bag in using a custom routine.
  69. */
  70. private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
  71. in.defaultReadObject();
  72. super.doReadObject(new HashMap(), in);
  73. }
  74. }