KickJava   Java API By Example, From Geeks To Geeks.

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


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 only one 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 OnePredicate implements Predicate, PredicateDecorator, Serializable JavaDoc {
32
33     /** Serial version UID */
34     static final long serialVersionUID = -8125389089924745785L;
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 OnePredicate(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 OnePredicate(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 OnePredicate(Predicate[] predicates) {
75         super();
76         iPredicates = predicates;
77     }
78
79     /**
80      * Evaluates the predicate returning true if only one decorated predicate
81      * returns true.
82      *
83      * @param object the input object
84      * @return true if only one decorated predicate returns true
85      */

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

105     public Predicate[] getPredicates() {
106         return iPredicates;
107     }
108
109 }
110
Popular Tags