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 * https://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.collections4.functors;
18
19 import java.io.Serializable;
20 import java.util.Objects;
21
22 import org.apache.commons.collections4.Closure;
23 import org.apache.commons.collections4.Predicate;
24
25 /**
26 * Closure implementation acts as an if statement calling one or other closure
27 * based on a predicate.
28 *
29 * @param <T> The type of the input to the operation.
30 * @since 3.0
31 */
32 public class IfClosure<T> implements Closure<T>, Serializable {
33
34 /** Serial version UID */
35 private static final long serialVersionUID = 3518477308466486130L;
36
37 /**
38 * Factory method that performs validation.
39 * <p>
40 * This factory creates a closure that performs no action when
41 * the predicate is false.
42 * </p>
43 *
44 * @param <E> The type that the closure acts on
45 * @param predicate predicate to switch on
46 * @param trueClosure closure used if true
47 * @return The {@code if} closure
48 * @throws NullPointerException if either argument is null
49 * @since 3.2
50 */
51 public static <E> Closure<E> ifClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure) {
52 return IfClosure.<E>ifClosure(predicate, trueClosure, NOPClosure.<E>nopClosure());
53 }
54
55 /**
56 * Factory method that performs validation.
57 *
58 * @param <E> The type that the closure acts on
59 * @param predicate predicate to switch on
60 * @param trueClosure closure used if true
61 * @param falseClosure closure used if false
62 * @return The {@code if} closure
63 * @throws NullPointerException if any argument is null
64 */
65 public static <E> Closure<E> ifClosure(final Predicate<? super E> predicate,
66 final Closure<? super E> trueClosure,
67 final Closure<? super E> falseClosure) {
68 return new IfClosure<>(Objects.requireNonNull(predicate, "predicate"),
69 Objects.requireNonNull(trueClosure, "trueClosure"),
70 Objects.requireNonNull(falseClosure, "falseClosure"));
71 }
72
73 /** The test */
74 private final Predicate<? super T> iPredicate;
75
76 /** The closure to use if true */
77 private final Closure<? super T> iTrueClosure;
78
79 /** The closure to use if false */
80 private final Closure<? super T> iFalseClosure;
81
82 /**
83 * Constructor that performs no validation.
84 * Use {@code ifClosure} if you want that.
85 * <p>
86 * This constructor creates a closure that performs no action when
87 * the predicate is false.
88 * </p>
89 *
90 * @param predicate predicate to switch on, not null
91 * @param trueClosure closure used if true, not null
92 * @since 3.2
93 */
94 public IfClosure(final Predicate<? super T> predicate, final Closure<? super T> trueClosure) {
95 this(predicate, trueClosure, NOPClosure.nopClosure());
96 }
97
98 /**
99 * Constructor that performs no validation.
100 * Use {@code ifClosure} if you want that.
101 *
102 * @param predicate predicate to switch on, not null
103 * @param trueClosure closure used if true, not null
104 * @param falseClosure closure used if false, not null
105 */
106 public IfClosure(final Predicate<? super T> predicate, final Closure<? super T> trueClosure,
107 final Closure<? super T> falseClosure) {
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 @Override
119 public void execute(final T input) {
120 if (iPredicate.test(input)) {
121 iTrueClosure.accept(input);
122 } else {
123 iFalseClosure.accept(input);
124 }
125 }
126
127 /**
128 * Gets the closure called when false.
129 *
130 * @return The closure
131 * @since 3.1
132 */
133 public Closure<? super T> getFalseClosure() {
134 return iFalseClosure;
135 }
136
137 /**
138 * Gets the predicate.
139 *
140 * @return The predicate
141 * @since 3.1
142 */
143 public Predicate<? super T> getPredicate() {
144 return iPredicate;
145 }
146
147 /**
148 * Gets the closure called when true.
149 *
150 * @return The closure
151 * @since 3.1
152 */
153 public Closure<? super T> getTrueClosure() {
154 return iTrueClosure;
155 }
156
157 }