01: /*
02: * Copyright 2001-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.commons.collections.functors;
17:
18: import java.io.Serializable;
19:
20: import org.apache.commons.collections.FunctorException;
21: import org.apache.commons.collections.Predicate;
22:
23: /**
24: * Predicate implementation that throws an exception if the input is null.
25: *
26: * @since Commons Collections 3.0
27: * @version $Revision: 348444 $ $Date: 2005-11-23 14:06:56 +0000 (Wed, 23 Nov 2005) $
28: *
29: * @author Stephen Colebourne
30: */
31: public final class NullIsExceptionPredicate implements Predicate,
32: PredicateDecorator, Serializable {
33:
34: /** Serial version UID */
35: private static final long serialVersionUID = 3243449850504576071L;
36:
37: /** The predicate to decorate */
38: private final Predicate iPredicate;
39:
40: /**
41: * Factory to create the null exception predicate.
42: *
43: * @param predicate the predicate to decorate, not null
44: * @return the predicate
45: * @throws IllegalArgumentException if the predicate is null
46: */
47: public static Predicate getInstance(Predicate predicate) {
48: if (predicate == null) {
49: throw new IllegalArgumentException(
50: "Predicate must not be null");
51: }
52: return new NullIsExceptionPredicate(predicate);
53: }
54:
55: /**
56: * Constructor that performs no validation.
57: * Use <code>getInstance</code> if you want that.
58: *
59: * @param predicate the predicate to call after the null check
60: */
61: public NullIsExceptionPredicate(Predicate predicate) {
62: super ();
63: iPredicate = predicate;
64: }
65:
66: /**
67: * Evaluates the predicate returning the result of the decorated predicate
68: * once a null check is performed.
69: *
70: * @param object the input object
71: * @return true if decorated predicate returns true
72: * @throws FunctorException if input is null
73: */
74: public boolean evaluate(Object object) {
75: if (object == null) {
76: throw new FunctorException("Input Object must not be null");
77: }
78: return iPredicate.evaluate(object);
79: }
80:
81: /**
82: * Gets the predicate being decorated.
83: *
84: * @return the predicate as the only element in an array
85: * @since Commons Collections 3.1
86: */
87: public Predicate[] getPredicates() {
88: return new Predicate[] { iPredicate };
89: }
90:
91: }
|