001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.functors;
018
019import java.io.Serializable;
020
021import org.apache.commons.collections4.Predicate;
022import org.apache.commons.collections4.Transformer;
023
024/**
025 * Predicate implementation that transforms the given object before invoking
026 * another <code>Predicate</code>.
027 *
028 * @since 3.1
029 * @version $Id: TransformedPredicate.html 972421 2015-11-14 20:00:04Z tn $
030 */
031public final class TransformedPredicate<T> implements PredicateDecorator<T>, Serializable {
032
033    /** Serial version UID */
034    private static final long serialVersionUID = -5596090919668315834L;
035
036    /** The transformer to call */
037    private final Transformer<? super T, ? extends T> iTransformer;
038
039    /** The predicate to call */
040    private final Predicate<? super T> iPredicate;
041
042    /**
043     * Factory to create the predicate.
044     *
045     * @param <T> the type that the predicate queries
046     * @param transformer  the transformer to call
047     * @param predicate  the predicate to call with the result of the transform
048     * @return the predicate
049     * @throws IllegalArgumentException if the transformer or the predicate is null
050     */
051    public static <T> Predicate<T> transformedPredicate(final Transformer<? super T, ? extends T> transformer,
052                                                        final Predicate<? super T> predicate) {
053        if (transformer == null) {
054            throw new IllegalArgumentException("The transformer to call must not be null");
055        }
056        if (predicate == null) {
057            throw new IllegalArgumentException("The predicate to call must not be null");
058        }
059        return new TransformedPredicate<T>(transformer, predicate);
060    }
061
062    /**
063     * Constructor that performs no validation.
064     * Use <code>transformedPredicate</code> if you want that.
065     *
066     * @param transformer  the transformer to use
067     * @param predicate  the predicate to decorate
068     */
069    public TransformedPredicate(final Transformer<? super T, ? extends T> transformer,
070                                final Predicate<? super T> predicate) {
071        iTransformer = transformer;
072        iPredicate = predicate;
073    }
074
075    /**
076     * Evaluates the predicate returning the result of the decorated predicate
077     * once the input has been transformed
078     *
079     * @param object  the input object which will be transformed
080     * @return true if decorated predicate returns true
081     */
082    public boolean evaluate(final T object) {
083        final T result = iTransformer.transform(object);
084        return iPredicate.evaluate(result);
085    }
086
087    /**
088     * Gets the predicate being decorated.
089     *
090     * @return the predicate as the only element in an array
091     * @since 3.1
092     */
093    @SuppressWarnings("unchecked")
094    public Predicate<? super T>[] getPredicates() {
095        return new Predicate[] {iPredicate};
096    }
097
098    /**
099     * 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}