1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.collections4.bag;
18
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.Serializable;
23 import java.util.Collection;
24 import java.util.Comparator;
25 import java.util.Objects;
26 import java.util.SortedMap;
27 import java.util.TreeMap;
28
29 import org.apache.commons.collections4.Bag;
30 import org.apache.commons.collections4.SortedBag;
31 import org.apache.commons.collections4.multiset.TreeMultiSet;
32
33 /**
34 * Implements {@link SortedBag}, using a {@link TreeMap} to provide the data storage.
35 * This is the standard implementation of a sorted bag.
36 * <p>
37 * Order will be maintained among the bag members and can be viewed through the iterator.
38 * </p>
39 * <p>
40 * A {@link Bag Bag} stores each object in the collection
41 * together with a count of occurrences. Extra methods on the interface allow multiple
42 * copies of an object to be added or removed at once. It is important to read the interface
43 * Javadoc carefully as several methods violate the {@link Collection} interface specification.
44 * </p>
45 * <p>
46 * <strong>Note that TreeBag is not synchronized and is not thread-safe.</strong>
47 * If you wish to use this bag from multiple threads concurrently, you must use
48 * appropriate synchronization. The simplest approach is to wrap this bag using
49 * {@link org.apache.commons.collections4.BagUtils#synchronizedSortedBag(SortedBag)}.
50 * Unsynchronized concurrent modification can corrupt the structure of the backing
51 * {@link TreeMap}, and a malformed tree may cause subsequent operations, including
52 * reads, to enter an infinite loop.
53 * </p>
54 *
55 * @param <E> The type of elements in this bag
56 * @since 3.0 (previously in main package v2.0)
57 * @deprecated Since 4.6.0, use {@link TreeMultiSet} instead.
58 */
59 @Deprecated
60 public class TreeBag<E> extends AbstractMapBag<E> implements SortedBag<E>, Serializable {
61
62 /** Serial version lock */
63 private static final long serialVersionUID = -7740146511091606676L;
64
65 /**
66 * Constructs an empty {@link TreeBag}.
67 */
68 public TreeBag() {
69 super(new TreeMap<>());
70 }
71
72 /**
73 * Constructs a {@link TreeBag} containing all the members of the
74 * specified collection.
75 *
76 * @param coll The collection to copy into the bag
77 */
78 public TreeBag(final Collection<? extends E> coll) {
79 this();
80 addAll(coll);
81 }
82
83 /**
84 * Constructs an empty bag that maintains order on its unique representative
85 * members according to the given {@link Comparator}.
86 *
87 * @param comparator The comparator to use
88 */
89 public TreeBag(final Comparator<? super E> comparator) {
90 super(new TreeMap<>(comparator));
91 }
92
93 /**
94 * Constructs a bag containing all the members of the given Iterable.
95 *
96 * @param iterable An iterable to copy into this bag.
97 * @since 4.5.0-M3
98 */
99 public TreeBag(final Iterable<? extends E> iterable) {
100 super(new TreeMap<>(), iterable);
101 }
102
103 /**
104 * {@inheritDoc}
105 *
106 * @throws IllegalArgumentException if the object to be added does not implement
107 * {@link Comparable} and the {@link TreeBag} is using natural ordering
108 * @throws NullPointerException if the specified key is null and this bag uses
109 * natural ordering, or its comparator does not permit null keys
110 */
111 @Override
112 public boolean add(final E object) {
113 if (comparator() == null && !(object instanceof Comparable)) {
114 Objects.requireNonNull(object, "object");
115 throw new IllegalArgumentException("Objects of type " + object.getClass() + " cannot be added to " +
116 "a naturally ordered TreeBag as it does not implement Comparable");
117 }
118 return super.add(object);
119 }
120
121 @Override
122 public Comparator<? super E> comparator() {
123 return getMap().comparator();
124 }
125
126 @Override
127 public E first() {
128 return getMap().firstKey();
129 }
130
131 @Override
132 protected SortedMap<E, AbstractMapBag.MutableInteger> getMap() {
133 return (SortedMap<E, AbstractMapBag.MutableInteger>) super.getMap();
134 }
135
136 @Override
137 public E last() {
138 return getMap().lastKey();
139 }
140
141 /**
142 * Deserializes the bag in using a custom routine.
143 *
144 * @param in The input stream
145 * @throws IOException Thrown if an error occurs while reading from the stream
146 * @throws ClassNotFoundException if an object read from the stream cannot be loaded
147 */
148 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
149 in.defaultReadObject();
150 @SuppressWarnings("unchecked") // This will fail at runtime if the stream is incorrect
151 final Comparator<? super E> comp = (Comparator<? super E>) in.readObject();
152 super.doReadObject(new TreeMap<>(comp), in);
153 }
154
155 /**
156 * Serializes this object to an ObjectOutputStream.
157 *
158 * @param out The target ObjectOutputStream.
159 * @throws IOException thrown when an I/O errors occur writing to the target stream.
160 */
161 private void writeObject(final ObjectOutputStream out) throws IOException {
162 out.defaultWriteObject();
163 out.writeObject(comparator());
164 super.doWriteObject(out);
165 }
166
167 }