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