001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.bag; 018 019import java.io.IOException; 020import java.io.ObjectInputStream; 021import java.io.ObjectOutputStream; 022import java.util.Collection; 023import java.util.Iterator; 024 025import org.apache.commons.collections4.Bag; 026 027/** 028 * Decorates another {@link Bag} to comply with the Collection contract. 029 * <p> 030 * By decorating an existing {@link Bag} instance with a {@link CollectionBag}, 031 * it can be safely passed on to methods that require Collection types that 032 * are fully compliant with the Collection contract. 033 * <p> 034 * The method javadoc highlights the differences compared to the original Bag interface. 035 * 036 * @see Bag 037 * @param <E> the type of elements in this bag 038 * @since 4.0 039 */ 040public final class CollectionBag<E> extends AbstractBagDecorator<E> { 041 042 /** Serialization version */ 043 private static final long serialVersionUID = -2560033712679053143L; 044 045 /** 046 * Factory method to create a bag that complies to the Collection contract. 047 * 048 * @param <E> the type of the elements in the bag 049 * @param bag the bag to decorate, must not be null 050 * @return a Bag that complies to the Collection contract 051 * @throws NullPointerException if bag is null 052 */ 053 public static <E> Bag<E> collectionBag(final Bag<E> bag) { 054 return new CollectionBag<>(bag); 055 } 056 057 //----------------------------------------------------------------------- 058 /** 059 * Constructor that wraps (not copies). 060 * 061 * @param bag the bag to decorate, must not be null 062 * @throws NullPointerException if bag is null 063 */ 064 public CollectionBag(final Bag<E> bag) { 065 super(bag); 066 } 067 068 //----------------------------------------------------------------------- 069 /** 070 * Write the collection out using a custom routine. 071 * 072 * @param out the output stream 073 * @throws IOException if an error occurs while writing to the stream 074 */ 075 private void writeObject(final ObjectOutputStream out) throws IOException { 076 out.defaultWriteObject(); 077 out.writeObject(decorated()); 078 } 079 080 /** 081 * Read the collection in using a custom routine. 082 * 083 * @param in the input stream 084 * @throws IOException if an error occurs while reading from the stream 085 * @throws ClassNotFoundException if an object read from the stream can not be loaded 086 * @throws ClassCastException if deserialised object has wrong type 087 */ 088 @SuppressWarnings("unchecked") // will throw CCE, see Javadoc 089 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { 090 in.defaultReadObject(); 091 setCollection((Collection<E>) in.readObject()); 092 } 093 094 //----------------------------------------------------------------------- 095 // Collection interface 096 //----------------------------------------------------------------------- 097 098 /** 099 * <i>(Change)</i> 100 * Returns <code>true</code> if the bag contains all elements in 101 * the given collection, <b>not</b> respecting cardinality. That is, 102 * if the given collection <code>coll</code> contains at least one of 103 * every object contained in this object. 104 * 105 * @param coll the collection to check against 106 * @return <code>true</code> if the Bag contains at least one of every object in the collection 107 */ 108 @Override 109 public boolean containsAll(final Collection<?> coll) { 110 final Iterator<?> e = coll.iterator(); 111 while (e.hasNext()) { 112 if(!contains(e.next())) { 113 return false; 114 } 115 } 116 return true; 117 } 118 119 /** 120 * <i>(Change)</i> 121 * Adds one copy of the specified object to the Bag. 122 * <p> 123 * Since this method always increases the size of the bag, it 124 * will always return <code>true</code>. 125 * 126 * @param object the object to add 127 * @return <code>true</code>, always 128 */ 129 @Override 130 public boolean add(final E object) { 131 return add(object, 1); 132 } 133 134 @Override 135 public boolean addAll(final Collection<? extends E> coll) { 136 boolean changed = false; 137 final Iterator<? extends E> i = coll.iterator(); 138 while (i.hasNext()) { 139 final boolean added = add(i.next(), 1); 140 changed = changed || added; 141 } 142 return changed; 143 } 144 145 /** 146 * <i>(Change)</i> 147 * Removes the first occurrence of the given object from the bag. 148 * <p> 149 * This will also remove the object from the {@link #uniqueSet()} if the 150 * bag contains no occurrence anymore of the object after this operation. 151 * 152 * @param object the object to remove 153 * @return <code>true</code> if this call changed the collection 154 */ 155 @Override 156 public boolean remove(final Object object) { 157 return remove(object, 1); 158 } 159 160 /** 161 * <i>(Change)</i> 162 * Remove all elements represented in the given collection, 163 * <b>not</b> respecting cardinality. That is, remove <i>all</i> 164 * occurrences of every object contained in the given collection. 165 * 166 * @param coll the collection to remove 167 * @return <code>true</code> if this call changed the collection 168 */ 169 @Override 170 public boolean removeAll(final Collection<?> coll) { 171 if (coll != null) { 172 boolean result = false; 173 final Iterator<?> i = coll.iterator(); 174 while (i.hasNext()) { 175 final Object obj = i.next(); 176 final boolean changed = remove(obj, getCount(obj)); 177 result = result || changed; 178 } 179 return result; 180 } 181 // let the decorated bag handle the case of null argument 182 return decorated().removeAll(null); 183 } 184 185 /** 186 * <i>(Change)</i> 187 * Remove any members of the bag that are not in the given collection, 188 * <i>not</i> respecting cardinality. That is, any object in the given 189 * collection <code>coll</code> will be retained in the bag with the same 190 * number of copies prior to this operation. All other objects will be 191 * completely removed from this bag. 192 * <p> 193 * This implementation iterates over the elements of this bag, checking 194 * each element in turn to see if it's contained in <code>coll</code>. 195 * If it's not contained, it's removed from this bag. As a consequence, 196 * it is advised to use a collection type for <code>coll</code> that provides 197 * a fast (e.g. O(1)) implementation of {@link Collection#contains(Object)}. 198 * 199 * @param coll the collection to retain 200 * @return <code>true</code> if this call changed the collection 201 */ 202 @Override 203 public boolean retainAll(final Collection<?> coll) { 204 if (coll != null) { 205 boolean modified = false; 206 final Iterator<E> e = iterator(); 207 while (e.hasNext()) { 208 if (!coll.contains(e.next())) { 209 e.remove(); 210 modified = true; 211 } 212 } 213 return modified; 214 } 215 // let the decorated bag handle the case of null argument 216 return decorated().retainAll(null); 217 } 218 219 //----------------------------------------------------------------------- 220 // Bag interface 221 //----------------------------------------------------------------------- 222 223 /** 224 * <i>(Change)</i> 225 * Adds <code>count</code> copies of the specified object to the Bag. 226 * <p> 227 * Since this method always increases the size of the bag, it 228 * will always return <code>true</code>. 229 * 230 * @param object the object to add 231 * @param count the number of copies to add 232 * @return <code>true</code>, always 233 */ 234 @Override 235 public boolean add(final E object, final int count) { 236 decorated().add(object, count); 237 return true; 238 } 239 240}