1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.collections4.functors;
18
19 import java.io.Serializable;
20 import java.util.Objects;
21
22 import org.apache.commons.collections4.FunctorException;
23 import org.apache.commons.collections4.Predicate;
24
25 /**
26 * Predicate implementation that throws an exception if the input is null.
27 *
28 * @param <T> the type of the input to the predicate.
29 * @since 3.0
30 */
31 public final class NullIsExceptionPredicate<T> extends AbstractPredicate<T> implements PredicateDecorator<T>, Serializable {
32
33 /** Serial version UID */
34 private static final long serialVersionUID = 3243449850504576071L;
35
36 /**
37 * Creates the null exception predicate.
38 *
39 * @param <T> the type that the predicate queries
40 * @param predicate the predicate to decorate, not null
41 * @return the predicate
42 * @throws NullPointerException if the predicate is null
43 */
44 public static <T> Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate) {
45 return new NullIsExceptionPredicate<>(Objects.requireNonNull(predicate, "predicate"));
46 }
47
48 /** The predicate to decorate */
49 private final Predicate<? super T> iPredicate;
50
51 /**
52 * Constructor that performs no validation.
53 * Use {@code nullIsExceptionPredicate} if you want that.
54 *
55 * @param predicate the predicate to call after the null check
56 */
57 public NullIsExceptionPredicate(final Predicate<? super T> predicate) {
58 iPredicate = predicate;
59 }
60
61 /**
62 * Gets the predicate being decorated.
63 *
64 * @return the predicate as the only element in an array
65 * @since 3.1
66 */
67 @Override
68 @SuppressWarnings("unchecked")
69 public Predicate<? super T>[] getPredicates() {
70 return new Predicate[] { iPredicate };
71 }
72
73 /**
74 * Evaluates the predicate returning the result of the decorated predicate
75 * once a null check is performed.
76 *
77 * @param object the input object
78 * @return true if decorated predicate returns true
79 * @throws FunctorException if input is null
80 */
81 @Override
82 public boolean test(final T object) {
83 if (object == null) {
84 throw new FunctorException("Input Object must not be null");
85 }
86 return iPredicate.test(object);
87 }
88
89 }