KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > actions > rules > RuleGroup


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.actions.rules;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.openharmonise.vfs.*;
25
26
27 /**
28  * Rule that groups other rules and returns true in either an
29  * <code>AND</code> or <code>OR</code> mode.
30  *
31  * @author Matthew Large
32  * @version $Revision: 1.1 $
33  *
34  */

35 public class RuleGroup implements EnableRule {
36
37     boolean m_bComparator = true;
38
39     /**
40      * Rules in this rule group.
41      */

42     private ArrayList JavaDoc m_aEnableRules = new ArrayList JavaDoc();
43     
44     /**
45      * Group type.
46      */

47     private String JavaDoc m_sGroupingType = "AND";
48     
49     /**
50      * Group type identifier for OR groups.
51      */

52     public static String JavaDoc GROUPTYPE_OR = "OR";
53     
54     /**
55      * Group type identifier for AND groups.
56      */

57     public static String JavaDoc GROUPTYPE_AND = "AND";
58
59     /**
60      *
61      */

62     public RuleGroup() {
63         super();
64     }
65
66     /* (non-Javadoc)
67      * @see com.simulacramedia.contentmanager.actions.rules.EnableRule#isEnabled(com.simulacramedia.vfs.VirtualFile)
68      */

69     public boolean isEnabled(VirtualFile vfFile) {
70         boolean bEnabled = true;
71         
72         if(vfFile!=null) {
73             if(this.m_sGroupingType.equals(RuleGroup.GROUPTYPE_AND)) {
74                 Iterator JavaDoc itor = this.m_aEnableRules.iterator();
75                 while(itor.hasNext()) {
76                     if( !((EnableRule)itor.next()).isEnabled(vfFile) ) {
77                         bEnabled = false;
78                     }
79                 }
80             } else {
81                 bEnabled = false;
82                 Iterator JavaDoc itor = this.m_aEnableRules.iterator();
83                 while(itor.hasNext()) {
84                     if( ((EnableRule)itor.next()).isEnabled(vfFile) ) {
85                         bEnabled = true;
86                     }
87                 }
88             }
89         }
90
91         return this.m_bComparator==bEnabled;
92     }
93     
94     /**
95      * Adds a rule to this rule group.
96      *
97      * @param rule Rule to add
98      */

99     public void addEnableRule(EnableRule rule) {
100         this.m_aEnableRules.add(rule);
101     }
102
103     /* (non-Javadoc)
104      * @see com.simulacramedia.contentmanager.actions.rules.EnableRule#setResultComparator(boolean)
105      */

106     public void setResultComparator(boolean bComparator) {
107         this.m_bComparator = bComparator;
108     }
109     
110     /**
111      * Sets the group type. This can be either <code>AND</code> or
112      * <code>OR</code>. An <code>AND</code> group will return true
113      * if all of the rules it contains return true. An <code>OR</code>
114      * group will return true if any of the rules it contains return true.
115      *
116      * @param sGroupType Group type
117      * @see RuleGroup#GROUPTYPE_AND
118      * @see RuleGroup#GROUPTYPE_OR
119      */

120     public void setGroupType(String JavaDoc sGroupType) {
121         if(sGroupType.equals(RuleGroup.GROUPTYPE_AND) || sGroupType.equals(RuleGroup.GROUPTYPE_OR)) {
122             this.m_sGroupingType = sGroupType;
123         }
124     }
125
126 }
127
Popular Tags