1. /*
  2. * Copyright 2001-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.functors;
  17. import java.io.Serializable;
  18. import java.util.Collection;
  19. import java.util.Iterator;
  20. import org.apache.commons.collections.Transformer;
  21. /**
  22. * Transformer implementation that chains the specified transformers together.
  23. * <p>
  24. * The input object is passed to the first transformer. The transformed result
  25. * is passed to the second transformer and so on.
  26. *
  27. * @since Commons Collections 3.0
  28. * @version $Revision: 1.7 $ $Date: 2004/05/16 11:36:31 $
  29. *
  30. * @author Stephen Colebourne
  31. */
  32. public class ChainedTransformer implements Transformer, Serializable {
  33. /** Serial version UID */
  34. static final long serialVersionUID = 3514945074733160196L;
  35. /** The transformers to call in turn */
  36. private final Transformer[] iTransformers;
  37. /**
  38. * Factory method that performs validation and copies the parameter array.
  39. *
  40. * @param transformers the transformers to chain, copied, no nulls
  41. * @return the <code>chained</code> transformer
  42. * @throws IllegalArgumentException if the transformers array is null
  43. * @throws IllegalArgumentException if any transformer in the array is null
  44. */
  45. public static Transformer getInstance(Transformer[] transformers) {
  46. FunctorUtils.validate(transformers);
  47. if (transformers.length == 0) {
  48. return NOPTransformer.INSTANCE;
  49. }
  50. transformers = FunctorUtils.copy(transformers);
  51. return new ChainedTransformer(transformers);
  52. }
  53. /**
  54. * Create a new Transformer that calls each transformer in turn, passing the
  55. * result into the next transformer. The ordering is that of the iterator()
  56. * method on the collection.
  57. *
  58. * @param transformers a collection of transformers to chain
  59. * @return the <code>chained</code> transformer
  60. * @throws IllegalArgumentException if the transformers collection is null
  61. * @throws IllegalArgumentException if any transformer in the collection is null
  62. */
  63. public static Transformer getInstance(Collection transformers) {
  64. if (transformers == null) {
  65. throw new IllegalArgumentException("Transformer collection must not be null");
  66. }
  67. if (transformers.size() == 0) {
  68. return NOPTransformer.INSTANCE;
  69. }
  70. // convert to array like this to guarantee iterator() ordering
  71. Transformer[] cmds = new Transformer[transformers.size()];
  72. int i = 0;
  73. for (Iterator it = transformers.iterator(); it.hasNext();) {
  74. cmds[i++] = (Transformer) it.next();
  75. }
  76. FunctorUtils.validate(cmds);
  77. return new ChainedTransformer(cmds);
  78. }
  79. /**
  80. * Factory method that performs validation.
  81. *
  82. * @param transformer1 the first transformer, not null
  83. * @param transformer2 the second transformer, not null
  84. * @return the <code>chained</code> transformer
  85. * @throws IllegalArgumentException if either transformer is null
  86. */
  87. public static Transformer getInstance(Transformer transformer1, Transformer transformer2) {
  88. if (transformer1 == null || transformer2 == null) {
  89. throw new IllegalArgumentException("Transformers must not be null");
  90. }
  91. Transformer[] transformers = new Transformer[] { transformer1, transformer2 };
  92. return new ChainedTransformer(transformers);
  93. }
  94. /**
  95. * Constructor that performs no validation.
  96. * Use <code>getInstance</code> if you want that.
  97. *
  98. * @param transformers the transformers to chain, not copied, no nulls
  99. */
  100. public ChainedTransformer(Transformer[] transformers) {
  101. super();
  102. iTransformers = transformers;
  103. }
  104. /**
  105. * Transforms the input to result via each decorated transformer
  106. *
  107. * @param object the input object passed to the first transformer
  108. * @return the transformed result
  109. */
  110. public Object transform(Object object) {
  111. for (int i = 0; i < iTransformers.length; i++) {
  112. object = iTransformers[i].transform(object);
  113. }
  114. return object;
  115. }
  116. /**
  117. * Gets the transformers, do not modify the array.
  118. * @return the transformers
  119. * @since Commons Collections 3.1
  120. */
  121. public Transformer[] getTransformers() {
  122. return iTransformers;
  123. }
  124. }