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