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.collections.functors;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.collections.Predicate;
22  import org.apache.commons.collections.Transformer;
23  
24  /**
25   * Predicate implementation that transforms the given object before invoking
26   * another <code>Predicate</code>.
27   *
28   * @since 3.1
29   * @version $Id: TransformedPredicate.java 1435965 2013-01-20 21:13:18Z tn $
30   */
31  public final class TransformedPredicate<T> implements Predicate<T>, PredicateDecorator<T>, Serializable {
32  
33      /** Serial version UID */
34      private static final long serialVersionUID = -5596090919668315834L;
35  
36      /** The transformer to call */
37      private final Transformer<? super T, ? extends T> iTransformer;
38  
39      /** The predicate to call */
40      private final Predicate<? super T> iPredicate;
41  
42      /**
43       * Factory to create the predicate.
44       *
45       * @param <T> the type that the predicate queries
46       * @param transformer  the transformer to call
47       * @param predicate  the predicate to call with the result of the transform
48       * @return the predicate
49       * @throws IllegalArgumentException if the transformer or the predicate is null
50       */
51      public static <T> Predicate<T> transformedPredicate(final Transformer<? super T, ? extends T> transformer,
52                                                          final Predicate<? super T> predicate) {
53          if (transformer == null) {
54              throw new IllegalArgumentException("The transformer to call must not be null");
55          }
56          if (predicate == null) {
57              throw new IllegalArgumentException("The predicate to call must not be null");
58          }
59          return new TransformedPredicate<T>(transformer, predicate);
60      }
61  
62      /**
63       * Constructor that performs no validation.
64       * Use <code>getInstance</code> if you want that.
65       *
66       * @param transformer  the transformer to use
67       * @param predicate  the predicate to decorate
68       */
69      public TransformedPredicate(final Transformer<? super T, ? extends T> transformer,
70                                  final Predicate<? super T> predicate) {
71          iTransformer = transformer;
72          iPredicate = predicate;
73      }
74  
75      /**
76       * Evaluates the predicate returning the result of the decorated predicate
77       * once the input has been transformed
78       *
79       * @param object  the input object which will be transformed
80       * @return true if decorated predicate returns true
81       */
82      public boolean evaluate(final T object) {
83          final T result = iTransformer.transform(object);
84          return iPredicate.evaluate(result);
85      }
86  
87      /**
88       * Gets the predicate being decorated.
89       *
90       * @return the predicate as the only element in an array
91       * @since 3.1
92       */
93      @SuppressWarnings("unchecked")
94      public Predicate<? super T>[] getPredicates() {
95          return new Predicate[] {iPredicate};
96      }
97  
98      /**
99       * Gets the transformer in use.
100      *
101      * @return the transformer
102      */
103     public Transformer<? super T, ? extends T> getTransformer() {
104         return iTransformer;
105     }
106 
107 }