*/
package org.sonar.plugins.cpd;
-import static org.mockito.Matchers.argThat;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.SensorContext;
import org.sonar.duplications.index.CloneGroup;
import org.sonar.duplications.index.ClonePart;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.*;
+
public class SonarEngineTest {
private SensorContext context;
}
private CloneGroup newCloneGroup(ClonePart... parts) {
- return new CloneGroup(0, parts[0], Arrays.asList(parts));
+ return CloneGroup.builder().setLength(0).setOrigin(parts[0]).setParts(Arrays.asList(parts)).build();
}
}
public interface Language {
- String fileSeparator = System.getProperty("file.separator");
-
Tokenizer getTokenizer();
FilenameFilter getFileFilter();
parts.add(part);
}
- filter.add(new CloneGroup(cloneLength, origin, parts));
+ filter.add(CloneGroup.builder().setLength(cloneLength).setOrigin(origin).setParts(parts).build());
}
}
*/
@Override
public void endOfGroup() {
- int lengthInUnits = 0;
ClonePart origin = null;
+
+ CloneGroup.Builder builder = CloneGroup.builder().setLength(length);
+
List<ClonePart> parts = Lists.newArrayListWithCapacity(count);
for (int[] b : blockNumbers) {
Block firstBlock = text.getBlock(b[0]);
if (origin == null) {
origin = part;
// To calculate length important to use the origin, because otherwise block may come from DB without required data
- lengthInUnits = lastBlock.getEndUnit() - firstBlock.getStartUnit() + 1;
+ builder.setLengthInUnits(lastBlock.getEndUnit() - firstBlock.getStartUnit() + 1);
} else if (part.getUnitStart() < origin.getUnitStart()) {
origin = part;
}
}
Collections.sort(parts, ContainsInComparator.CLONEPART_COMPARATOR);
+ builder.setOrigin(origin).setParts(parts);
- CloneGroup group = new CloneGroup(length, origin, parts);
- group.setLengthInUnits(lengthInUnits);
-
- filter(group);
+ filter(builder.build());
reset();
}
*/
private int hash;
- public CloneGroup(int cloneLength, ClonePart origin, List<ClonePart> parts) {
- this.cloneLength = cloneLength;
- this.originPart = origin;
- this.parts = ImmutableList.copyOf(parts);
+ /**
+ * @since 2.14
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * @since 2.14
+ */
+ public static final class Builder {
+ private ClonePart origin;
+ private int length;
+ private int lengthInUnits;
+ private List<ClonePart> parts;
+
+ public Builder setLength(int length) {
+ this.length = length;
+ return this;
+ }
+
+ public Builder setOrigin(ClonePart origin) {
+ this.origin = origin;
+ return this;
+ }
+
+ public Builder setParts(List<ClonePart> parts) {
+ this.parts = ImmutableList.copyOf(parts);
+ return this;
+ }
+
+ public Builder setLengthInUnits(int length) {
+ this.lengthInUnits = length;
+ return this;
+ }
+
+ public CloneGroup build() {
+ return new CloneGroup(this);
+ }
+ }
+
+ private CloneGroup(Builder builder) {
+ this.cloneLength = builder.length;
+ this.originPart = builder.origin;
+ this.parts = builder.parts;
+ this.length = builder.lengthInUnits;
}
public ClonePart getOriginPart() {
return length;
}
- /**
- * TODO get rid of this method, otherwise class is not immutable
- *
- * @since 2.14
- * @see #getLengthInUnits()
- */
- @Beta
- public void setLengthInUnits(int length) {
- this.length = length;
- }
-
/**
* @return clone length in {@link org.sonar.duplications.block.Block}s
*/
*/
package org.sonar.duplications.detector.original;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
-
-import java.util.Arrays;
-
import org.junit.Test;
import org.sonar.duplications.index.CloneGroup;
import org.sonar.duplications.index.ClonePart;
+import java.util.Arrays;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.*;
+
public class FilterTest {
/**
* Creates new group from list of parts, origin - is a first part from list.
*/
private CloneGroup newCloneGroup(int len, ClonePart... parts) {
- return new CloneGroup(len, parts[0], Arrays.asList(parts));
+ return CloneGroup.builder().setLength(len).setOrigin(parts[0]).setParts(Arrays.asList(parts)).build();
}
}