    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.Predicate;
    023
    024/**
    025 * Predicate implementation that returns true if the input is null.
    026 *
    027 * @since 3.0
    028 */
    029public final class NullIsTruePredicate<T> implements PredicateDecorator<T>, Serializable {
    030
    031    /** Serial version UID */
    032    private static final long serialVersionUID = -7625133768987126273L;
    033
    034    /**
    035     * Factory to create the null true predicate.
    036     *
    037     * @param <T> the type that the predicate queries
    038     * @param predicate  the predicate to decorate, not null
    039     * @return the predicate
    040     * @throws NullPointerException if the predicate is null
    041     */
    042    public static <T> Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate) {
    043        return new NullIsTruePredicate<>(Objects.requireNonNull(predicate, "predicate"));
    044    }
    045
    046    /** The predicate to decorate */
    047    private final Predicate<? super T> iPredicate;
    048
    049    /**
    050     * Constructor that performs no validation.
    051     * Use {@code nullIsTruePredicate} if you want that.
    052     *
    053     * @param predicate  the predicate to call after the null check
    054     */
    055    public NullIsTruePredicate(final Predicate<? super T> predicate) {
    056        iPredicate = predicate;
    057    }
    058
    059    /**
    060     * Evaluates the predicate returning the result of the decorated predicate
    061     * once a null check is performed.
    062     *
    063     * @param object  the input object
    064     * @return true if decorated predicate returns true or input is null
    065     */
    066    @Override
    067    public boolean evaluate(final T object) {
    068        if (object == null) {
    069            return true;
    070        }
    071        return iPredicate.evaluate(object);
    072    }
    073
    074    /**
    075     * Gets the predicate being decorated.
    076     *
    077     * @return the predicate as the only element in an array
    078     * @since 3.1
    079     */
    080    @Override
    081    @SuppressWarnings("unchecked")
    082    public Predicate<? super T>[] getPredicates() {
    083        return new Predicate[] { iPredicate };
    084    }
    085
    086}