2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * SonarQube is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.plugins.core.issue.tracking;
22 import com.google.common.annotations.VisibleForTesting;
23 import org.sonar.plugins.core.issue.tracking.HashedSequence;
24 import org.sonar.plugins.core.issue.tracking.HashedSequenceComparator;
25 import org.sonar.plugins.core.issue.tracking.StringText;
26 import org.sonar.plugins.core.issue.tracking.StringTextComparator;
28 public class ViolationTrackingBlocksRecognizer {
30 private final HashedSequence<StringText> a;
31 private final HashedSequence<StringText> b;
32 private final HashedSequenceComparator<StringText> cmp;
35 public ViolationTrackingBlocksRecognizer(String referenceSource, String source) {
36 this.a = HashedSequence.wrap(new StringText(referenceSource), StringTextComparator.IGNORE_WHITESPACE);
37 this.b = HashedSequence.wrap(new StringText(source), StringTextComparator.IGNORE_WHITESPACE);
38 this.cmp = new HashedSequenceComparator<StringText>(StringTextComparator.IGNORE_WHITESPACE);
41 public ViolationTrackingBlocksRecognizer(HashedSequence<StringText> a, HashedSequence<StringText> b, HashedSequenceComparator<StringText> cmp) {
47 public boolean isValidLineInReference(Integer line) {
48 return (line != null) && (0 <= line - 1) && (line - 1 < a.length());
51 public boolean isValidLineInSource(Integer line) {
52 return (line != null) && (0 <= line - 1) && (line - 1 < b.length());
56 * @param startA number of line from first version of text (numbering starts from 0)
57 * @param startB number of line from second version of text (numbering starts from 0)
59 public int computeLengthOfMaximalBlock(int startA, int startB) {
60 if (!cmp.equals(a, startA, b, startB)) {
66 while (ai < a.length() && bi < b.length() && cmp.equals(a, ai, b, bi)) {
73 while (ai >= 0 && bi >= 0 && cmp.equals(a, ai, b, bi)) {
78 // Note that position (startA, startB) was counted twice