KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > gems > constraints > Or


1 /*****************************************************************************
2  * Copyright (C) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8
9 package org.picocontainer.gems.constraints;
10
11 import org.picocontainer.ComponentAdapter;
12 import org.picocontainer.PicoVisitor;
13
14 /**
15  * Aggregates multiple constraints together using boolean OR logic.
16  * Constraints are short-circuited as in java.
17  *
18  * @author Nick Sieger
19  * @version 1.1
20  */

21 public class Or extends AbstractConstraint {
22     private Constraint[] children;
23
24     public Or(Constraint c1, Constraint c2) {
25         children = new Constraint[2];
26         children[0] = c1;
27         children[1] = c2;
28     }
29
30     public Or(Constraint c1, Constraint c2, Constraint c3) {
31         children = new Constraint[3];
32         children[0] = c1;
33         children[1] = c2;
34         children[2] = c3;
35     }
36
37     public Or(Constraint[] cc) {
38         children = new Constraint[cc.length];
39         System.arraycopy(cc, 0, children, 0, cc.length);
40     }
41
42     public boolean evaluate(ComponentAdapter adapter) {
43         for (int i = 0; i < children.length; i++) {
44             if (children[i].evaluate(adapter)) {
45                 return true;
46             }
47         }
48         return false;
49     }
50
51     public void accept(PicoVisitor visitor) {
52         super.accept(visitor);
53         for (int i = 0; i < children.length; i++) {
54             children[i].accept(visitor);
55         }
56     }
57 }
58
Popular Tags