aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/sonar-core-plugin
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2012-03-15 21:44:29 +0400
committerEvgeny Mandrikov <mandrikov@gmail.com>2012-03-15 21:44:40 +0400
commit44094c161d794bf3d107f6e0d3b5fc71df72b5ac (patch)
treecdd71e98e490229dd47bf260ede28e9e3d0c8781 /plugins/sonar-core-plugin
parentda854ba498a3c6ecad5bea7756067f0e703ec792 (diff)
downloadsonarqube-44094c161d794bf3d107f6e0d3b5fc71df72b5ac.tar.gz
sonarqube-44094c161d794bf3d107f6e0d3b5fc71df72b5ac.zip
SONAR-3072 sonar-diff module is useless for the moment, so remove it
Move all code directly into sonar-core-plugin.
Diffstat (limited to 'plugins/sonar-core-plugin')
-rw-r--r--plugins/sonar-core-plugin/pom.xml5
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationTrackingBlocksRecognizer.java8
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/HashedSequence.java39
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/HashedSequenceComparator.java43
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/Sequence.java32
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/SequenceComparator.java42
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/StringText.java71
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/StringTextComparator.java93
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/package-info.java25
-rw-r--r--plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/tracking/StringTextComparatorTest.java25
-rw-r--r--plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/tracking/StringTextTest.java22
11 files changed, 396 insertions, 9 deletions
diff --git a/plugins/sonar-core-plugin/pom.xml b/plugins/sonar-core-plugin/pom.xml
index 987bb0e93a8..88d2101ab66 100644
--- a/plugins/sonar-core-plugin/pom.xml
+++ b/plugins/sonar-core-plugin/pom.xml
@@ -15,11 +15,6 @@
<dependencies>
<dependency>
<groupId>org.codehaus.sonar</groupId>
- <artifactId>sonar-diff</artifactId>
- <version>${project.version}</version>
- </dependency>
- <dependency>
- <groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-plugin-api</artifactId>
<scope>provided</scope>
</dependency>
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationTrackingBlocksRecognizer.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationTrackingBlocksRecognizer.java
index 0ba647693d9..7193c6b940d 100644
--- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationTrackingBlocksRecognizer.java
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationTrackingBlocksRecognizer.java
@@ -19,10 +19,10 @@
*/
package org.sonar.plugins.core.timemachine;
-import org.sonar.diff.HashedSequence;
-import org.sonar.diff.HashedSequenceComparator;
-import org.sonar.diff.StringText;
-import org.sonar.diff.StringTextComparator;
+import org.sonar.plugins.core.timemachine.tracking.HashedSequence;
+import org.sonar.plugins.core.timemachine.tracking.HashedSequenceComparator;
+import org.sonar.plugins.core.timemachine.tracking.StringText;
+import org.sonar.plugins.core.timemachine.tracking.StringTextComparator;
public class ViolationTrackingBlocksRecognizer {
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/HashedSequence.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/HashedSequence.java
new file mode 100644
index 00000000000..332d6d32456
--- /dev/null
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/HashedSequence.java
@@ -0,0 +1,39 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 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
+ */
+package org.sonar.plugins.core.timemachine.tracking;
+
+/**
+ * Wraps a {@link Sequence} to assign hash codes to elements.
+ */
+public class HashedSequence<S extends Sequence> implements Sequence {
+
+ final S base;
+ final int[] hashes;
+
+ public HashedSequence(S base, int[] hashes) {
+ this.base = base;
+ this.hashes = hashes;
+ }
+
+ public int length() {
+ return base.length();
+ }
+
+}
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/HashedSequenceComparator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/HashedSequenceComparator.java
new file mode 100644
index 00000000000..f7a88a4596e
--- /dev/null
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/HashedSequenceComparator.java
@@ -0,0 +1,43 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 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
+ */
+package org.sonar.plugins.core.timemachine.tracking;
+
+/**
+ * Wrap another {@link SequenceComparator} for use with {@link HashedSequence}.
+ */
+public class HashedSequenceComparator<S extends Sequence> implements SequenceComparator<HashedSequence<S>> {
+
+ private final SequenceComparator<? super S> cmp;
+
+ public HashedSequenceComparator(SequenceComparator<? super S> cmp) {
+ this.cmp = cmp;
+ }
+
+ @Override
+ public boolean equals(HashedSequence<S> a, int ai, HashedSequence<S> b, int bi) {
+ return a.hashes[ai] == b.hashes[bi] && cmp.equals(a.base, ai, b.base, bi);
+ }
+
+ @Override
+ public int hash(HashedSequence<S> seq, int i) {
+ return seq.hashes[i];
+ }
+
+}
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/Sequence.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/Sequence.java
new file mode 100644
index 00000000000..28a4277b265
--- /dev/null
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/Sequence.java
@@ -0,0 +1,32 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 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
+ */
+package org.sonar.plugins.core.timemachine.tracking;
+
+/**
+ * Arbitrary sequence of elements.
+ */
+public interface Sequence {
+
+ /**
+ * @return total number of items in the sequence
+ */
+ int length();
+
+}
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/SequenceComparator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/SequenceComparator.java
new file mode 100644
index 00000000000..4bf73dd93ba
--- /dev/null
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/SequenceComparator.java
@@ -0,0 +1,42 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 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
+ */
+package org.sonar.plugins.core.timemachine.tracking;
+
+/**
+ * Equivalence function for a {@link Sequence}.
+ */
+public interface SequenceComparator<S extends Sequence> {
+
+ /**
+ * Compare two items to determine if they are equivalent.
+ */
+ boolean equals(S a, int ai, S b, int bi);
+
+ /**
+ * Get a hash value for an item in a sequence.
+ *
+ * If two items are equal according to this comparator's
+ * {@link #equals(Sequence, int, Sequence, int)} method,
+ * then this hash method must produce the same integer result for both items.
+ * However not required to have different hash values for different items.
+ */
+ int hash(S seq, int i);
+
+}
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/StringText.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/StringText.java
new file mode 100644
index 00000000000..756c6e6fce6
--- /dev/null
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/StringText.java
@@ -0,0 +1,71 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 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
+ */
+package org.sonar.plugins.core.timemachine.tracking;
+
+import com.google.common.collect.Lists;
+
+import java.util.List;
+
+/**
+ * Text is a {@link Sequence} of lines.
+ */
+public class StringText implements Sequence {
+
+ final String content;
+
+ /**
+ * Map of line number to starting position within {@link #content}.
+ */
+ final List<Integer> lines;
+
+ public StringText(String str) {
+ this.content = str;
+ this.lines = lineMap(content, 0, content.length());
+ }
+
+ @Override
+ public int length() {
+ return lines.size() - 2;
+ }
+
+ private static List<Integer> lineMap(String buf, int ptr, int end) {
+ List<Integer> lines = Lists.newArrayList();
+ lines.add(Integer.MIN_VALUE);
+ for (; ptr < end; ptr = nextLF(buf, ptr)) {
+ lines.add(ptr);
+ }
+ lines.add(end);
+ return lines;
+ }
+
+ private static final int nextLF(String b, int ptr) {
+ return next(b, ptr, '\n');
+ }
+
+ private static final int next(final String b, int ptr, final char chrA) {
+ final int sz = b.length();
+ while (ptr < sz) {
+ if (b.charAt(ptr++) == chrA)
+ return ptr;
+ }
+ return ptr;
+ }
+
+}
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/StringTextComparator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/StringTextComparator.java
new file mode 100644
index 00000000000..3c4430add31
--- /dev/null
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/StringTextComparator.java
@@ -0,0 +1,93 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 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
+ */
+package org.sonar.plugins.core.timemachine.tracking;
+
+/**
+ * Equivalence function for {@link StringText}.
+ */
+public abstract class StringTextComparator implements SequenceComparator<StringText> {
+
+ /**
+ * Ignores all whitespace.
+ */
+ public static final StringTextComparator IGNORE_WHITESPACE = new StringTextComparator() {
+
+ @Override
+ public boolean equals(StringText a, int ai, StringText b, int bi) {
+ ai++;
+ bi++;
+ int as = a.lines.get(ai);
+ int bs = b.lines.get(bi);
+ int ae = a.lines.get(ai + 1);
+ int be = b.lines.get(bi + 1);
+ ae = trimTrailingWhitespace(a.content, as, ae);
+ be = trimTrailingWhitespace(b.content, bs, be);
+ while ((as < ae) && (bs < be)) {
+ char ac = a.content.charAt(as);
+ char bc = b.content.charAt(bs);
+ while ((as < ae - 1) && (Character.isWhitespace(ac))) {
+ as++;
+ ac = a.content.charAt(as);
+ }
+ while ((bs < be - 1) && (Character.isWhitespace(bc))) {
+ bs++;
+ bc = b.content.charAt(bs);
+ }
+ if (ac != bc) {
+ return false;
+ }
+ as++;
+ bs++;
+ }
+ return (as == ae) && (bs == be);
+ }
+
+ @Override
+ protected int hashRegion(String content, int start, int end) {
+ int hash = 5381;
+ for (; start < end; start++) {
+ char c = content.charAt(start);
+ if (!Character.isWhitespace(c)) {
+ hash = ((hash << 5) + hash) + (c & 0xff);
+ }
+ }
+ return hash;
+ }
+
+ };
+
+ @Override
+ public int hash(StringText seq, int line) {
+ final int begin = seq.lines.get(line + 1);
+ final int end = seq.lines.get(line + 2);
+ return hashRegion(seq.content, begin, end);
+ }
+
+ protected abstract int hashRegion(String content, int start, int end);
+
+ public static int trimTrailingWhitespace(String content, int start, int end) {
+ end--;
+ while (start <= end && Character.isWhitespace(content.charAt(end))) {
+ end--;
+ }
+ return end + 1;
+ }
+
+}
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/package-info.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/package-info.java
new file mode 100644
index 00000000000..344eb1dd688
--- /dev/null
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/tracking/package-info.java
@@ -0,0 +1,25 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 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
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonar.plugins.core.timemachine.tracking;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+
diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/tracking/StringTextComparatorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/tracking/StringTextComparatorTest.java
new file mode 100644
index 00000000000..5d437a3e360
--- /dev/null
+++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/tracking/StringTextComparatorTest.java
@@ -0,0 +1,25 @@
+package org.sonar.plugins.core.timemachine.tracking;
+
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+public class StringTextComparatorTest {
+
+ @Test
+ public void testEquals() {
+ StringTextComparator cmp = StringTextComparator.IGNORE_WHITESPACE;
+
+ StringText a = new StringText("abc\nabc\na bc");
+ StringText b = new StringText("abc\nabc d\nab c");
+
+ assertThat("abc == abc", cmp.equals(a, 0, b, 0), is(true));
+ assertThat("abc != abc d", cmp.equals(a, 1, b, 1), is(false));
+ assertThat("a bc == ab c", cmp.equals(a, 2, b, 2), is(true));
+ assertThat(cmp.hash(a, 0), equalTo(cmp.hash(b, 0)));
+ assertThat(cmp.hash(a, 2), equalTo(cmp.hash(b, 2)));
+ }
+
+}
diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/tracking/StringTextTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/tracking/StringTextTest.java
new file mode 100644
index 00000000000..15898084af1
--- /dev/null
+++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/tracking/StringTextTest.java
@@ -0,0 +1,22 @@
+package org.sonar.plugins.core.timemachine.tracking;
+
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+public class StringTextTest {
+
+ @Test
+ public void testEmpty() {
+ StringText r = new StringText("");
+ assertThat(r.length(), is(0));
+ }
+
+ @Test
+ public void testTwoLines() {
+ StringText r = new StringText("a\nb");
+ assertThat(r.length(), is(2));
+ }
+
+}