1. /*
  2. * Copyright 2003-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections.collection;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import org.apache.commons.collections.Transformer;
  22. /**
  23. * Decorates another <code>Collection</code> to transform objects that are added.
  24. * <p>
  25. * The add methods are affected by this class.
  26. * Thus objects must be removed or searched for using their transformed form.
  27. * For example, if the transformation converts Strings to Integers, you must
  28. * use the Integer form to remove objects.
  29. * <p>
  30. * This class is Serializable from Commons Collections 3.1.
  31. *
  32. * @since Commons Collections 3.0
  33. * @version $Revision: 1.7 $ $Date: 2004/06/03 22:02:13 $
  34. *
  35. * @author Stephen Colebourne
  36. */
  37. public class TransformedCollection extends AbstractSerializableCollectionDecorator {
  38. /** Serialization version */
  39. private static final long serialVersionUID = 8692300188161871514L;
  40. /** The transformer to use */
  41. protected final Transformer transformer;
  42. /**
  43. * Factory method to create a transforming collection.
  44. * <p>
  45. * If there are any elements already in the collection being decorated, they
  46. * are NOT transformed.
  47. *
  48. * @param coll the collection to decorate, must not be null
  49. * @param transformer the transformer to use for conversion, must not be null
  50. * @return a new transformed collection
  51. * @throws IllegalArgumentException if collection or transformer is null
  52. */
  53. public static Collection decorate(Collection coll, Transformer transformer) {
  54. return new TransformedCollection(coll, transformer);
  55. }
  56. //-----------------------------------------------------------------------
  57. /**
  58. * Constructor that wraps (not copies).
  59. * <p>
  60. * If there are any elements already in the collection being decorated, they
  61. * are NOT transformed.
  62. *
  63. * @param coll the collection to decorate, must not be null
  64. * @param transformer the transformer to use for conversion, must not be null
  65. * @throws IllegalArgumentException if collection or transformer is null
  66. */
  67. protected TransformedCollection(Collection coll, Transformer transformer) {
  68. super(coll);
  69. if (transformer == null) {
  70. throw new IllegalArgumentException("Transformer must not be null");
  71. }
  72. this.transformer = transformer;
  73. }
  74. /**
  75. * Transforms an object.
  76. * <p>
  77. * The transformer itself may throw an exception if necessary.
  78. *
  79. * @param object the object to transform
  80. * @return a transformed object
  81. */
  82. protected Object transform(Object object) {
  83. return transformer.transform(object);
  84. }
  85. /**
  86. * Transforms a collection.
  87. * <p>
  88. * The transformer itself may throw an exception if necessary.
  89. *
  90. * @param coll the collection to transform
  91. * @return a transformed object
  92. */
  93. protected Collection transform(Collection coll) {
  94. List list = new ArrayList(coll.size());
  95. for (Iterator it = coll.iterator(); it.hasNext(); ) {
  96. list.add(transform(it.next()));
  97. }
  98. return list;
  99. }
  100. //-----------------------------------------------------------------------
  101. public boolean add(Object object) {
  102. object = transform(object);
  103. return getCollection().add(object);
  104. }
  105. public boolean addAll(Collection coll) {
  106. coll = transform(coll);
  107. return getCollection().addAll(coll);
  108. }
  109. }