1 16 package org.apache.commons.collections.collection; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.commons.collections.Transformer; 24 25 40 public class TransformedCollection extends AbstractSerializableCollectionDecorator { 41 42 43 private static final long serialVersionUID = 8692300188161871514L; 44 45 46 protected final Transformer transformer; 47 48 59 public static Collection decorate(Collection coll, Transformer transformer) { 60 return new TransformedCollection(coll, transformer); 61 } 62 63 74 protected TransformedCollection(Collection coll, Transformer transformer) { 75 super(coll); 76 if (transformer == null) { 77 throw new IllegalArgumentException ("Transformer must not be null"); 78 } 79 this.transformer = transformer; 80 } 81 82 90 protected Object transform(Object object) { 91 return transformer.transform(object); 92 } 93 94 102 protected Collection transform(Collection coll) { 103 List list = new ArrayList (coll.size()); 104 for (Iterator it = coll.iterator(); it.hasNext(); ) { 105 list.add(transform(it.next())); 106 } 107 return list; 108 } 109 110 public boolean add(Object object) { 112 object = transform(object); 113 return getCollection().add(object); 114 } 115 116 public boolean addAll(Collection coll) { 117 coll = transform(coll); 118 return getCollection().addAll(coll); 119 } 120 121 } 122 | Popular Tags |