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