001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.bag;
017:
018: import java.util.Set;
019:
020: import org.apache.commons.collections.Bag;
021: import org.apache.commons.collections.Transformer;
022: import org.apache.commons.collections.collection.TransformedCollection;
023: import org.apache.commons.collections.set.TransformedSet;
024:
025: /**
026: * Decorates another <code>Bag</code> to transform objects that are added.
027: * <p>
028: * The add methods are affected by this class.
029: * Thus objects must be removed or searched for using their transformed form.
030: * For example, if the transformation converts Strings to Integers, you must
031: * use the Integer form to remove objects.
032: * <p>
033: * This class is Serializable from Commons Collections 3.1.
034: *
035: * @since Commons Collections 3.0
036: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
037: *
038: * @author Stephen Colebourne
039: */
040: public class TransformedBag extends TransformedCollection implements
041: Bag {
042:
043: /** Serialization version */
044: private static final long serialVersionUID = 5421170911299074185L;
045:
046: /**
047: * Factory method to create a transforming bag.
048: * <p>
049: * If there are any elements already in the bag being decorated, they
050: * are NOT transformed.
051: *
052: * @param bag the bag to decorate, must not be null
053: * @param transformer the transformer to use for conversion, must not be null
054: * @return a new transformed Bag
055: * @throws IllegalArgumentException if bag or transformer is null
056: */
057: public static Bag decorate(Bag bag, Transformer transformer) {
058: return new TransformedBag(bag, transformer);
059: }
060:
061: //-----------------------------------------------------------------------
062: /**
063: * Constructor that wraps (not copies).
064: * <p>
065: * If there are any elements already in the bag being decorated, they
066: * are NOT transformed.
067: *
068: * @param bag the bag to decorate, must not be null
069: * @param transformer the transformer to use for conversion, must not be null
070: * @throws IllegalArgumentException if bag or transformer is null
071: */
072: protected TransformedBag(Bag bag, Transformer transformer) {
073: super (bag, transformer);
074: }
075:
076: /**
077: * Gets the decorated bag.
078: *
079: * @return the decorated bag
080: */
081: protected Bag getBag() {
082: return (Bag) collection;
083: }
084:
085: //-----------------------------------------------------------------------
086: public int getCount(Object object) {
087: return getBag().getCount(object);
088: }
089:
090: public boolean remove(Object object, int nCopies) {
091: return getBag().remove(object, nCopies);
092: }
093:
094: //-----------------------------------------------------------------------
095: public boolean add(Object object, int nCopies) {
096: object = transform(object);
097: return getBag().add(object, nCopies);
098: }
099:
100: public Set uniqueSet() {
101: Set set = getBag().uniqueSet();
102: return TransformedSet.decorate(set, transformer);
103: }
104:
105: }
|