aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-duplications/src/main/java/org/sonar/duplications/cpd/Match.java
blob: 0ec9a9ad72c1166ec18679e69be01647aca74c6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2008-2011 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * Sonar is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Sonar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */

/**
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
 */
package org.sonar.duplications.cpd;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import net.sourceforge.pmd.cpd.TokenEntry;

public class Match implements Comparable<Match> {

  public static final String EOL = System.getProperty("line.separator", "\n");

  private int tokenCount;
  private int lineCount;
  private Set<TokenEntry> markSet = new TreeSet<TokenEntry>();
  private TokenEntry[] marks = new TokenEntry[2];
  private String code;
  private MatchCode mc;
  private String label;

  public static final Comparator<Match> MatchesComparator = new Comparator<Match>() {

    public int compare(Match ma, Match mb) {
      return mb.getMarkCount() - ma.getMarkCount();
    }
  };

  public static final Comparator<Match> LinesComparator = new Comparator<Match>() {

    public int compare(Match ma, Match mb) {
      return mb.getLineCount() - ma.getLineCount();
    }
  };

  public static final Comparator<Match> LabelComparator = new Comparator<Match>() {

    public int compare(Match ma, Match mb) {
      if (ma.getLabel() == null) {
        return 1;
      }
      if (mb.getLabel() == null) {
        return -1;
      }
      return mb.getLabel().compareTo(ma.getLabel());
    }
  };

  public static final Comparator<Match> LengthComparator = new Comparator<Match>() {

    public int compare(Match ma, Match mb) {
      return mb.getLineCount() - ma.getLineCount();
    }
  };

  public static class MatchCode {

    private int first;
    private int second;

    public MatchCode() {
    }

    public MatchCode(TokenEntry m1, TokenEntry m2) {
      first = m1.getIndex();
      second = m2.getIndex();
    }

    public int hashCode() {
      return first + 37 * second;
    }

    public boolean equals(Object other) {
      MatchCode mc = (MatchCode) other;
      return mc.first == first && mc.second == second;
    }

    public void setFirst(int first) {
      this.first = first;
    }

    public void setSecond(int second) {
      this.second = second;
    }

  }

  public Match(int tokenCount, TokenEntry first, TokenEntry second) {
    markSet.add(first);
    markSet.add(second);
    marks[0] = first;
    marks[1] = second;
    this.tokenCount = tokenCount;
  }

  public int getMarkCount() {
    return markSet.size();
  }

  public void setLineCount(int lineCount) {
    this.lineCount = lineCount;
  }

  public int getLineCount() {
    return this.lineCount;
  }

  public int getTokenCount() {
    return this.tokenCount;
  }

  public String getSourceCodeSlice() {
    return this.code;
  }

  public void setSourceCodeSlice(String code) {
    this.code = code;
  }

  public Iterator<TokenEntry> iterator() {
    return markSet.iterator();
  }

  public int compareTo(Match other) {
    int diff = other.getTokenCount() - getTokenCount();
    if (diff != 0) {
      return diff;
    }
    return other.getFirstMark().getIndex() - getFirstMark().getIndex();
  }

  public TokenEntry getFirstMark() {
    return marks[0];
  }

  public TokenEntry getSecondMark() {
    return marks[1];
  }

  public String toString() {
    return "Match: " + EOL + "tokenCount = " + tokenCount + EOL + "marks = " + markSet.size();
  }

  public Set<TokenEntry> getMarkSet() {
    return markSet;
  }

  public MatchCode getMatchCode() {
    if (mc == null) {
      mc = new MatchCode(marks[0], marks[1]);
    }
    return mc;
  }

  public int getEndIndex() {
    return marks[1].getIndex() + getTokenCount() - 1;
  }

  public void setMarkSet(Set<TokenEntry> markSet) {
    this.markSet = markSet;
  }

  public void setLabel(String aLabel) {
    label = aLabel;
  }

  public String getLabel() {
    return label;
  }
}