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