View Javadoc

1   
2   package org.state4j.sm;
3   
4   import org.state4j.exceptions.PreconditionViolatedException;
5   
6   
7   public class SmGuardConditionImpl implements SmGuardCondition {
8       private String      triggerClassName;
9       private Class       triggerClass;
10      private SmEvaluator evaluator;
11      private String      name;
12  
13      public void setTriggerClassName(String trigger) {
14          this.triggerClassName = trigger;
15          if (this.triggerClassName == null) {
16              this.setTriggerClass(null);
17          } else {
18              try {
19                  Class event = Class.forName(trigger);
20                  if (!SmEvent.class.isAssignableFrom(event)) {
21                      throw new PreconditionViolatedException("Sorry!! cannot setTriggerClassName class " + trigger
22                              + " isn't IPEvent instance");
23                  }
24                  this.setTriggerClass(event);
25              } catch (ClassNotFoundException e) {
26                  throw new PreconditionViolatedException("Sorry!! cannot setTriggerClassName class not found: "
27                          + trigger);
28              }
29          }
30      }
31  
32      public String getName() {
33          return name;
34      }
35  
36      public void setName(String name) {
37          this.name = name;
38      }
39  
40      public boolean hasName() {
41          return this.name != null;
42      }
43  
44      public boolean evaluate(SmContext cntx) {
45          return this.evaluateTrigger(cntx) && this.evaluateCondition(cntx);
46      }
47  
48      protected boolean evaluateCondition(SmContext cntx) {
49          if (!hasEvaluator()) {
50              return true;
51          } else {
52              return getEvaluator().evaluate(cntx);
53          }
54      }
55  
56      protected boolean evaluateTrigger(SmContext cntx) {
57          if (this.hasTriggerClassName()) {
58              if (!cntx.hasCurrentEvent()) {
59                  return false;
60              }
61              if (!this.getTriggerClass().isInstance(cntx.getCurrentEvent())) {
62                  return false;
63              }
64          }
65          return true;
66      }
67  
68      public String getTriggerClassName() {
69          return this.triggerClassName;
70      }
71  
72      public boolean hasTriggerClassName() {
73          return this.triggerClassName != null;
74      }
75  
76      private void setTriggerClass(Class triggerClass) {
77          this.triggerClass = triggerClass;
78      }
79  
80      public Class getTriggerClass() {
81          if (!this.hasTriggerClass()) {
82              throw new PreconditionViolatedException("Sorry!! cannot get TriggerClass");
83          }
84          return triggerClass;
85      }
86  
87      public boolean hasTriggerClass() {
88          return this.triggerClass != null;
89      }
90  
91      public boolean hasEvaluator() {
92          return evaluator != null;
93      }
94  
95      public SmEvaluator getEvaluator() {
96          if (!hasEvaluator()) {
97              throw new RuntimeException("this.evaluator is null and can't be returned");
98          }
99          return this.evaluator;
100     }
101 
102     public void setEvaluator(SmEvaluator evaluator) {
103         this.evaluator = evaluator;
104     }
105 }