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.Comparator;
  23. import java.util.SortedMap;
  24. import java.util.TreeMap;
  25. import org.apache.commons.collections.SortedBag;
  26. /**
  27. * Implements <code>SortedBag</code>, using a <code>TreeMap</code> to provide
  28. * the data storage. This is the standard implementation of a sorted bag.
  29. * <p>
  30. * Order will be maintained among the bag members and can be viewed through the
  31. * iterator.
  32. * <p>
  33. * A <code>Bag</code> stores each object in the collection together with a
  34. * count of occurrences. Extra methods on the interface allow multiple copies
  35. * of an object to be added or removed at once. It is important to read the
  36. * interface javadoc carefully as several methods violate the
  37. * <code>Collection</code> interface specification.
  38. *
  39. * @since Commons Collections 3.0 (previously in main package v2.0)
  40. * @version $Revision: 1.9 $ $Date: 2004/02/18 00:56:25 $
  41. *
  42. * @author Chuck Burdick
  43. * @author Stephen Colebourne
  44. */
  45. public class TreeBag
  46. extends AbstractMapBag implements SortedBag, Serializable {
  47. /** Serial version lock */
  48. static final long serialVersionUID = -7740146511091606676L;
  49. /**
  50. * Constructs an empty <code>TreeBag</code>.
  51. */
  52. public TreeBag() {
  53. super(new TreeMap());
  54. }
  55. /**
  56. * Constructs an empty bag that maintains order on its unique
  57. * representative members according to the given {@link Comparator}.
  58. *
  59. * @param comparator the comparator to use
  60. */
  61. public TreeBag(Comparator comparator) {
  62. super(new TreeMap(comparator));
  63. }
  64. /**
  65. * Constructs a <code>TreeBag</code> containing all the members of the
  66. * specified collection.
  67. *
  68. * @param coll the collection to copy into the bag
  69. */
  70. public TreeBag(Collection coll) {
  71. this();
  72. addAll(coll);
  73. }
  74. //-----------------------------------------------------------------------
  75. public Object first() {
  76. return ((SortedMap) getMap()).firstKey();
  77. }
  78. public Object last() {
  79. return ((SortedMap) getMap()).lastKey();
  80. }
  81. public Comparator comparator() {
  82. return ((SortedMap) getMap()).comparator();
  83. }
  84. //-----------------------------------------------------------------------
  85. /**
  86. * Write the bag out using a custom routine.
  87. */
  88. private void writeObject(ObjectOutputStream out) throws IOException {
  89. out.defaultWriteObject();
  90. out.writeObject(comparator());
  91. super.doWriteObject(out);
  92. }
  93. /**
  94. * Read the bag in using a custom routine.
  95. */
  96. private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
  97. in.defaultReadObject();
  98. Comparator comp = (Comparator) in.readObject();
  99. super.doReadObject(new TreeMap(comp), in);
  100. }
  101. }