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