aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-duplications
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-duplications')
-rw-r--r--sonar-duplications/src/main/java/org/sonar/duplications/index/CloneIndex.java2
-rw-r--r--sonar-duplications/src/main/java/org/sonar/duplications/index/MemoryCloneIndex.java5
-rw-r--r--sonar-duplications/src/main/java/org/sonar/duplications/index/PackedMemoryCloneIndex.java20
-rw-r--r--sonar-duplications/src/test/java/org/sonar/duplications/index/PackedMemoryCloneIndexTest.java1
4 files changed, 27 insertions, 1 deletions
diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/index/CloneIndex.java b/sonar-duplications/src/main/java/org/sonar/duplications/index/CloneIndex.java
index e529a600f54..11cdc7e680f 100644
--- a/sonar-duplications/src/main/java/org/sonar/duplications/index/CloneIndex.java
+++ b/sonar-duplications/src/main/java/org/sonar/duplications/index/CloneIndex.java
@@ -52,4 +52,6 @@ public interface CloneIndex {
*/
Iterator<ResourceBlocks> iterator();
+
+ int noResources();
}
diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/index/MemoryCloneIndex.java b/sonar-duplications/src/main/java/org/sonar/duplications/index/MemoryCloneIndex.java
index 0acb4edc0bb..bbfff14680f 100644
--- a/sonar-duplications/src/main/java/org/sonar/duplications/index/MemoryCloneIndex.java
+++ b/sonar-duplications/src/main/java/org/sonar/duplications/index/MemoryCloneIndex.java
@@ -54,4 +54,9 @@ public class MemoryCloneIndex implements CloneIndex {
throw new UnsupportedOperationException();
}
+ @Override
+ public int noResources() {
+ return byResource.keySet().size();
+ }
+
}
diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/index/PackedMemoryCloneIndex.java b/sonar-duplications/src/main/java/org/sonar/duplications/index/PackedMemoryCloneIndex.java
index a0e43ffab6f..97a97d792c4 100644
--- a/sonar-duplications/src/main/java/org/sonar/duplications/index/PackedMemoryCloneIndex.java
+++ b/sonar-duplications/src/main/java/org/sonar/duplications/index/PackedMemoryCloneIndex.java
@@ -120,7 +120,7 @@ public class PackedMemoryCloneIndex extends AbstractCloneIndex {
private Block createBlock(int index, String resourceId, @Nullable ByteArray byteHash) {
int offset = index * blockInts;
ByteArray blockHash;
-
+
if (byteHash == null) {
int[] hash = new int[hashInts];
for (int j = 0; j < hashInts; j++) {
@@ -371,4 +371,22 @@ public class PackedMemoryCloneIndex extends AbstractCloneIndex {
}
};
+ @Override
+ /**
+ * Computation is O(N)
+ */
+ public int noResources() {
+ ensureSorted();
+ int count = 0;
+ String lastResource = null;
+
+ for (int i = 0; i < size; i++) {
+ String resource = resourceIds[resourceIdsIndex[i]];
+ if (resource != null && !resource.equals(lastResource)) {
+ count++;
+ lastResource = resource;
+ }
+ }
+ return count;
+ }
}
diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/index/PackedMemoryCloneIndexTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/index/PackedMemoryCloneIndexTest.java
index d96f6ebeaf9..09ff7f055cc 100644
--- a/sonar-duplications/src/test/java/org/sonar/duplications/index/PackedMemoryCloneIndexTest.java
+++ b/sonar-duplications/src/test/java/org/sonar/duplications/index/PackedMemoryCloneIndexTest.java
@@ -54,6 +54,7 @@ public class PackedMemoryCloneIndexTest {
index.insert(newBlock("e", 2));
index.insert(newBlock("e", 3));
+ assertThat(index.noResources()).isEqualTo(5);
assertThat(index.getBySequenceHash(new ByteArray(1L)).size(), is(5));
assertThat(index.getBySequenceHash(new ByteArray(2L)).size(), is(2));
assertThat(index.getBySequenceHash(new ByteArray(3L)).size(), is(1));