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.Closure;
  21. import org.apache.commons.collections.Predicate;
  22. /**
  23. * Closure implementation calls the closure 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:47:38 $
  28. *
  29. * @author Stephen Colebourne
  30. */
  31. public class SwitchClosure implements Closure, Serializable {
  32. /** Serial version UID */
  33. static final long serialVersionUID = 3518477308466486130L;
  34. /** The tests to consider */
  35. private final Predicate[] iPredicates;
  36. /** The matching closures to call */
  37. private final Closure[] iClosures;
  38. /** The default closure to call if no tests match */
  39. private final Closure 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 closures matching array of closures, cloned, no nulls
  45. * @param defaultClosure the closure to use if no match, null means nop
  46. * @return the <code>chained</code> closure
  47. * @throws IllegalArgumentException if array is null
  48. * @throws IllegalArgumentException if any element in the array is null
  49. */
  50. public static Closure getInstance(Predicate[] predicates, Closure[] closures, Closure defaultClosure) {
  51. FunctorUtils.validate(predicates);
  52. FunctorUtils.validate(closures);
  53. if (predicates.length != closures.length) {
  54. throw new IllegalArgumentException("The predicate and closure arrays must be the same size");
  55. }
  56. if (predicates.length == 0) {
  57. return (defaultClosure == null ? NOPClosure.INSTANCE : defaultClosure);
  58. }
  59. predicates = FunctorUtils.copy(predicates);
  60. closures = FunctorUtils.copy(closures);
  61. return new SwitchClosure(predicates, closures, defaultClosure);
  62. }
  63. /**
  64. * Create a new Closure that calls one of the closures depending
  65. * on the predicates.
  66. * <p>
  67. * The Map consists of Predicate keys and Closure values. A closure
  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. * closure is called. The default closure 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 predicatesAndClosures a map of predicates to closures
  75. * @return the <code>switch</code> closure
  76. * @throws IllegalArgumentException if the map is null
  77. * @throws IllegalArgumentException if any closure in the map is null
  78. * @throws ClassCastException if the map elements are of the wrong type
  79. */
  80. public static Closure getInstance(Map predicatesAndClosures) {
  81. Closure[] closures = null;
  82. Predicate[] preds = null;
  83. if (predicatesAndClosures == null) {
  84. throw new IllegalArgumentException("The predicate and closure map must not be null");
  85. }
  86. if (predicatesAndClosures.size() == 0) {
  87. return NOPClosure.INSTANCE;
  88. }
  89. // convert to array like this to guarantee iterator() ordering
  90. Closure defaultClosure = (Closure) predicatesAndClosures.remove(null);
  91. int size = predicatesAndClosures.size();
  92. if (size == 0) {
  93. return (defaultClosure == null ? NOPClosure.INSTANCE : defaultClosure);
  94. }
  95. closures = new Closure[size];
  96. preds = new Predicate[size];
  97. int i = 0;
  98. for (Iterator it = predicatesAndClosures.entrySet().iterator(); it.hasNext();) {
  99. Map.Entry entry = (Map.Entry) it.next();
  100. preds[i] = (Predicate) entry.getKey();
  101. closures[i] = (Closure) entry.getValue();
  102. i++;
  103. }
  104. return new SwitchClosure(preds, closures, defaultClosure);
  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 closures matching array of closures, not cloned, no nulls
  112. * @param defaultClosure the closure to use if no match, null means nop
  113. */
  114. public SwitchClosure(Predicate[] predicates, Closure[] closures, Closure defaultClosure) {
  115. super();
  116. iPredicates = predicates;
  117. iClosures = closures;
  118. iDefault = (defaultClosure == null ? NOPClosure.INSTANCE : defaultClosure);
  119. }
  120. /**
  121. * Executes the closure whose matching predicate returns true
  122. *
  123. * @param input the input object
  124. */
  125. public void execute(Object input) {
  126. for (int i = 0; i < iPredicates.length; i++) {
  127. if (iPredicates[i].evaluate(input) == true) {
  128. iClosures[i].execute(input);
  129. return;
  130. }
  131. }
  132. iDefault.execute(input);
  133. }
  134. /**
  135. * Gets the predicates, do not modify the array.
  136. *
  137. * @return the predicates
  138. * @since Commons Collections 3.1
  139. */
  140. public Predicate[] getPredicates() {
  141. return iPredicates;
  142. }
  143. /**
  144. * Gets the closures, do not modify the array.
  145. *
  146. * @return the closures
  147. * @since Commons Collections 3.1
  148. */
  149. public Closure[] getClosures() {
  150. return iClosures;
  151. }
  152. /**
  153. * Gets the default closure.
  154. *
  155. * @return the default closure
  156. * @since Commons Collections 3.1
  157. */
  158. public Closure getDefaultClosure() {
  159. return iDefault;
  160. }
  161. }