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;
020import java.util.Objects;
021
022import org.apache.commons.collections4.FunctorException;
023import org.apache.commons.collections4.Predicate;
024import org.apache.commons.collections4.Transformer;
025
026/**
027 * Predicate implementation that returns the result of a transformer.
028 *
029 * @since 3.0
030 */
031public final class TransformerPredicate<T> implements Predicate<T>, Serializable {
032
033    /** Serial version UID */
034    private static final long serialVersionUID = -2407966402920578741L;
035
036    /**
037     * Factory to create the predicate.
038     *
039     * @param <T> the type that the predicate queries
040     * @param transformer  the transformer to decorate
041     * @return the predicate
042     * @throws NullPointerException if the transformer is null
043     */
044    public static <T> Predicate<T> transformerPredicate(final Transformer<? super T, Boolean> transformer) {
045        return new TransformerPredicate<>(Objects.requireNonNull(transformer, "transformer"));
046    }
047
048    /** The transformer to call */
049    private final Transformer<? super T, Boolean> iTransformer;
050
051    /**
052     * Constructor that performs no validation.
053     * Use {@code transformerPredicate} if you want that.
054     *
055     * @param transformer  the transformer to decorate
056     */
057    public TransformerPredicate(final Transformer<? super T, Boolean> transformer) {
058        iTransformer = transformer;
059    }
060
061    /**
062     * Evaluates the predicate returning the result of the decorated transformer.
063     *
064     * @param object  the input object
065     * @return true if decorated transformer returns Boolean.TRUE
066     * @throws FunctorException if the transformer returns an invalid type
067     */
068    @Override
069    public boolean evaluate(final T object) {
070        final Boolean result = iTransformer.transform(object);
071        if (result == null) {
072            throw new FunctorException(
073                    "Transformer must return an instanceof Boolean, it was a null object");
074        }
075        return result.booleanValue();
076    }
077
078    /**
079     * Gets the transformer.
080     *
081     * @return the transformer
082     * @since 3.1
083     */
084    public Transformer<? super T, Boolean> getTransformer() {
085        return iTransformer;
086    }
087
088}