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.Predicate;
21:
22: /**
23: * Predicate implementation that returns false if the input is null.
24: *
25: * @since Commons Collections 3.0
26: * @version $Revision: 348444 $ $Date: 2005-11-23 14:06:56 +0000 (Wed, 23 Nov 2005) $
27: *
28: * @author Stephen Colebourne
29: */
30: public final class NullIsFalsePredicate implements Predicate,
31: PredicateDecorator, Serializable {
32:
33: /** Serial version UID */
34: private static final long serialVersionUID = -2997501534564735525L;
35:
36: /** The predicate to decorate */
37: private final Predicate iPredicate;
38:
39: /**
40: * Factory to create the null false predicate.
41: *
42: * @param predicate the predicate to decorate, not null
43: * @return the predicate
44: * @throws IllegalArgumentException if the predicate is null
45: */
46: public static Predicate getInstance(Predicate predicate) {
47: if (predicate == null) {
48: throw new IllegalArgumentException(
49: "Predicate must not be null");
50: }
51: return new NullIsFalsePredicate(predicate);
52: }
53:
54: /**
55: * Constructor that performs no validation.
56: * Use <code>getInstance</code> if you want that.
57: *
58: * @param predicate the predicate to call after the null check
59: */
60: public NullIsFalsePredicate(Predicate predicate) {
61: super ();
62: iPredicate = predicate;
63: }
64:
65: /**
66: * Evaluates the predicate returning the result of the decorated predicate
67: * once a null check is performed.
68: *
69: * @param object the input object
70: * @return true if decorated predicate returns true, false if input is null
71: */
72: public boolean evaluate(Object object) {
73: if (object == null) {
74: return false;
75: }
76: return iPredicate.evaluate(object);
77: }
78:
79: /**
80: * Gets the predicate being decorated.
81: *
82: * @return the predicate as the only element in an array
83: * @since Commons Collections 3.1
84: */
85: public Predicate[] getPredicates() {
86: return new Predicate[] { iPredicate };
87: }
88:
89: }
|