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.Comparator;
7   
8   public class RuleViolation {
9   
10      public static class RuleViolationComparator implements Comparator {
11          //
12          // Changed logic of Comparator so that rules in the same file
13          // get grouped together in the output report.
14          // DDP 7/11/2002
15          //
16          public int compare(Object o1, Object o2) {
17              RuleViolation r1 = (RuleViolation) o1;
18              RuleViolation r2 = (RuleViolation) o2;
19              if (!r1.getFilename().equals(r2.getFilename())) {
20                  return r1.getFilename().compareTo(r2.getFilename());
21              }
22  
23              if (r1.getLine() != r2.getLine())
24                  return r1.getLine() - r2.getLine();
25  
26              if (r1.getDescription() != null && r2.getDescription() != null && !r1.getDescription().equals(r2.getDescription())) {
27                  return r1.getDescription().compareTo(r2.getDescription());
28              }
29              // line number diff maps nicely to compare()
30              return r1.getLine() - r2.getLine();
31          }
32      }
33  
34      private int line;
35      private Rule rule;
36      private String description;
37      private String filename;
38  
39      public RuleViolation(Rule rule, int line, RuleContext ctx) {
40          this(rule, line, rule.getMessage(), ctx);
41      }
42  
43      public RuleViolation(Rule rule, int line, String specificDescription, RuleContext ctx) {
44          this.line = line;
45          this.rule = rule;
46          this.description = specificDescription;
47          this.filename = ctx.getSourceCodeFilename();
48      }
49  
50      public Rule getRule() {
51          return rule;
52      }
53  
54      public int getLine() {
55          return line;
56      }
57  
58      public String getDescription() {
59          return description;
60      }
61  
62      public String getFilename() {
63          return filename;
64      }
65  }