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    *      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.util.Collection;
23  import java.util.Iterator;
24  
25  import org.apache.commons.collections4.SortedBag;
26  import org.apache.commons.collections4.SortedMultiSet;
27  
28  /**
29   * Decorates another {@link SortedBag} to comply with the Collection contract.
30   *
31   * @param <E> The type of elements in this bag
32   * @since 4.0
33   * @deprecated Since 4.6.0, no longer needed; a {@link SortedMultiSet} already complies with the {@link Collection} contract.
34   */
35  @Deprecated
36  public final class CollectionSortedBag<E> extends AbstractSortedBagDecorator<E> {
37  
38      /** Serialization version */
39      private static final long serialVersionUID = -2560033712679053143L;
40  
41      /**
42       * Factory method to create a sorted bag that complies to the Collection contract.
43       *
44       * @param <E> The type of the elements in the bag
45       * @param bag  The sorted bag to decorate, must not be null
46       * @return A SortedBag that complies to the Collection contract
47       * @throws NullPointerException if bag is null
48       */
49      public static <E> SortedBag<E> collectionSortedBag(final SortedBag<E> bag) {
50          return new CollectionSortedBag<>(bag);
51      }
52  
53      /**
54       * Constructor that wraps (not copies).
55       *
56       * @param bag  The sorted bag to decorate, must not be null
57       * @throws NullPointerException if bag is null
58       */
59      public CollectionSortedBag(final SortedBag<E> bag) {
60          super(bag);
61      }
62  
63      @Override
64      public boolean add(final E object) {
65          return add(object, 1);
66      }
67  
68      @Override
69      public boolean add(final E object, final int count) {
70          decorated().add(object, count);
71          return true;
72      }
73  
74      // Collection interface
75  
76      @Override
77      public boolean addAll(final Collection<? extends E> coll) {
78          boolean changed = false;
79          for (final E current : coll) {
80              final boolean added = add(current, 1);
81              changed = changed || added;
82          }
83          return changed;
84      }
85  
86      @Override
87      public boolean containsAll(final Collection<?> coll) {
88          return coll.stream().allMatch(this::contains);
89      }
90  
91      /**
92       * Deserializes the collection in using a custom routine.
93       *
94       * @param in  The input stream
95       * @throws IOException Thrown if an error occurs while reading from the stream
96       * @throws ClassNotFoundException if an object read from the stream cannot be loaded
97       * @throws ClassCastException if deserialized object has wrong type
98       */
99      @SuppressWarnings("unchecked") // will throw CCE, see Javadoc
100     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
101         in.defaultReadObject();
102         setCollection((Collection<E>) in.readObject());
103     }
104 
105     @Override
106     public boolean remove(final Object object) {
107         return remove(object, 1);
108     }
109 
110     @Override
111     public boolean removeAll(final Collection<?> coll) {
112         if (coll != null) {
113             boolean result = false;
114             for (final Object obj : coll) {
115                 final boolean changed = remove(obj, getCount(obj));
116                 result = result || changed;
117             }
118             return result;
119         }
120         // let the decorated bag handle the case of null argument
121         return decorated().removeAll(null);
122     }
123 
124     @Override
125     public boolean retainAll(final Collection<?> coll) {
126         if (coll != null) {
127             boolean modified = false;
128             final Iterator<E> e = iterator();
129             while (e.hasNext()) {
130                 if (!coll.contains(e.next())) {
131                     e.remove();
132                     modified = true;
133                 }
134             }
135             return modified;
136         }
137         // let the decorated bag handle the case of null argument
138         return decorated().retainAll(null);
139     }
140 
141     /**
142      * Serializes this object to an ObjectOutputStream.
143      *
144      * @param out The target ObjectOutputStream.
145      * @throws IOException thrown when an I/O errors occur writing to the target stream.
146      */
147     private void writeObject(final ObjectOutputStream out) throws IOException {
148         out.defaultWriteObject();
149         out.writeObject(decorated());
150     }
151 
152 }