2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 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.api.batch.sensor.duplication.internal;
22 import org.sonar.api.batch.sensor.internal.SensorStorage;
24 import com.google.common.base.Preconditions;
25 import org.apache.commons.lang.builder.EqualsBuilder;
26 import org.apache.commons.lang.builder.HashCodeBuilder;
27 import org.apache.commons.lang.builder.ToStringBuilder;
28 import org.apache.commons.lang.builder.ToStringStyle;
29 import org.sonar.api.batch.fs.InputFile;
30 import org.sonar.api.batch.fs.internal.DefaultInputFile;
31 import org.sonar.api.batch.sensor.duplication.Duplication;
32 import org.sonar.api.batch.sensor.duplication.NewDuplication;
33 import org.sonar.api.batch.sensor.internal.DefaultStorable;
35 import javax.annotation.Nullable;
37 import java.util.LinkedList;
38 import java.util.List;
40 public class DefaultDuplication extends DefaultStorable implements NewDuplication, Duplication {
42 private Block originBlock;
43 private List<Block> duplicates = new LinkedList<>();
45 public DefaultDuplication() {
49 public DefaultDuplication(@Nullable SensorStorage storage) {
54 public DefaultDuplication originBlock(InputFile inputFile, int startLine, int endLine) {
55 Preconditions.checkArgument(inputFile != null, "InputFile can't be null");
56 validateLineArgument(inputFile, startLine, "startLine");
57 validateLineArgument(inputFile, endLine, "endLine");
58 originBlock = new Duplication.Block(((DefaultInputFile) inputFile).key(), startLine, endLine - startLine + 1);
63 public DefaultDuplication isDuplicatedBy(InputFile sameOrOtherFile, int startLine, int endLine) {
64 Preconditions.checkArgument(sameOrOtherFile != null, "InputFile can't be null");
65 validateLineArgument(sameOrOtherFile, startLine, "startLine");
66 validateLineArgument(sameOrOtherFile, endLine, "endLine");
67 return isDuplicatedBy(((DefaultInputFile) sameOrOtherFile).key(), startLine, endLine);
71 * For internal use. Global duplications are referencing files outside of current project so
72 * no way to manipulate an InputFile.
74 public DefaultDuplication isDuplicatedBy(String fileKey, int startLine, int endLine) {
75 Preconditions.checkNotNull(originBlock, "Call originBlock() first");
76 duplicates.add(new Duplication.Block(fileKey, startLine, endLine - startLine + 1));
81 public void doSave() {
82 Preconditions.checkNotNull(originBlock, "Call originBlock() first");
83 Preconditions.checkState(!duplicates.isEmpty(), "No duplicates. Call isDuplicatedBy()");
88 public Block originBlock() {
92 public DefaultDuplication setOriginBlock(Block originBlock) {
93 this.originBlock = originBlock;
98 public List<Block> duplicates() {
102 // Just for unit tests
104 public boolean equals(Object obj) {
111 if (obj.getClass() != getClass()) {
114 DefaultDuplication rhs = (DefaultDuplication) obj;
115 EqualsBuilder equalsBuilder = new EqualsBuilder()
116 .append(originBlock, rhs.originBlock)
117 .append(duplicates.size(), rhs.duplicates.size());
118 if (duplicates.size() == rhs.duplicates.size()) {
119 for (int i = 0; i < duplicates.size(); i++) {
120 equalsBuilder.append(duplicates.get(i), rhs.duplicates.get(i));
123 return equalsBuilder.isEquals();
127 public int hashCode() {
128 HashCodeBuilder hcBuilder = new HashCodeBuilder(17, 37)
130 .append(duplicates.size());
131 for (int i = 0; i < duplicates.size(); i++) {
132 hcBuilder.append(duplicates.get(i));
134 return hcBuilder.toHashCode();
138 public String toString() {
139 return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).
140 append("origin", originBlock).
141 append("duplicates", duplicates, true).