    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;
    024
    025/**
    026 * Predicate implementation that throws an exception if the input is null.
    027 *
    028 * @since 3.0
    029 */
    030public final class NullIsExceptionPredicate<T> implements PredicateDecorator<T>, Serializable {
    031
    032    /** Serial version UID */
    033    private static final long serialVersionUID = 3243449850504576071L;
    034
    035    /**
    036     * Factory to create the null exception 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> nullIsExceptionPredicate(final Predicate<? super T> predicate) {
    044        return new NullIsExceptionPredicate<>(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 nullIsExceptionPredicate} if you want that.
    053     *
    054     * @param predicate  the predicate to call after the null check
    055     */
    056    public NullIsExceptionPredicate(final Predicate<? super T> predicate) {
    057        iPredicate = predicate;
    058    }
    059
    060    /**
    061     * Evaluates the predicate returning the result of the decorated predicate
    062     * once a null check is performed.
    063     *
    064     * @param object  the input object
    065     * @return true if decorated predicate returns true
    066     * @throws FunctorException if input is null
    067     */
    068    @Override
    069    public boolean evaluate(final T object) {
    070        if (object == null) {
    071            throw new FunctorException("Input Object must not be null");
    072        }
    073        return iPredicate.evaluate(object);
    074    }
    075
    076    /**
    077     * Gets the predicate being decorated.
    078     *
    079     * @return the predicate as the only element in an array
    080     * @since 3.1
    081     */
    082    @Override
    083    @SuppressWarnings("unchecked")
    084    public Predicate<? super T>[] getPredicates() {
    085        return new Predicate[] { iPredicate };
    086    }
    087
    088}