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.LinkedHashMap;
21  import java.util.Map;
22  import java.util.Objects;
23  
24  import org.apache.commons.collections4.Predicate;
25  import org.apache.commons.collections4.Transformer;
26  
27  /**
28   * Transformer implementation calls the transformer whose predicate returns true,
29   * like a switch statement.
30   *
31   * @param <T> The type of the input to the function.
32   * @param <R> The type of the result of the function.
33   * @since 3.0
34   */
35  public class SwitchTransformer<T, R> implements Transformer<T, R>, Serializable {
36  
37      /** Serial version UID */
38      private static final long serialVersionUID = -6404460890903469332L;
39  
40      /**
41       * Create a new Transformer that calls one of the transformers depending
42       * on the predicates.
43       * <p>
44       * The Map consists of Predicate keys and Transformer values. A transformer
45       * is called if its matching predicate returns true. Each predicate is evaluated
46       * until one returns true. If no predicates evaluate to true, the default
47       * transformer is called. The default transformer is set in the map with a
48       * null key. The ordering is that of the iterator() method on the entryset
49       * collection of the map.
50       * </p>
51       *
52       * @param <I>  the input type
53       * @param <O>  the output type
54       * @param map  A map of predicates to transformers
55       * @return The {@code switch} transformer
56       * @throws NullPointerException if the map is null
57       * @throws NullPointerException if any transformer in the map is null
58       * @throws ClassCastException  if the map elements are of the wrong type
59       */
60      @SuppressWarnings("unchecked")
61      public static <I, O> Transformer<I, O> switchTransformer(
62              final Map<? extends Predicate<? super I>, ? extends Transformer<? super I, ? extends O>> map) {
63  
64          Objects.requireNonNull(map, "map");
65          if (map.isEmpty()) {
66              return ConstantTransformer.<I, O>nullTransformer();
67          }
68          // copy so the caller's map is not mutated; LinkedHashMap preserves iterator() ordering
69          final Map<Predicate<? super I>, Transformer<? super I, ? extends O>> entries = new LinkedHashMap<>(map);
70          final Transformer<? super I, ? extends O> defaultTransformer = entries.remove(null);
71          final int size = entries.size();
72          if (size == 0) {
73              return (Transformer<I, O>) (defaultTransformer == null ? ConstantTransformer.<I, O>nullTransformer() :
74                                                                       defaultTransformer);
75          }
76          final Transformer<? super I, ? extends O>[] transformers = new Transformer[size];
77          final Predicate<? super I>[] preds = new Predicate[size];
78          int i = 0;
79          for (final Map.Entry<Predicate<? super I>,
80                               Transformer<? super I, ? extends O>> entry : entries.entrySet()) {
81              preds[i] = entry.getKey();
82              transformers[i] = entry.getValue();
83              i++;
84          }
85          return new SwitchTransformer<>(false, preds, transformers, defaultTransformer);
86      }
87  
88      /**
89       * Factory method that performs validation and copies the parameter arrays.
90       *
91       * @param <I>  the input type
92       * @param <O>  the output type
93       * @param predicates  array of predicates, cloned, no nulls
94       * @param transformers  matching array of transformers, cloned, no nulls
95       * @param defaultTransformer  The transformer to use if no match, null means return null
96       * @return The {@code chained} transformer
97       * @throws NullPointerException if either array is null
98       * @throws NullPointerException if any element in the arrays is null
99       * @throws IllegalArgumentException if the arrays have different sizes
100      */
101     @SuppressWarnings("unchecked")
102     public static <I, O> Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
103             final Transformer<? super I, ? extends O>[] transformers,
104             final Transformer<? super I, ? extends O> defaultTransformer) {
105         FunctorUtils.validate(predicates);
106         FunctorUtils.validate(transformers);
107         if (predicates.length != transformers.length) {
108             throw new IllegalArgumentException("The predicate and transformer arrays must be the same size");
109         }
110         if (predicates.length == 0) {
111             return (Transformer<I, O>) (defaultTransformer == null ? ConstantTransformer.<I, O>nullTransformer() :
112                                                                      defaultTransformer);
113         }
114         return new SwitchTransformer<>(predicates, transformers, defaultTransformer);
115     }
116 
117     /** The tests to consider */
118     private final Predicate<? super T>[] iPredicates;
119 
120     /** The matching transformers to call */
121     private final Transformer<? super T, ? extends R>[] iTransformers;
122 
123     /** The default transformer to call if no tests match */
124     private final Transformer<? super T, ? extends R> iDefault;
125 
126     /**
127      * Hidden constructor for the use by the static factory methods.
128      *
129      * @param clone  if {@code true} the input arguments will be cloned
130      * @param predicates  array of predicates, no nulls
131      * @param transformers  matching array of transformers, no nulls
132      * @param defaultTransformer  The transformer to use if no match, null means return null
133      */
134     private SwitchTransformer(final boolean clone, final Predicate<? super T>[] predicates,
135                              final Transformer<? super T, ? extends R>[] transformers,
136                              final Transformer<? super T, ? extends R> defaultTransformer) {
137         iPredicates = clone ? FunctorUtils.copy(predicates) : predicates;
138         iTransformers = clone ? FunctorUtils.copy(transformers) : transformers;
139         iDefault = defaultTransformer == null ?
140                 ConstantTransformer.<T, R>nullTransformer() : defaultTransformer;
141     }
142 
143     /**
144      * Constructor that performs no validation.
145      * Use {@code switchTransformer} if you want that.
146      *
147      * @param predicates  array of predicates, cloned, no nulls
148      * @param transformers  matching array of transformers, cloned, no nulls
149      * @param defaultTransformer  The transformer to use if no match, null means return null
150      */
151     public SwitchTransformer(final Predicate<? super T>[] predicates,
152             final Transformer<? super T, ? extends R>[] transformers,
153             final Transformer<? super T, ? extends R> defaultTransformer) {
154         this(true, predicates, transformers, defaultTransformer);
155     }
156 
157     /**
158      * Gets the default transformer.
159      *
160      * @return The default transformer
161      * @since 3.1
162      */
163     public Transformer<? super T, ? extends R> getDefaultTransformer() {
164         return iDefault;
165     }
166 
167     /**
168      * Gets the predicates.
169      *
170      * @return A copy of the predicates
171      * @since 3.1
172      */
173     public Predicate<? super T>[] getPredicates() {
174         return FunctorUtils.copy(iPredicates);
175     }
176 
177     /**
178      * Gets the transformers.
179      *
180      * @return A copy of the transformers
181      * @since 3.1
182      */
183     public Transformer<? super T, ? extends R>[] getTransformers() {
184         return FunctorUtils.copy(iTransformers);
185     }
186 
187     /**
188      * Transforms the input to result by calling the transformer whose matching
189      * predicate returns true.
190      *
191      * @param input  The input object to transform
192      * @return The transformed result
193      */
194     @Override
195     public R transform(final T input) {
196         for (int i = 0; i < iPredicates.length; i++) {
197             if (iPredicates[i].test(input)) {
198                 return iTransformers[i].apply(input);
199             }
200         }
201         return iDefault.apply(input);
202     }
203 
204 }