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