View Javadoc

1   // Copyright (c) 2003, Chad Woolley, All rights reserved.
2   
3   package org.virtualmock.rule;
4   
5   import java.util.ArrayList;
6   import java.util.HashMap;
7   import java.util.Iterator;
8   import java.util.List;
9   import java.util.Map;
10  import org.virtualmock.resource.ResourceManager;
11  
12  
13  /***
14   * Class which handles evaluation and reporting of rules and rule violations.
15   * This method checkAllRulesInCurrentPhase performs checks for all Rules that
16   * are applicable to the current phase. New rules can be added at runtime.  If
17   * a new rule is added which has the same key as an existing rule, the old
18   * rule will be replaced.
19   *
20   * @author Chad Woolley
21   * @version $Revision: 1.26 $
22   *
23   * @todo Figure out how rules will be handled - ignore, warn, fail...
24   */
25  public class RuleManager {
26      /*** The ArrayList containing all the rules. */
27      private ArrayList rules = null;
28  
29      /*** The Map containing all the rules. */
30      private Map rulesMap = null;
31  
32      /*** The ResourceManager to be used when creating messages. */
33      private ResourceManager resourceManager = null;
34  
35      /***
36       * Creates a new RuleManager object.
37       *
38       * @param rules the initial list of Rules which will be managed
39       * @param resourceManager the ResourceManager to be used when creating
40       *        messages
41       */
42      public RuleManager(List rules, ResourceManager resourceManager) {
43          this.rules = (ArrayList) rules;
44          this.resourceManager = resourceManager;
45  
46          rulesMap = new HashMap();
47  
48          // transform the list of rules into an internal HashMap keyed by 
49          // the keys of the rules
50          Iterator iterator = rules.iterator();
51  
52          while (iterator.hasNext()) {
53              Rule rule = (Rule) iterator.next();
54              String key = rule.getKey();
55  
56              // insert the rule into the internal map, keyed by its key
57              rulesMap.put(key, rule);
58          }
59      }
60  
61      /***
62       * Returns the Rule for the specified key, or null if no Rule matches the
63       * key.
64       *
65       * @param key DOCUMENT ME! (Constructor Parameter)
66       *
67       * @return the Rule for the specified key, or null if no Rule matches the
68       *         key.
69       */
70      public Rule getRule(String key) {
71          return (Rule) rulesMap.get(key);
72      }
73  
74      /***
75       * Returns all of the rules that are currently defined.
76       *
77       * @return all of the rules that are currently defined
78       */
79      public List getRules() {
80          return rules;
81      }
82  
83      /***
84       * Adds a rule to be checked.
85       *
86       * @param rule the Rule that is being added
87       */
88      public void addRule(Rule rule) {
89          rules.add(rule);
90      }
91  
92      /***
93       * Checks all rules in current phase.
94       *
95       * @throws Error if the rule was violated
96       *
97       * @todo for now, only shows first message for first failed rule.  Should
98       *       optionally show all messages
99       * @todo for now, processes all rules regardless of phase
100      */
101     public void checkAllRulesInCurrentPhase() {
102         Iterator iterator = rules.iterator();
103 
104         while (iterator.hasNext()) {
105             Rule rule = (Rule) iterator.next();
106 
107             String[] messages = rule.getViolationMessages();
108 
109             // hardcoded to fail
110             if (messages.length > 0) {
111                 throw new Error(messages[0]);
112             }
113         }
114     }
115 }