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.Iterator; 9 import java.util.Set; 10 import java.util.TreeSet; 11 12 public class Match implements Comparable { 13 14 private int tokenCount; 15 private int lineCount; 16 private Set markSet = new TreeSet(); 17 private TokenEntry[] marks = new TokenEntry[2]; 18 private String code; 19 private MatchCode mc; 20 21 public static class MatchCode { 22 23 private int first; 24 private int second; 25 26 public MatchCode() { 27 } 28 29 public MatchCode(TokenEntry m1, TokenEntry m2) { 30 first = m1.getIndex(); 31 second = m2.getIndex(); 32 } 33 34 public int hashCode() { 35 return first + 37 * second; 36 } 37 38 public boolean equals(Object other) { 39 MatchCode mc = (MatchCode) other; 40 return mc.first == first && mc.second == second; 41 } 42 43 public void setFirst(int first) { 44 this.first = first; 45 } 46 47 public void setSecond(int second) { 48 this.second = second; 49 } 50 51 } 52 53 public Match(int tokenCount, TokenEntry first, TokenEntry second) { 54 markSet.add(first); 55 markSet.add(second); 56 marks[0] = first; 57 marks[1] = second; 58 this.tokenCount = tokenCount; 59 } 60 61 public int getMarkCount() { 62 return markSet.size(); 63 } 64 65 public void setLineCount(int lineCount) { 66 this.lineCount = lineCount; 67 } 68 69 public int getLineCount() { 70 return this.lineCount; 71 } 72 73 public int getTokenCount() { 74 return this.tokenCount; 75 } 76 77 public String getSourceCodeSlice() { 78 return this.code; 79 } 80 81 public void setSourceCodeSlice(String code) { 82 this.code = code; 83 } 84 85 public Iterator iterator() { 86 return markSet.iterator(); 87 } 88 89 public int compareTo(Object o) { 90 Match other = (Match) o; 91 int diff = other.getTokenCount() - getTokenCount(); 92 if (diff != 0) { 93 return diff; 94 } 95 return other.getFirstMark().getIndex() - getFirstMark().getIndex(); 96 } 97 98 public TokenEntry getFirstMark() { 99 return marks[0]; 100 } 101 102 public TokenEntry getSecondMark() { 103 return marks[1]; 104 } 105 106 public String toString() { 107 return "Match: " + PMD.EOL + "tokenCount = " + tokenCount + PMD.EOL + "marks = " + markSet.size(); 108 } 109 110 public Set getMarkSet() { 111 return markSet; 112 } 113 114 public MatchCode getMatchCode() { 115 if (mc == null) { 116 mc = new MatchCode(marks[0], marks[1]); 117 } 118 return mc; 119 } 120 121 public int getEndIndex() { 122 return marks[1].getIndex() + getTokenCount() - 1; 123 } 124 125 public void setMarkSet(Set markSet) { 126 this.markSet = markSet; 127 } 128 129 }