KickJava   Java API By Example, From Geeks To Geeks.

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


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 the opposite of the decorated predicate.
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 NotPredicate implements Predicate, PredicateDecorator, Serializable JavaDoc {
31
32     /** Serial version UID */
33     static final long serialVersionUID = -2654603322338049674L;
34     
35     /** The predicate to decorate */
36     private final Predicate iPredicate;
37     
38     /**
39      * Factory to create the not 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 NotPredicate(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 NotPredicate(Predicate predicate) {
59         super();
60         iPredicate = predicate;
61     }
62
63     /**
64      * Evaluates the predicate returning the opposite to the stored predicate.
65      *
66      * @param object the input object
67      * @return true if predicate returns false
68      */

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

79     public Predicate[] getPredicates() {
80         return new Predicate[] {iPredicate};
81     }
82
83 }
84
Popular Tags