View Javadoc

1   package net.sf.collections15.functors.closure;
2   
3   import net.sf.collections15.Closure;
4   import net.sf.collections15.Predicate;
5   
6   /***
7    * <code>Closure</code> decorator that conditionally executes one of two
8    * <code>Closure</code>s based on a predicate, like an <code>if-else</code>
9    * statement.
10   *
11   * @author Chris Lambrou
12   */
13  public class IfElseClosure <E> extends IfClosure<E>
14  {
15      /***
16       * The <code>Closure</code> to execute if the condition evaluates to
17       * <code>false</code>.
18       */
19      private final Closure<? super E> falseClosure;
20  
21      /***
22       * Returns a <code>Closure</code>, that executes one of two
23       * <code>Closure</code>s depending upon the outcome of a condition.
24       *
25       * @param predicate    The condition that determeins which <code>Closure</code>
26       *                     to execute.
27       * @param trueClosure  The <code>Closure</code>executed if the condition
28       *                     evaluates to <code>true</code>.
29       * @param falseClosure The <code>Closure</code>executed if the condition
30       *                     evaluates to <code>false</code>.
31       *
32       * @return A <code>Closure</code> that conditionally executes one of the
33       *         specified <code>Closure</code>s depending upon the outcome of the
34       *         <code>Predicate</code>.
35       *
36       * @throws IllegalArgumentException Thrown if any argument is <code>null</code>.
37       */
38      public static <T> Closure<T> getInstance(Predicate<? super T> predicate,
39                                               Closure<? super T> trueClosure, Closure<? super T> falseClosure)
40      {
41          return new IfElseClosure<T>(predicate, trueClosure, falseClosure);
42      }
43  
44      /***
45       * Creates a <code>Closure</code>, that executes one of two
46       * <code>Closure</code>s depending upon the outcome of a condition.
47       *
48       * @param predicate    The condition that determeins which <code>Closure</code>
49       *                     to execute.
50       * @param trueClosure  The <code>Closure</code>executed if the condition
51       *                     evaluates to <code>true</code>.
52       * @param falseClosure The <code>Closure</code>executed if the condition
53       *                     evaluates to <code>false</code>.
54       *
55       * @throws IllegalArgumentException Thrown if any argument is <code>null</code>.
56       */
57      protected IfElseClosure(Predicate<? super E> predicate,
58                              Closure<? super E> trueClosure, Closure<? super E> falseClosure)
59      {
60          super(predicate, trueClosure);
61          if (falseClosure == null) {
62              throw new IllegalArgumentException("False Closure must not be null");
63          }
64          this.falseClosure = falseClosure;
65      }
66  
67      /***
68       * Conditionally executes one of the two decorated <code>Closure</code>s
69       * according to the result of the <code>Predicate</code>.
70       *
71       * @param input The input object to act upon.
72       */
73      public void execute(E input)
74      {
75          if (predicate.evaluate(input)) {
76              trueClosure.execute(input);
77          }
78          else {
79              falseClosure.execute(input);
80          }
81      }
82  
83      /***
84       * Gets the <code>Closure</code> executed when the <code>Predicate</code>
85       * evaluates to <code>false</code>.
86       *
87       * @return The <code>false Closure</code>.
88       *
89       * @since Commons Collections15 1.0
90       */
91      public Closure<? super E> getTrueClosure()
92      {
93          return falseClosure;
94      }
95  }