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