KickJava   Java API By Example, From Geeks To Geeks.

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


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 true if both the predicates return true.
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 AndPredicate implements Predicate, PredicateDecorator, Serializable JavaDoc {
31
32     /** Serial version UID */
33     static final long serialVersionUID = 4189014213763186912L;
34     
35     /** The array of predicates to call */
36     private final Predicate iPredicate1;
37     /** The array of predicates to call */
38     private final Predicate iPredicate2;
39     
40     /**
41      * Factory to create the predicate.
42      *
43      * @param predicate1 the first predicate to check, not null
44      * @param predicate2 the second predicate to check, not null
45      * @return the <code>and</code> predicate
46      * @throws IllegalArgumentException if either predicate is null
47      */

48     public static Predicate getInstance(Predicate predicate1, Predicate predicate2) {
49         if (predicate1 == null || predicate2 == null) {
50             throw new IllegalArgumentException JavaDoc("Predicate must not be null");
51         }
52         return new AndPredicate(predicate1, predicate2);
53     }
54
55     /**
56      * Constructor that performs no validation.
57      * Use <code>getInstance</code> if you want that.
58      *
59      * @param predicate1 the first predicate to check, not null
60      * @param predicate2 the second predicate to check, not null
61      */

62     public AndPredicate(Predicate predicate1, Predicate predicate2) {
63         super();
64         iPredicate1 = predicate1;
65         iPredicate2 = predicate2;
66     }
67
68     /**
69      * Evaluates the predicate returning true if both predicates return true.
70      *
71      * @param object the input object
72      * @return true if both decorated predicates return true
73      */

74     public boolean evaluate(Object JavaDoc object) {
75        return (iPredicate1.evaluate(object) && iPredicate2.evaluate(object));
76     }
77
78     /**
79      * Gets the two predicates being decorated as an array.
80      *
81      * @return the predicates
82      * @since Commons Collections 3.1
83      */

84     public Predicate[] getPredicates() {
85         return new Predicate[] {iPredicate1, iPredicate2};
86     }
87
88 }
89
Popular Tags