View Javadoc
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.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      /**
58       * Factory method that performs validation.
59       * <p>
60       * This factory creates a transformer that just returns the input object when
61       * the predicate is false.
62       * </p>
63       *
64       * @param <T>  input and output type for the transformer
65       * @param predicate  predicate to switch on
66       * @param trueTransformer  transformer used if true
67       * @return The {@code if} transformer
68       * @throws NullPointerException if either argument is null
69       */
70      public static <T> Transformer<T, T> ifTransformer(
71              final Predicate<? super T> predicate,
72              final Transformer<? super T, ? extends T> trueTransformer) {
73          return new IfTransformer<>(Objects.requireNonNull(predicate, "predicate"),
74                  Objects.requireNonNull(trueTransformer, "trueTransformer"), NOPTransformer.<T>nopTransformer());
75      }
76  
77      /** The test */
78      private final Predicate<? super T> iPredicate;
79  
80      /** The transformer to use if true */
81      private final Transformer<? super T, ? extends R> iTrueTransformer;
82  
83      /** The transformer to use if false */
84      private final Transformer<? super T, ? extends R> iFalseTransformer;
85  
86      /**
87       * Constructor that performs no validation.
88       * Use the static factory method {@code ifTransformer} if you want that.
89       *
90       * @param predicate  predicate to switch on, not null
91       * @param trueTransformer  transformer used if true, not null
92       * @param falseTransformer  transformer used if false, not null
93       */
94      public IfTransformer(final Predicate<? super T> predicate,
95          final Transformer<? super T, ? extends R> trueTransformer,
96          final Transformer<? super T, ? extends R> falseTransformer) {
97  
98          iPredicate = predicate;
99          iTrueTransformer = trueTransformer;
100         iFalseTransformer = falseTransformer;
101     }
102 
103     /**
104      * Gets the transformer used when false.
105      *
106      * @return The transformer
107      */
108     public Transformer<? super T, ? extends R> getFalseTransformer() {
109         return iFalseTransformer;
110     }
111 
112     /**
113      * Gets the predicate.
114      *
115      * @return The predicate
116      */
117     public Predicate<? super T> getPredicate() {
118         return iPredicate;
119     }
120 
121     /**
122      * Gets the transformer used when true.
123      *
124      * @return The transformer
125      */
126     public Transformer<? super T, ? extends R> getTrueTransformer() {
127         return iTrueTransformer;
128     }
129 
130     /**
131      * Transforms the input using the true or false transformer based to the result of the predicate.
132      *
133      * @param input  The input object to transform
134      * @return The transformed result
135      */
136     @Override
137     public R transform(final T input) {
138         if (iPredicate.test(input)) {
139             return iTrueTransformer.apply(input);
140         }
141         return iFalseTransformer.apply(input);
142     }
143 }