Skip to content

Commit

Permalink
SONAR-9111 Remove Guava dependency from sonar-duplications
Browse files Browse the repository at this point in the history
  • Loading branch information
henryju committed Oct 4, 2017
1 parent b811d8e commit 7aae338
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 127 deletions.
4 changes: 0 additions & 4 deletions sonar-duplications/pom.xml
Expand Up @@ -21,10 +21,6 @@
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-channel</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
Expand Down
Expand Up @@ -19,10 +19,9 @@
*/
package org.sonar.duplications.index;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* Groups a set of related {@link ClonePart}s.
Expand Down Expand Up @@ -66,12 +65,12 @@ public Builder setOrigin(ClonePart origin) {
}

public Builder setParts(List<ClonePart> parts) {
this.parts = ImmutableList.copyOf(parts);
this.parts = new ArrayList<>(parts);
return this;
}

public Builder addPart(ClonePart part) {
Preconditions.checkNotNull(part);
Objects.requireNonNull(part);
this.parts.add(part);
return this;
}
Expand Down
Expand Up @@ -19,34 +19,35 @@
*/
package org.sonar.duplications.index;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.sonar.duplications.block.Block;
import org.sonar.duplications.block.ByteArray;
import org.sonar.duplications.index.PackedMemoryCloneIndex.ResourceBlocks;

import java.util.Collection;
import java.util.Iterator;

public class MemoryCloneIndex implements CloneIndex {

private Multimap<String, Block> byResource = ArrayListMultimap.create();
private Multimap<ByteArray, Block> byHash = ArrayListMultimap.create();
private Map<String, List<Block>> byResource = new LinkedHashMap<>();
private Map<ByteArray, List<Block>> byHash = new LinkedHashMap<>();

@Override
public Collection<Block> getByResourceId(String resourceId) {
return byResource.get(resourceId);
return byResource.computeIfAbsent(resourceId, k -> new ArrayList<>());
}

@Override
public Collection<Block> getBySequenceHash(ByteArray sequenceHash) {
return byHash.get(sequenceHash);
return byHash.computeIfAbsent(sequenceHash, k -> new ArrayList<>());
}

@Override
public void insert(Block block) {
byResource.put(block.getResourceId(), block);
byHash.put(block.getBlockHash(), block);
getByResourceId(block.getResourceId()).add(block);
getBySequenceHash(block.getBlockHash()).add(block);
}

@Override
Expand Down
Expand Up @@ -19,10 +19,8 @@
*/
package org.sonar.duplications.internal.pmd;

import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.pmd.cpd.SourceCode;
import net.sourceforge.pmd.cpd.TokenEntry;
Expand Down Expand Up @@ -54,8 +52,10 @@ public List<TokensLine> chunk(String fileName, Reader fileReader) {
TokenEntry.clearImages();
try {
tokenizer.tokenize(sourceCode, tokens);
} catch (IOException e) {
throw Throwables.propagate(e);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
TokenEntry.clearImages();
return convert(tokens.getTokens());
Expand All @@ -66,7 +66,7 @@ public List<TokensLine> chunk(String fileName, Reader fileReader) {
* tokens ordered by occurrence in source code and last token is EOF.
*/
public static List<TokensLine> convert(List<TokenEntry> tokens) {
ImmutableList.Builder<TokensLine> result = ImmutableList.builder();
List<TokensLine> result = new ArrayList<>();
StringBuilder sb = new StringBuilder();
int startLine = Integer.MIN_VALUE;
int startIndex = 0;
Expand All @@ -85,10 +85,10 @@ public static List<TokensLine> convert(List<TokenEntry> tokens) {
}
}
addNewTokensLine(result, startIndex, currentIndex, startLine, sb);
return result.build();
return result;
}

private static void addNewTokensLine(ImmutableList.Builder<TokensLine> result, int startUnit, int endUnit, int startLine, StringBuilder sb) {
private static void addNewTokensLine(List<TokensLine> result, int startUnit, int endUnit, int startLine, StringBuilder sb) {
if (sb.length() != 0) {
result.add(new TokensLine(startUnit, endUnit, startLine, sb.toString()));
sb.setLength(0);
Expand Down
Expand Up @@ -19,7 +19,6 @@
*/
package org.sonar.duplications.internal.pmd;

import com.google.common.base.Preconditions;
import org.sonar.duplications.CodeFragment;

/**
Expand All @@ -36,7 +35,9 @@ public class TokensLine implements CodeFragment {
private final int endUnit;

public TokensLine(int startUnit, int endUnit, int startLine, String value) {
Preconditions.checkArgument(startLine > 0);
if (startLine <= 0) {
throw new IllegalArgumentException("Start line should be strictly positive");
}
// TODO do we have requirements for length and hashcode ?
this.startLine = startLine;
this.value = value;
Expand Down
Expand Up @@ -19,14 +19,16 @@
*/
package org.sonar.duplications.block;

import com.google.common.collect.Lists;
import org.junit.Test;
import org.sonar.duplications.statement.Statement;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.sonar.duplications.statement.Statement;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;

/**
Expand Down Expand Up @@ -131,7 +133,7 @@ public void shouldNotBuildBlocksWhenNotEnoughStatements() {
* Creates list of statements from Strings, each statement on a new line starting from 0.
*/
protected static List<Statement> createStatementsFromStrings(String... values) {
List<Statement> result = Lists.newArrayList();
List<Statement> result = new ArrayList<>();
for (int i = 0; i < values.length; i++) {
result.add(new Statement(i, i, values[i]));
}
Expand Down
Expand Up @@ -19,7 +19,10 @@
*/
package org.sonar.duplications.detector;

import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.duplications.block.Block;
Expand All @@ -30,10 +33,6 @@
import org.sonar.duplications.index.MemoryCloneIndex;
import org.sonar.duplications.junit.TestNamePrinter;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
Expand Down Expand Up @@ -367,8 +366,7 @@ public void shouldReturnEmptyListWhenNoBlocksForFile() {
public void problemWithEndOfFile() {
CloneIndex cloneIndex = createIndex(
newBlocks("b", "1 2 3 4"));
Block[] fileBlocks =
newBlocks("a", "1 2 3");
Block[] fileBlocks = newBlocks("a", "1 2 3");
List<CloneGroup> clones = detect(cloneIndex, fileBlocks);

print(clones);
Expand Down Expand Up @@ -399,7 +397,7 @@ public void same_lines_but_different_indexes() {
Block.Builder block = Block.builder()
.setResourceId("a")
.setLines(0, 1);
Block[] fileBlocks = new Block[]{
Block[] fileBlocks = new Block[] {
block.setBlockHash(new ByteArray("1".getBytes())).setIndexInFile(0).build(),
block.setBlockHash(new ByteArray("2".getBytes())).setIndexInFile(1).build(),
block.setBlockHash(new ByteArray("1".getBytes())).setIndexInFile(2).build()
Expand All @@ -426,7 +424,7 @@ protected static void print(List<CloneGroup> clones) {
}

protected static Block[] newBlocks(String resourceId, String hashes) {
List<Block> result = Lists.newArrayList();
List<Block> result = new ArrayList<>();
int indexInFile = 0;
for (int i = 0; i < hashes.length(); i += 2) {
Block block = newBlock(resourceId, new ByteArray("0" + hashes.charAt(i)), indexInFile);
Expand Down

0 comments on commit 7aae338

Please sign in to comment.