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.collections4.functors;
18
19 import java.io.Serializable;
20 import java.util.Objects;
21
22 import org.apache.commons.collections4.Predicate;
23 import org.apache.commons.collections4.Transformer;
24
25 /**
26 * Transformer implementation that will call one of two closures based on whether a predicate evaluates
27 * as true or false.
28 *
29 * @param <T> the type of the input to the function.
30 * @param <R> the type of the result of the function.
31 * @since 4.1
32 */
33 public class IfTransformer<T, R> implements Transformer<T, R>, Serializable {
34
35 /** Serial version UID */
36 private static final long serialVersionUID = 8069309411242014252L;
37
38 /**
39 * Factory method that performs validation.
40 *
41 * @param <I> input type for the transformer
42 * @param <O> output type for the transformer
43 * @param predicate predicate to switch on
44 * @param trueTransformer transformer used if true
45 * @param falseTransformer transformer used if false
46 * @return the {@code if} transformer
47 * @throws NullPointerException if either argument is null
48 */
49 public static <I, O> Transformer<I, O> ifTransformer(final Predicate<? super I> predicate,
50 final Transformer<? super I, ? extends O> trueTransformer,
51 final Transformer<? super I, ? extends O> falseTransformer) {
52 return new IfTransformer<>(Objects.requireNonNull(predicate, "predicate"),
53 Objects.requireNonNull(trueTransformer, "trueTransformer"),
54 Objects.requireNonNull(falseTransformer, "falseTransformer"));
55 }
56 /**
57 * Factory method that performs validation.
58 * <p>
59 * This factory creates a transformer that just returns the input object when
60 * the predicate is false.
61 *
62 * @param <T> input and output type for the transformer
63 * @param predicate predicate to switch on
64 * @param trueTransformer transformer used if true
65 * @return the {@code if} transformer
66 * @throws NullPointerException if either argument is null
67 */
68 public static <T> Transformer<T, T> ifTransformer(
69 final Predicate<? super T> predicate,
70 final Transformer<? super T, ? extends T> trueTransformer) {
71 return new IfTransformer<>(Objects.requireNonNull(predicate, "predicate"),
72 Objects.requireNonNull(trueTransformer, "trueTransformer"), NOPTransformer.<T>nopTransformer());
73 }
74 /** The test */
75 private final Predicate<? super T> iPredicate;
76
77 /** The transformer to use if true */
78 private final Transformer<? super T, ? extends R> iTrueTransformer;
79
80 /** The transformer to use if false */
81 private final Transformer<? super T, ? extends R> iFalseTransformer;
82
83 /**
84 * Constructor that performs no validation.
85 * Use the static factory method {@code ifTransformer} if you want that.
86 *
87 * @param predicate predicate to switch on, not null
88 * @param trueTransformer transformer used if true, not null
89 * @param falseTransformer transformer used if false, not null
90 */
91 public IfTransformer(final Predicate<? super T> predicate,
92 final Transformer<? super T, ? extends R> trueTransformer,
93 final Transformer<? super T, ? extends R> falseTransformer) {
94
95 iPredicate = predicate;
96 iTrueTransformer = trueTransformer;
97 iFalseTransformer = falseTransformer;
98 }
99
100 /**
101 * Gets the transformer used when false.
102 *
103 * @return the transformer
104 */
105 public Transformer<? super T, ? extends R> getFalseTransformer() {
106 return iFalseTransformer;
107 }
108
109 /**
110 * Gets the predicate.
111 *
112 * @return the predicate
113 */
114 public Predicate<? super T> getPredicate() {
115 return iPredicate;
116 }
117
118 /**
119 * Gets the transformer used when true.
120 *
121 * @return the transformer
122 */
123 public Transformer<? super T, ? extends R> getTrueTransformer() {
124 return iTrueTransformer;
125 }
126
127 /**
128 * Transforms the input using the true or false transformer based to the result of the predicate.
129 *
130 * @param input the input object to transform
131 * @return the transformed result
132 */
133 @Override
134 public R transform(final T input) {
135 if (iPredicate.test(input)) {
136 return iTrueTransformer.apply(input);
137 }
138 return iFalseTransformer.apply(input);
139 }
140 }