Source of Bag.java


  1: import java.util.Collection;

  3: /**
  4:  * A type intermediate between {@link java.util.List List}s and 
  5:  * {@link java.util.Set Set}s: it allows duplicates, but is not sequential.
  6:  * This type is sometimes called <em>MultiSet</em>.
  7:  * <p>
  8:  * This version of the interface extends the Collection interface, and so
  9:  * contains more methods than the one from last week. Various of the inherited
 10:  * methods have been given default implementations.
 11:  * <p>
 12:  * <b>NOTE:</b>
 13:  * The default implementations are provided as an example of using default
 14:  * method definitions in an interface. <b>In this case</b> these default
 15:  * implementations are not ideal, and should not be included in the interface.
 16:  * The version of <b>Bag</b> used later in teh course will not include them.
 17:  * We will discuss the reasons behind that in class.
 18:  *
 19:  * @param <T> the base type of this Bag
 20:  * @author Mark Young (A00000000)
 21:  */
 22: public interface Bag<T> extends Collection<T> {

 24:     /**
 25:      * Remove an item from this Bag. The item to remove is at the discretion of
 26:      * the implementation.
 27:      *
 28:      * @return an arbitrary item removed from this Bag
 29:      */
 30:     public T remove();

 32:     /**
 33:      * Count how many times the given item appears in this Bag. That is, count
 34:      * each {@code e} such that {@code Objects.equals(anItem, e)}.
 35:      *
 36:      * @param anItem the item to count
 37:      * @return how many times that item appears in this Bag
 38:      */
 39:     public int count(T anItem);
 40:     
 41:     /*
 42:      * Some default implementations for methods. I don't know why Collection
 43:      * doesn't give these itself!
 44:      */
 45:     
 46:     /**
 47:      * Add all of the elements from collection {@code c} to this Bag.
 48:      *
 49:      * @param c the collection whose elements are to be added to this Bag
 50:      * @return true if the Bag has elements added, false if not (this is the 
 51:      * behaviour required by the {@link java.util.Collection Collection}
 52:      * interface)
 53:      */
 54:     @Override
 55:     public default boolean addAll(Collection<? extends T> c) {
 56:         int oldSize = size();
 57:         for (var item : c) {
 58:             add(item);
 59:         }
 60:         return oldSize != size();
 61:     }
 62:     
 63:     /**
 64:      * Remove all elements from this Bag.
 65:      */
 66:     @Override
 67:     public default void clear() {
 68:         while (!isEmpty()) {
 69:             remove();
 70:         }
 71:     }
 72:     
 73:     /**
 74:      * Check whether this Bag contains each of the elements of {@code c}.
 75:      *
 76:      * @param c the collection of items to check for
 77:      * @return true if this Bag contains all those items; false otherwise
 78:      */
 79:     @Override
 80:     public default boolean containsAll(Collection<?> c) {
 81:         for (var obj : c) {
 82:             if (!contains(obj)) {
 83:                 return false;
 84:             }
 85:         }
 86:         return true;
 87:     }
 88:     
 89:     /**
 90:      * Check whether this Bag is empty.
 91:      *
 92:      * @return true if this Bag is empty; false otherwise
 93:      */
 94:     @Override
 95:     public default boolean isEmpty() {
 96:         return size() == 0;
 97:     }
 98:     
 99:     /**
100:      * Remove every element of {@code c} from this Bag. Only one copy of each
101:      * item is removed, unless {@code c} itself contains duplicates, in which
102:      * case the same number of that item is removed from this Bag.
103:      * <p>
104:      * The collection {@code c} may contain items that do not appear in this
105:      * Bag, and for those items nothing is removed from this Bag, and no
106:      * exception is thrown.
107:      *
108:      * @param c the collection whose elements are to be removed from this Bag
109:      * @return true if this Bag has had elements removed; false otherwise
110:      */
111:     @Override
112:     public default boolean removeAll(Collection<?> c) {
113:         int oldSize = size();
114:         for (var obj : c) {
115:             remove(obj);
116:         }
117:         return oldSize != size();
118:     }
119:     
120:     /**
121:      * Keep only the items in collection {@code c} in this Bag, removing all 
122:      * others. All duplicates in this Bag are either retained (if in {@code c}
123:      * or removed (if not in {@code c}) uniformly.
124:      * <p>
125:      * Items in {@code c} that do not appear in this Bag have no effect.
126:      *
127:      * @param c the collection whose elements are to be retained in this Bag
128:      * @return true if items were removed from this Bag; false otherwise
129:      */
130:     @Override
131:     public default boolean retainAll(Collection<?> c) {
132:         int oldSize = size();
133:         for (Object obj : toArray()) {
134:             if (!c.contains(obj)) {
135:                 remove(obj);
136:             }
137:         }
138:         return oldSize != size();
139:     }

141: }