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.collection;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.apache.commons.collections.Transformer;
024:
025: /**
026: * Decorates another <code>Collection</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 TransformedCollection extends
041: AbstractSerializableCollectionDecorator {
042:
043: /** Serialization version */
044: private static final long serialVersionUID = 8692300188161871514L;
045:
046: /** The transformer to use */
047: protected final Transformer transformer;
048:
049: /**
050: * Factory method to create a transforming collection.
051: * <p>
052: * If there are any elements already in the collection being decorated, they
053: * are NOT transformed.
054: *
055: * @param coll the collection to decorate, must not be null
056: * @param transformer the transformer to use for conversion, must not be null
057: * @return a new transformed collection
058: * @throws IllegalArgumentException if collection or transformer is null
059: */
060: public static Collection decorate(Collection coll,
061: Transformer transformer) {
062: return new TransformedCollection(coll, transformer);
063: }
064:
065: //-----------------------------------------------------------------------
066: /**
067: * Constructor that wraps (not copies).
068: * <p>
069: * If there are any elements already in the collection being decorated, they
070: * are NOT transformed.
071: *
072: * @param coll the collection to decorate, must not be null
073: * @param transformer the transformer to use for conversion, must not be null
074: * @throws IllegalArgumentException if collection or transformer is null
075: */
076: protected TransformedCollection(Collection coll,
077: Transformer transformer) {
078: super (coll);
079: if (transformer == null) {
080: throw new IllegalArgumentException(
081: "Transformer must not be null");
082: }
083: this .transformer = transformer;
084: }
085:
086: /**
087: * Transforms an object.
088: * <p>
089: * The transformer itself may throw an exception if necessary.
090: *
091: * @param object the object to transform
092: * @return a transformed object
093: */
094: protected Object transform(Object object) {
095: return transformer.transform(object);
096: }
097:
098: /**
099: * Transforms a collection.
100: * <p>
101: * The transformer itself may throw an exception if necessary.
102: *
103: * @param coll the collection to transform
104: * @return a transformed object
105: */
106: protected Collection transform(Collection coll) {
107: List list = new ArrayList(coll.size());
108: for (Iterator it = coll.iterator(); it.hasNext();) {
109: list.add(transform(it.next()));
110: }
111: return list;
112: }
113:
114: //-----------------------------------------------------------------------
115: public boolean add(Object object) {
116: object = transform(object);
117: return getCollection().add(object);
118: }
119:
120: public boolean addAll(Collection coll) {
121: coll = transform(coll);
122: return getCollection().addAll(coll);
123: }
124:
125: }
|