View Javadoc

1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd;
5   
6   import java.util.HashSet;
7   import java.util.Iterator;
8   import java.util.List;
9   import java.util.Set;
10  
11  /***
12   * This class represents a Set of rules.
13   * @see Rule
14   */
15  public class RuleSet {
16  
17      private Set rules = new HashSet();
18      private String name = "";
19      private String description = "";
20  
21  	/***
22  	 * Returns the number of rules in this ruleset
23  	 * @return an int representing the number of rules
24  	 */
25      public int size() {
26          return rules.size();
27      }
28  
29  	/***
30  	 * Add a new rule to this ruleset
31  	 * @param rule the rule to be added
32  	 */
33      public void addRule(Rule rule) {
34          if (rule == null) {
35              throw new RuntimeException("Null Rule reference added to a RuleSet; that's a bug somewhere in PMD");
36          }
37          rules.add(rule);
38      }
39  
40  	/***
41  	 * Returns the actual Set of rules in this ruleset
42  	 * @return a Set with the rules. All objects are of type {@link Rule}
43  	 */
44      public Set getRules() {
45          return rules;
46      }
47  
48      /***
49       * @return true if any rule in the RuleSet needs the DFA layer
50       */
51      public boolean usesDFA() {
52          for (Iterator i = rules.iterator(); i.hasNext();) {
53              Rule r = (Rule) i.next();
54              if (r.usesDFA()) {
55                  return true;
56              }
57          }
58          return false;
59      }
60  
61  	/***
62  	 * Returns the Rule with the given name
63  	 *
64  	 * @param ruleName the name of the rule to find
65  	 * @return the rule or null if not found
66  	 */
67      public Rule getRuleByName(String ruleName) {
68          Rule rule = null;
69          for (Iterator i = rules.iterator(); i.hasNext() && (rule == null);) {
70              Rule r = (Rule) i.next();
71              if (r.getName().equals(ruleName)) {
72                  rule = r;
73              }
74          }
75          return rule;
76      }
77  
78  	/***
79  	 * Add a whole RuleSet to this RuleSet
80  	 *
81  	 * @param ruleSet the RuleSet to add
82  	 */
83      public void addRuleSet(RuleSet ruleSet) {
84          rules.addAll(ruleSet.getRules());
85      }
86  
87      public void apply(List acuList, RuleContext ctx) {
88          Iterator rs = rules.iterator();
89          while (rs.hasNext()) {
90              Rule rule = (Rule) rs.next();
91              rule.apply(acuList, ctx);
92          }
93      }
94  
95  	/***
96  	 * Gives the name of this ruleset
97  	 *
98  	 * @return a String representing the name
99  	 */
100     public String getName() {
101         return name;
102     }
103 
104 	/***
105 	 * Set the name of this ruleset
106 	 *
107 	 * @param name a String representing the name
108 	 */
109     public void setName(String name) {
110         this.name = name;
111     }
112 
113 	/***
114 	 * Gives the description of this ruleset
115 	 *
116 	 * @return a String representing the description
117 	 */
118     public String getDescription() {
119         return description;
120     }
121 
122 	/***
123 	 * Set the description of this ruleset
124 	 *
125 	 * @param description a String representing the description
126 	 */
127     public void setDescription(String description) {
128         this.description = description;
129     }
130     
131     /***
132      * @see java.lang.Object#equals(java.lang.Object)
133      */
134     public boolean equals(Object o) {
135         if ((o == null) || !(o instanceof RuleSet)) {
136             return false; // Trivial
137         }
138         
139         if (this == o) {
140             return true; // Basic equality
141         }
142         
143         RuleSet ruleSet = (RuleSet) o;
144         return this.getName().equals(ruleSet.getName()) && this.getRules().equals(ruleSet.getRules()); 
145     }
146     
147     /***
148      * @see java.lang.Object#hashCode()
149      */
150     public int hashCode() {
151         return this.getName().hashCode() + 13*this.getRules().hashCode();
152     }
153 }