View Javadoc
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    *      http://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;
18  
19  import java.util.Collection;
20  import java.util.Iterator;
21  import java.util.Set;
22  
23  /**
24   * Defines a collection that counts the number of times an object appears in
25   * the collection.
26   * <p>
27   * Suppose you have a Bag that contains {@code {a, a, b, c}}.
28   * Calling {@link #getCount(Object)} on {@code a} would return 2, while
29   * calling {@link #uniqueSet()} would return {@code {a, b, c}}.
30   * </p>
31   * <p>
32   * <i>NOTE: This interface violates the {@link Collection} contract.</i>
33   * The behavior specified in many of these methods is <i>not</i> the same
34   * as the behavior specified by {@code Collection}.
35   * The non-compliant methods are clearly marked with "(Violation)".
36   * Exercise caution when using a bag as a {@code Collection}.
37   * </p>
38   * <p>
39   * This violation resulted from the original specification of this interface.
40   * In an ideal world, the interface would be changed to fix the problems, however
41   * it has been decided to maintain backwards compatibility instead.
42   * </p>
43   *
44   * @param <E> the type of elements in this bag
45   * @since 2.0
46   */
47  public interface Bag<E> extends Collection<E> {
48  
49      /**
50       * <i>(Violation)</i>
51       * Adds one copy of the specified object to the Bag.
52       * <p>
53       * If the object is already in the {@link #uniqueSet()} then increment its
54       * count as reported by {@link #getCount(Object)}. Otherwise add it to the
55       * {@link #uniqueSet()} and report its count as 1.
56       * </p>
57       * <p>
58       * Since this method always increases the size of the bag,
59       * according to the {@link Collection#add(Object)} contract, it
60       * should always return {@code true}.  Since it sometimes returns
61       * {@code false}, this method violates the contract.
62       * </p>
63       *
64       * @param object  the object to add
65       * @return {@code true} if the object was not already in the {@code uniqueSet}
66       */
67      @Override
68      boolean add(E object);
69  
70      /**
71       * Adds {@code nCopies} copies of the specified object to the Bag.
72       * <p>
73       * If the object is already in the {@link #uniqueSet()} then increment its
74       * count as reported by {@link #getCount(Object)}. Otherwise add it to the
75       * {@link #uniqueSet()} and report its count as {@code nCopies}.
76       * </p>
77       *
78       * @param object  the object to add
79       * @param nCopies  the number of copies to add
80       * @return {@code true} if the object was not already in the {@code uniqueSet}
81       */
82      boolean add(E object, int nCopies);
83  
84      /**
85       * <i>(Violation)</i>
86       * Returns {@code true} if the bag contains all elements in
87       * the given collection, respecting cardinality.  That is, if the
88       * given collection {@code coll} contains {@code n} copies
89       * of a given object, calling {@link #getCount(Object)} on that object must
90       * be {@code &gt;= n} for all {@code n} in {@code coll}.
91       *
92       * <p>
93       * The {@link Collection#containsAll(Collection)} method specifies
94       * that cardinality should <i>not</i> be respected; this method should
95       * return true if the bag contains at least one of every object contained
96       * in the given collection.
97       * </p>
98       *
99       * @param coll  the collection to check against
100      * @return {@code true} if the Bag contains all the collection
101      */
102     @Override
103     boolean containsAll(Collection<?> coll);
104 
105     /**
106      * Returns the number of occurrences (cardinality) of the given
107      * object currently in the bag. If the object does not exist in the
108      * bag, return 0.
109      *
110      * @param object  the object to search for
111      * @return the number of occurrences of the object, zero if not found
112      */
113     int getCount(Object object);
114 
115     /**
116      * Returns an {@link Iterator} over the entire set of members,
117      * including copies due to cardinality. This iterator is fail-fast
118      * and will not tolerate concurrent modifications.
119      *
120      * @return iterator over all elements in the Bag
121      */
122     @Override
123     Iterator<E> iterator();
124 
125     /**
126      * <i>(Violation)</i>
127      * Removes all occurrences of the given object from the bag.
128      * <p>
129      * This will also remove the object from the {@link #uniqueSet()}.
130      * </p>
131      * <p>
132      * According to the {@link Collection#remove(Object)} method,
133      * this method should only remove the <i>first</i> occurrence of the
134      * given object, not <i>all</i> occurrences.
135      * </p>
136      *
137      * @param object  the object to remove
138      * @return {@code true} if this call changed the collection
139      */
140     @Override
141     boolean remove(Object object);
142 
143     /**
144      * Removes {@code nCopies} copies of the specified object from the Bag.
145      * <p>
146      * If the number of copies to remove is greater than the actual number of
147      * copies in the Bag, no error is thrown.
148      * </p>
149      *
150      * @param object  the object to remove
151      * @param nCopies  the number of copies to remove
152      * @return {@code true} if this call changed the collection
153      */
154     boolean remove(Object object, int nCopies);
155 
156     /**
157      * <i>(Violation)</i>
158      * Remove all elements represented in the given collection,
159      * respecting cardinality.  That is, if the given collection
160      * {@code coll} contains {@code n} copies of a given object,
161      * the bag will have {@code n} fewer copies, assuming the bag
162      * had at least {@code n} copies to begin with.
163      *
164      * <p>
165      * The {@link Collection#removeAll(Collection)} method specifies
166      * that cardinality should <i>not</i> be respected; this method should
167      * remove <i>all</i> occurrences of every object contained in the
168      * given collection.
169      * </p>
170      *
171      * @param coll  the collection to remove
172      * @return {@code true} if this call changed the collection
173      */
174     @Override
175     boolean removeAll(Collection<?> coll);
176 
177     /**
178      * <i>(Violation)</i>
179      * Remove any members of the bag that are not in the given
180      * collection, respecting cardinality.  That is, if the given
181      * collection {@code coll} contains {@code n} copies of a
182      * given object and the bag has {@code m &gt; n} copies, then
183      * delete {@code m - n} copies from the bag.  In addition, if
184      * {@code e} is an object in the bag but
185      * {@code !coll.contains(e)}, then remove {@code e} and any
186      * of its copies.
187      *
188      * <p>
189      * The {@link Collection#retainAll(Collection)} method specifies
190      * that cardinality should <i>not</i> be respected; this method should
191      * keep <i>all</i> occurrences of every object contained in the
192      * given collection.
193      * </p>
194      *
195      * @param coll  the collection to retain
196      * @return {@code true} if this call changed the collection
197      */
198     @Override
199     boolean retainAll(Collection<?> coll);
200 
201     /**
202      * Returns the total number of items in the bag across all types.
203      *
204      * @return the total size of the Bag
205      */
206     @Override
207     int size();
208 
209     /**
210      * Returns a {@link Set} of unique elements in the Bag.
211      * <p>
212      * Uniqueness constraints are the same as those in {@link java.util.Set}.
213      * </p>
214      *
215      * @return the Set of unique Bag elements
216      */
217     Set<E> uniqueSet();
218 
219     // The following is not part of the formal Bag interface, however where possible
220     // Bag implementations should follow these comments.
221 //    /**
222 //     * Compares this Bag to another.
223 //     * This Bag equals another Bag if it contains the same number of occurrences of
224 //     * the same elements.
225 //     * This equals definition is compatible with the Set interface.
226 //     *
227 //     * @param obj  the Bag to compare to
228 //     * @return true if equal
229 //     */
230 //    boolean equals(Object obj);
231 //
232 //    /**
233 //     * Gets a hash code for the Bag compatible with the definition of equals.
234 //     * The hash code is defined as the sum total of a hash code for each element.
235 //     * The per element hash code is defined as
236 //     * {@code (e==null ? 0 : e.hashCode()) ^ noOccurrences)}.
237 //     * This hash code definition is compatible with the Set interface.
238 //     *
239 //     * @return the hash code of the Bag
240 //     */
241 //    int hashCode();
242 
243 }