1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.collections.functors;
18
19 import java.io.Serializable;
20
21 import org.apache.commons.collections.Closure;
22 import org.apache.commons.collections.Predicate;
23
24 /**
25 * Closure implementation acts as an if statement calling one or other closure
26 * based on a predicate.
27 *
28 * @since 3.0
29 * @version $Id: IfClosure.java 1436624 2013-01-21 21:15:19Z tn $
30 */
31 public class IfClosure<E> implements Closure<E>, Serializable {
32
33 /** Serial version UID */
34 private static final long serialVersionUID = 3518477308466486130L;
35
36 /** The test */
37 private final Predicate<? super E> iPredicate;
38 /** The closure to use if true */
39 private final Closure<? super E> iTrueClosure;
40 /** The closure to use if false */
41 private final Closure<? super E> iFalseClosure;
42
43 /**
44 * Factory method that performs validation.
45 * <p>
46 * This factory creates a closure that performs no action when
47 * the predicate is false.
48 *
49 * @param <E> the type that the closure acts on
50 * @param predicate predicate to switch on
51 * @param trueClosure closure used if true
52 * @return the <code>if</code> closure
53 * @throws IllegalArgumentException if either argument is null
54 * @since 3.2
55 */
56 public static <E> Closure<E> ifClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure) {
57 return IfClosure.<E>ifClosure(predicate, trueClosure, NOPClosure.<E>nopClosure());
58 }
59
60 /**
61 * Factory method that performs validation.
62 *
63 * @param <E> the type that the closure acts on
64 * @param predicate predicate to switch on
65 * @param trueClosure closure used if true
66 * @param falseClosure closure used if false
67 * @return the <code>if</code> closure
68 * @throws IllegalArgumentException if any argument is null
69 */
70 public static <E> Closure<E> ifClosure(final Predicate<? super E> predicate,
71 final Closure<? super E> trueClosure,
72 final Closure<? super E> falseClosure) {
73 if (predicate == null) {
74 throw new IllegalArgumentException("Predicate must not be null");
75 }
76 if (trueClosure == null || falseClosure == null) {
77 throw new IllegalArgumentException("Closures must not be null");
78 }
79 return new IfClosure<E>(predicate, trueClosure, falseClosure);
80 }
81
82 /**
83 * Constructor that performs no validation.
84 * Use <code>getInstance</code> if you want that.
85 * <p>
86 * This constructor creates a closure that performs no action when
87 * the predicate is false.
88 *
89 * @param predicate predicate to switch on, not null
90 * @param trueClosure closure used if true, not null
91 * @since 3.2
92 */
93 public IfClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure) {
94 this(predicate, trueClosure, NOPClosure.INSTANCE);
95 }
96
97 /**
98 * Constructor that performs no validation.
99 * Use <code>getInstance</code> if you want that.
100 *
101 * @param predicate predicate to switch on, not null
102 * @param trueClosure closure used if true, not null
103 * @param falseClosure closure used if false, not null
104 */
105 public IfClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure,
106 final Closure<? super E> falseClosure) {
107 super();
108 iPredicate = predicate;
109 iTrueClosure = trueClosure;
110 iFalseClosure = falseClosure;
111 }
112
113 /**
114 * Executes the true or false closure according to the result of the predicate.
115 *
116 * @param input the input object
117 */
118 public void execute(final E input) {
119 if (iPredicate.evaluate(input)) {
120 iTrueClosure.execute(input);
121 } else {
122 iFalseClosure.execute(input);
123 }
124 }
125
126 /**
127 * Gets the predicate.
128 *
129 * @return the predicate
130 * @since 3.1
131 */
132 public Predicate<? super E> getPredicate() {
133 return iPredicate;
134 }
135
136 /**
137 * Gets the closure called when true.
138 *
139 * @return the closure
140 * @since 3.1
141 */
142 public Closure<? super E> getTrueClosure() {
143 return iTrueClosure;
144 }
145
146 /**
147 * Gets the closure called when false.
148 *
149 * @return the closure
150 * @since 3.1
151 */
152 public Closure<? super E> getFalseClosure() {
153 return iFalseClosure;
154 }
155
156 }