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 */
017 package org.apache.commons.collections.bag;
018
019 import java.util.Set;
020
021 import org.apache.commons.collections.Bag;
022 import org.apache.commons.collections.Transformer;
023 import org.apache.commons.collections.collection.TransformedCollection;
024 import org.apache.commons.collections.set.TransformedSet;
025
026 /**
027 * Decorates another <code>Bag</code> to transform objects that are added.
028 * <p>
029 * The add methods are affected by this class.
030 * Thus objects must be removed or searched for using their transformed form.
031 * For example, if the transformation converts Strings to Integers, you must
032 * use the Integer form to remove objects.
033 * <p>
034 * This class is Serializable from Commons Collections 3.1.
035 *
036 * @since Commons Collections 3.0
037 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
038 *
039 * @author Stephen Colebourne
040 */
041 public class TransformedBag
042 extends TransformedCollection implements Bag {
043
044 /** Serialization version */
045 private static final long serialVersionUID = 5421170911299074185L;
046
047 /**
048 * Factory method to create a transforming bag.
049 * <p>
050 * If there are any elements already in the bag being decorated, they
051 * are NOT transformed.
052 *
053 * @param bag the bag to decorate, must not be null
054 * @param transformer the transformer to use for conversion, must not be null
055 * @return a new transformed Bag
056 * @throws IllegalArgumentException if bag or transformer is null
057 */
058 public static Bag decorate(Bag bag, Transformer transformer) {
059 return new TransformedBag(bag, transformer);
060 }
061
062 //-----------------------------------------------------------------------
063 /**
064 * Constructor that wraps (not copies).
065 * <p>
066 * If there are any elements already in the bag being decorated, they
067 * are NOT transformed.
068 *
069 * @param bag the bag to decorate, must not be null
070 * @param transformer the transformer to use for conversion, must not be null
071 * @throws IllegalArgumentException if bag or transformer is null
072 */
073 protected TransformedBag(Bag bag, Transformer transformer) {
074 super(bag, transformer);
075 }
076
077 /**
078 * Gets the decorated bag.
079 *
080 * @return the decorated bag
081 */
082 protected Bag getBag() {
083 return (Bag) collection;
084 }
085
086 //-----------------------------------------------------------------------
087 public int getCount(Object object) {
088 return getBag().getCount(object);
089 }
090
091 public boolean remove(Object object, int nCopies) {
092 return getBag().remove(object, nCopies);
093 }
094
095 //-----------------------------------------------------------------------
096 public boolean add(Object object, int nCopies) {
097 object = transform(object);
098 return getBag().add(object, nCopies);
099 }
100
101 public Set uniqueSet() {
102 Set set = getBag().uniqueSet();
103 return TransformedSet.decorate(set, transformer);
104 }
105
106 }