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.Iterator;
  19. import java.util.Map;
  20. import org.apache.commons.collections.Predicate;
  21. import org.apache.commons.collections.Transformer;
  22. /**
  23. * Transformer implementation calls the transformer whose predicate returns true,
  24. * like a switch statement.
  25. *
  26. * @since Commons Collections 3.0
  27. * @version $Revision: 1.5 $ $Date: 2004/05/16 11:36:31 $
  28. *
  29. * @author Stephen Colebourne
  30. */
  31. public class SwitchTransformer implements Transformer, Serializable {
  32. /** Serial version UID */
  33. static final long serialVersionUID = -6404460890903469332L;
  34. /** The tests to consider */
  35. private final Predicate[] iPredicates;
  36. /** The matching transformers to call */
  37. private final Transformer[] iTransformers;
  38. /** The default transformer to call if no tests match */
  39. private final Transformer iDefault;
  40. /**
  41. * Factory method that performs validation and copies the parameter arrays.
  42. *
  43. * @param predicates array of predicates, cloned, no nulls
  44. * @param transformers matching array of transformers, cloned, no nulls
  45. * @param defaultTransformer the transformer to use if no match, null means nop
  46. * @return the <code>chained</code> transformer
  47. * @throws IllegalArgumentException if array is null
  48. * @throws IllegalArgumentException if any element in the array is null
  49. */
  50. public static Transformer getInstance(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) {
  51. FunctorUtils.validate(predicates);
  52. FunctorUtils.validate(transformers);
  53. if (predicates.length != transformers.length) {
  54. throw new IllegalArgumentException("The predicate and transformer arrays must be the same size");
  55. }
  56. if (predicates.length == 0) {
  57. return (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE : defaultTransformer);
  58. }
  59. predicates = FunctorUtils.copy(predicates);
  60. transformers = FunctorUtils.copy(transformers);
  61. return new SwitchTransformer(predicates, transformers, defaultTransformer);
  62. }
  63. /**
  64. * Create a new Transformer that calls one of the transformers depending
  65. * on the predicates.
  66. * <p>
  67. * The Map consists of Predicate keys and Transformer values. A transformer
  68. * is called if its matching predicate returns true. Each predicate is evaluated
  69. * until one returns true. If no predicates evaluate to true, the default
  70. * transformer is called. The default transformer is set in the map with a
  71. * null key. The ordering is that of the iterator() method on the entryset
  72. * collection of the map.
  73. *
  74. * @param predicatesAndTransformers a map of predicates to transformers
  75. * @return the <code>switch</code> transformer
  76. * @throws IllegalArgumentException if the map is null
  77. * @throws IllegalArgumentException if any transformer in the map is null
  78. * @throws ClassCastException if the map elements are of the wrong type
  79. */
  80. public static Transformer getInstance(Map predicatesAndTransformers) {
  81. Transformer[] transformers = null;
  82. Predicate[] preds = null;
  83. if (predicatesAndTransformers == null) {
  84. throw new IllegalArgumentException("The predicate and transformer map must not be null");
  85. }
  86. if (predicatesAndTransformers.size() == 0) {
  87. return ConstantTransformer.NULL_INSTANCE;
  88. }
  89. // convert to array like this to guarantee iterator() ordering
  90. Transformer defaultTransformer = (Transformer) predicatesAndTransformers.remove(null);
  91. int size = predicatesAndTransformers.size();
  92. if (size == 0) {
  93. return (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE : defaultTransformer);
  94. }
  95. transformers = new Transformer[size];
  96. preds = new Predicate[size];
  97. int i = 0;
  98. for (Iterator it = predicatesAndTransformers.entrySet().iterator(); it.hasNext();) {
  99. Map.Entry entry = (Map.Entry) it.next();
  100. preds[i] = (Predicate) entry.getKey();
  101. transformers[i] = (Transformer) entry.getValue();
  102. i++;
  103. }
  104. return new SwitchTransformer(preds, transformers, defaultTransformer);
  105. }
  106. /**
  107. * Constructor that performs no validation.
  108. * Use <code>getInstance</code> if you want that.
  109. *
  110. * @param predicates array of predicates, not cloned, no nulls
  111. * @param transformers matching array of transformers, not cloned, no nulls
  112. * @param defaultTransformer the transformer to use if no match, null means nop
  113. */
  114. public SwitchTransformer(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) {
  115. super();
  116. iPredicates = predicates;
  117. iTransformers = transformers;
  118. iDefault = (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE : defaultTransformer);
  119. }
  120. /**
  121. * Transforms the input to result by calling the transformer whose matching
  122. * predicate returns true.
  123. *
  124. * @param input the input object to transform
  125. * @return the transformed result
  126. */
  127. public Object transform(Object input) {
  128. for (int i = 0; i < iPredicates.length; i++) {
  129. if (iPredicates[i].evaluate(input) == true) {
  130. return iTransformers[i].transform(input);
  131. }
  132. }
  133. return iDefault.transform(input);
  134. }
  135. /**
  136. * Gets the predicates, do not modify the array.
  137. *
  138. * @return the predicates
  139. * @since Commons Collections 3.1
  140. */
  141. public Predicate[] getPredicates() {
  142. return iPredicates;
  143. }
  144. /**
  145. * Gets the transformers, do not modify the array.
  146. *
  147. * @return the transformers
  148. * @since Commons Collections 3.1
  149. */
  150. public Transformer[] getTransformers() {
  151. return iTransformers;
  152. }
  153. /**
  154. * Gets the default transformer.
  155. *
  156. * @return the default transformer
  157. * @since Commons Collections 3.1
  158. */
  159. public Transformer getDefaultTransformer() {
  160. return iDefault;
  161. }
  162. }