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