KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Collection JavaDoc;
20
21 import org.apache.commons.collections.Predicate;
22
23 /**
24  * Predicate implementation that returns true if none of the predicates return true.
25  *
26  * @since Commons Collections 3.0
27  * @version $Revision: 1.6 $ $Date: 2004/05/31 16:43:17 $
28  *
29  * @author Stephen Colebourne
30  */

31 public final class NonePredicate implements Predicate, PredicateDecorator, Serializable JavaDoc {
32
33     /** Serial version UID */
34     static final long serialVersionUID = 2007613066565892961L;
35     
36     /** The array of predicates to call */
37     private final Predicate[] iPredicates;
38     
39     /**
40      * Factory to create the predicate.
41      *
42      * @param predicates the predicates to check, cloned, not null
43      * @return the <code>any</code> predicate
44      * @throws IllegalArgumentException if the predicates array is null
45      * @throws IllegalArgumentException if the predicates array has less than 2 elements
46      * @throws IllegalArgumentException if any predicate in the array is null
47      */

48     public static Predicate getInstance(Predicate[] predicates) {
49         FunctorUtils.validateMin2(predicates);
50         predicates = FunctorUtils.copy(predicates);
51         return new NonePredicate(predicates);
52     }
53
54     /**
55      * Factory to create the predicate.
56      *
57      * @param predicates the predicates to check, cloned, not null
58      * @return the <code>one</code> predicate
59      * @throws IllegalArgumentException if the predicates array is null
60      * @throws IllegalArgumentException if any predicate in the array is null
61      * @throws IllegalArgumentException if the predicates array has less than 2 elements
62      */

63     public static Predicate getInstance(Collection JavaDoc predicates) {
64         Predicate[] preds = FunctorUtils.validate(predicates);
65         return new NonePredicate(preds);
66     }
67
68     /**
69      * Constructor that performs no validation.
70      * Use <code>getInstance</code> if you want that.
71      *
72      * @param predicates the predicates to check, not cloned, not null
73      */

74     public NonePredicate(Predicate[] predicates) {
75         super();
76         iPredicates = predicates;
77     }
78
79     /**
80      * Evaluates the predicate returning false if any stored predicate returns false.
81      *
82      * @param object the input object
83      * @return true if none of decorated predicates return true
84      */

85     public boolean evaluate(Object JavaDoc object) {
86         for (int i = 0; i < iPredicates.length; i++) {
87             if (iPredicates[i].evaluate(object)) {
88                 return false;
89             }
90         }
91         return true;
92     }
93
94     /**
95      * Gets the predicates, do not modify the array.
96      *
97      * @return the predicates
98      * @since Commons Collections 3.1
99      */

100     public Predicate[] getPredicates() {
101         return iPredicates;
102     }
103
104 }
105
Popular Tags