View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.cpd;
5   
6   import net.sourceforge.pmd.PMD;
7   
8   import java.util.Comparator;
9   import java.util.Iterator;
10  import java.util.Set;
11  import java.util.TreeSet;
12  
13  public class Match implements Comparable<Match> {
14  
15      private int tokenCount;
16      private int lineCount;
17      private Set<TokenEntry> markSet = new TreeSet<TokenEntry>();    
18      private String code;    
19      private String label;
20      
21      public static final Comparator<Match> MATCHES_COMPARATOR = new Comparator<Match>() {
22      	public int compare(Match ma, Match mb) {
23      		return mb.getMarkCount() - ma.getMarkCount();
24      	}
25      };
26      
27      public static final Comparator<Match> LINES_COMPARATOR = new Comparator<Match>() {
28      	public int compare(Match ma, Match mb) {
29      		return mb.getLineCount() - ma.getLineCount();
30      	}
31      };
32      
33      public static final Comparator<Match> LABEL_COMPARATOR = new Comparator<Match>() {
34      	public int compare(Match ma, Match mb) {
35      		if (ma.getLabel() == null) {
36      		    return 1;
37      		}
38      		if (mb.getLabel() == null) {
39      		    return -1;
40      		}
41      		return mb.getLabel().compareTo(ma.getLabel());
42      	}
43      };
44      
45      public static final Comparator<Match> LENGTH_COMPARATOR = new Comparator<Match>() {
46          public int compare(Match ma, Match mb) {
47              return mb.getLineCount() - ma.getLineCount();
48          }
49      };
50  
51      public Match(int tokenCount, TokenEntry first, TokenEntry second) {
52          markSet.add(first);
53          markSet.add(second);
54          this.tokenCount = tokenCount;
55      }
56  
57      public int getMarkCount() {
58          return markSet.size();
59      }
60  
61      public void setLineCount(int lineCount) {
62          this.lineCount = lineCount;
63      }
64  
65      public int getLineCount() {
66          return this.lineCount;
67      }
68  
69      public int getTokenCount() {
70          return this.tokenCount;
71      }
72  
73      public String getSourceCodeSlice() {
74          return this.code;
75      }
76  
77      public void setSourceCodeSlice(String code) {
78          this.code = code;
79      }
80  
81      public Iterator<TokenEntry> iterator() {
82          return markSet.iterator();
83      }
84  
85      public int compareTo(Match other) {
86          int diff = other.getTokenCount() - getTokenCount();
87          if (diff != 0) {
88              return diff;
89          }
90          return getFirstMark().getIndex() - other.getFirstMark().getIndex();
91      }
92  
93      public TokenEntry getFirstMark() {
94          return getMark(0);
95      }
96  
97      public TokenEntry getSecondMark() {
98          return getMark(1);
99      }
100 
101     public String toString() {
102         return "Match: " + PMD.EOL + "tokenCount = " + tokenCount + PMD.EOL + "marks = " + markSet.size();
103     }
104 
105     public Set<TokenEntry> getMarkSet() {
106         return markSet;
107     }
108 
109     public int getEndIndex() {
110         return getMark(0).getIndex() + getTokenCount() - 1;
111     }
112 
113     public void setMarkSet(Set<TokenEntry> markSet) {
114         this.markSet = markSet;
115     }
116 
117     public void setLabel(String aLabel) {
118     	label = aLabel;
119     }
120     
121     public String getLabel() {
122     	return label;
123     }
124     
125     public void addTokenEntry(TokenEntry entry){
126         markSet.add(entry);                
127     }
128     
129     private TokenEntry getMark(int index) {
130         TokenEntry result = null;
131         int i = 0;
132         for (Iterator<TokenEntry> it = markSet.iterator(); it.hasNext() && i < index + 1; ){            
133             result = it.next();
134             i++;
135         }
136         return result;
137     }
138 }