View Javadoc

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 net.sf.collections15.functors.closure;
17  
18  import net.sf.collections15.Closure;
19  import net.sf.collections15.Predicate;
20  
21  import java.io.Serializable;
22  
23  /***
24   * <code>Closure</code> decorator that conditionally executes a decorated
25   * <code>Closure</code> based on a predicate, like an <code>if</code>
26   * statement.
27   *
28   * @author Stephen Colebourne
29   * @author Chris Lambrou (port to Java 5.0)
30   * @since Collections15 1.0
31   */
32  public class IfClosure <E> implements Closure<E>, Serializable
33  {
34  
35      static final long serialVersionUID = 3330475392299965720L;
36  
37      /***
38       * The condition to determine whether or not the decorated
39       * <code>Closure</code> executes.
40       */
41      protected final Predicate<? super E> predicate;
42  
43      /***
44       * The decorated <code>Closure</code> to execute if the condition evaluates
45       * to <code>true</code>.
46       */
47      protected final Closure<? super E> trueClosure;
48  
49  
50      /***
51       * Decorates a <code>Closure</code>, returning a <code>Closure</code> that
52       * executes only if a specified condition is met.
53       *
54       * @param predicate   The condition that must evaluate to <code>true</code>
55       *                    if the <code>Closure</code> is to execute.
56       * @param trueClosure The conditionally executed <code>Closure</code>.
57       *
58       * @return A <code>Closure</code> that executes only if the
59       *         <code>Predicate</code> evaluates to <code>true</code>.
60       *
61       * @throws IllegalArgumentException Thrown if any argument is <code>null</code>.
62       */
63      public static <T> Closure<T> decorate(Predicate<? super T> predicate, Closure<? super T> trueClosure)
64      {
65          return new IfClosure<T>(predicate, trueClosure);
66      }
67  
68      /***
69       * Creates a <code>Closure</code>, that executes an existing
70       * <code>Closure</code> only if a specified condition is met.
71       *
72       * @param predicate   The condition that must evaluate to <code>true</code>
73       *                    if the <code>Closure</code> is to execute.
74       * @param trueClosure The conditionally executed <code>Closure</code>.
75       *
76       * @throws IllegalArgumentException Thrown if any argument is <code>null</code>.
77       */
78      protected IfClosure(Predicate<? super E> predicate, Closure<? super E> trueClosure)
79      {
80          if (predicate == null) {
81              throw new IllegalArgumentException("Predicate must not be null");
82          }
83          if (trueClosure == null) {
84              throw new IllegalArgumentException("True Closure must not be null");
85          }
86          this.predicate = predicate;
87          this.trueClosure = trueClosure;
88      }
89  
90      /***
91       * Conditionally executes the <code>Closure</code> according to the result
92       * of the <code>Predicate</code>.
93       *
94       * @param input The input object to act upon.
95       */
96      public void execute(E input)
97      {
98          if (predicate.evaluate(input)) {
99              trueClosure.execute(input);
100         }
101     }
102 
103     /***
104      * Gets the <code>Predicate</code>.
105      *
106      * @return The <code>Predicate</code>.
107      *
108      * @since Collections15 1.0
109      */
110     public Predicate<? super E> getPredicate()
111     {
112         return predicate;
113     }
114 
115     /***
116      * Gets the <code>Closure</code> executed when the <code>Predicate</code>
117      * evaluate to <code>true</code>.
118      *
119      * @return The <code>true Closure</code>.
120      *
121      * @since Commons Collections15 1.0
122      */
123     public Closure<? super E> getTrueClosure()
124     {
125         return trueClosure;
126     }
127 }