KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > functors > NullIsFalsePredicate


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
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: 1.6 $ $Date: 2004/05/31 16:43:17 $
27  *
28  * @author Stephen Colebourne
29  */

30 public final class NullIsFalsePredicate implements Predicate, PredicateDecorator, Serializable JavaDoc {
31
32     /** Serial version UID */
33     static final long serialVersionUID = -2997501534564735525L;
34     
35     /** The predicate to decorate */
36     private final Predicate iPredicate;
37     
38     /**
39      * Factory to create the null false predicate.
40      *
41      * @param predicate the predicate to decorate, not null
42      * @return the predicate
43      * @throws IllegalArgumentException if the predicate is null
44      */

45     public static Predicate getInstance(Predicate predicate) {
46         if (predicate == null) {
47             throw new IllegalArgumentException JavaDoc("Predicate must not be null");
48         }
49         return new NullIsFalsePredicate(predicate);
50     }
51
52     /**
53      * Constructor that performs no validation.
54      * Use <code>getInstance</code> if you want that.
55      *
56      * @param predicate the predicate to call after the null check
57      */

58     public NullIsFalsePredicate(Predicate predicate) {
59         super();
60         iPredicate = predicate;
61     }
62
63     /**
64      * Evaluates the predicate returning the result of the decorated predicate
65      * once a null check is performed.
66      *
67      * @param object the input object
68      * @return true if decorated predicate returns true, false if input is null
69      */

70     public boolean evaluate(Object JavaDoc object) {
71         if (object == null) {
72             return false;
73         }
74         return iPredicate.evaluate(object);
75     }
76
77     /**
78      * Gets the predicate being decorated.
79      *
80      * @return the predicate as the only element in an array
81      * @since Commons Collections 3.1
82      */

83     public Predicate[] getPredicates() {
84         return new Predicate[] {iPredicate};
85     }
86
87 }
88
Popular Tags