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