    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 *      https://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 * Transformer implementation that calls a Predicate using the input object
    026 * and then returns the result.
    027 *
    028 * @param <T> The type of the input and result to the function.
    029 * @since 3.0
    030 */
    031public class PredicateTransformer<T> implements Transformer<T, Boolean>, Serializable {
    032
    033    /** Serial version UID */
    034    private static final long serialVersionUID = 5278818408044349346L;
    035
    036    /**
    037     * Factory method that performs validation.
    038     *
    039     * @param <T>  the input type
    040     * @param predicate  The predicate to call, not null
    041     * @return The {@code predicate} transformer
    042     * @throws IllegalArgumentException if the predicate is null
    043     */
    044    public static <T> Transformer<T, Boolean> predicateTransformer(final Predicate<? super T> predicate) {
    045        if (predicate == null) {
    046            throw new IllegalArgumentException("Predicate must not be null");
    047        }
    048        return new PredicateTransformer<>(predicate);
    049    }
    050
    051    /** The closure to wrap */
    052    private final Predicate<? super T> iPredicate;
    053
    054    /**
    055     * Constructor that performs no validation.
    056     * Use {@code predicateTransformer} if you want that.
    057     *
    058     * @param predicate  The predicate to call, not null
    059     */
    060    public PredicateTransformer(final Predicate<? super T> predicate) {
    061        iPredicate = predicate;
    062    }
    063
    064    /**
    065     * Gets the predicate.
    066     *
    067     * @return The predicate
    068     * @since 3.1
    069     */
    070    public Predicate<? super T> getPredicate() {
    071        return iPredicate;
    072    }
    073
    074    /**
    075     * Transforms the input to result by calling a predicate.
    076     *
    077     * @param input  The input object to transform
    078     * @return The transformed result
    079     */
    080    @Override
    081    public Boolean transform(final T input) {
    082        return Boolean.valueOf(iPredicate.test(input));
    083    }
    084
    085}