KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > util > patterns > flow > Conditional


1 package org.mr.core.util.patterns.flow;
2
3 import org.mr.core.util.exceptions.CreationException;
4 import org.w3c.dom.Element JavaDoc;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
8
9 /*
10  * Copyright 2002 by
11  * <a HREF="http://www.coridan.com">Coridan</a>
12  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
13  *
14  * The contents of this file are subject to the Mozilla Public License Version
15  * 1.1 (the "License"); you may not use this file except in compliance with the
16  * License. You may obtain a copy of the License at
17  * http://www.mozilla.org/MPL/
18  *
19  * Software distributed under the License is distributed on an "AS IS" basis,
20  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
21  * for the specific language governing rights and limitations under the
22  * License.
23  *
24  * The Original Code is "MantaRay" (TM).
25  *
26  * The Initial Developer of the Original Code is Coridan.
27  * Portions created by the Initial Developer are Copyright (C) 2006
28  * Coridan Inc. All Rights Reserved.
29  *
30  * Contributor(s): all the names of the contributors are added in the source
31  * code where applicable.
32  *
33  * Alternatively, the contents of this file may be used under the terms of the
34  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
35  * provisions of LGPL are applicable instead of those above. If you wish to
36  * allow use of your version of this file only under the terms of the LGPL
37  * License and not to allow others to use your version of this file under
38  * the MPL, indicate your decision by deleting the provisions above and
39  * replace them with the notice and other provisions required by the LGPL.
40  * If you do not delete the provisions above, a recipient may use your version
41  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
42  
43  *
44  * This library is free software; you can redistribute it and/or modify it
45  * under the terms of the MPL as stated above or under the terms of the GNU
46  * Lesser General Public License as published by the Free Software Foundation;
47  * either version 2.1 of the License, or any later version.
48  *
49  * This library is distributed in the hope that it will be useful, but WITHOUT
50  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
51  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
52  * License for more details.
53  */

54
55  /**
56  * User: Moti Tal
57  * Date: Feb 21, 2005
58  * Time: 12:36:02 PM
59  *
60  * This class represents sets of condition.
61  * The Conditional check recursively for sub Conditional/Condition met its conditions.
62  */

63
64 public class Conditional implements Condition{
65
66     public final static String JavaDoc AND_OPERATOR = "AND";
67     public final static String JavaDoc OR_OPERATOR = "OR";
68
69     char m_operator;
70     List JavaDoc m_conditions = null;
71
72     public void configure(Element JavaDoc i_object) throws CreationException {
73         String JavaDoc operator = i_object.getAttribute("operator");
74         if(AND_OPERATOR.equals(operator) || operator == null || operator.length() == 0)
75             m_operator = '+';
76         else if(OR_OPERATOR.equals(operator))
77             m_operator = '|';
78         else
79             throw new CreationException("Unsupported operator="+operator);
80 // if(AND_OPERATOR.equals(operator))
81
m_conditions = new ArrayList JavaDoc();
82     }
83
84     public void addCondition(Condition i_condition){
85         m_conditions.add(i_condition);
86     }
87
88     public boolean metCondition(Object JavaDoc i_object) {
89         for(int i = 0 ; i < m_conditions.size() ; i++){
90             Condition condition = (Condition)m_conditions.get(i);
91             boolean metCondition = false;
92             try {
93                 metCondition = condition.metCondition(i_object);
94             } catch (Exception JavaDoc e) {
95                 metCondition = false;
96             }
97             switch(m_operator){
98                 case '+':
99                     if(!metCondition)
100                         return false;
101                     break;
102                 case '|':
103                     if(metCondition)
104                         return true;
105             }
106         }
107         if('+' == m_operator)
108             return true;
109         return false;
110     }
111 }
Popular Tags