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 com.google.common.base.Preconditions;
23 import org.apache.commons.lang.builder.EqualsBuilder;
24 import org.apache.commons.lang.builder.HashCodeBuilder;
25 import org.apache.commons.lang.builder.ToStringBuilder;
26 import org.apache.commons.lang.builder.ToStringStyle;
27 import org.sonar.api.batch.fs.InputFile;
28 import org.sonar.api.batch.fs.internal.DefaultInputFile;
29 import org.sonar.api.batch.sensor.SensorStorage;
30 import org.sonar.api.batch.sensor.duplication.Duplication;
31 import org.sonar.api.batch.sensor.duplication.NewDuplication;
32 import org.sonar.api.batch.sensor.internal.DefaultStorable;
34 import javax.annotation.Nullable;
36 import java.util.LinkedList;
37 import java.util.List;
39 public class DefaultDuplication extends DefaultStorable implements NewDuplication, Duplication {
41 private Block originBlock;
42 private List<Block> duplicates = new LinkedList<Duplication.Block>();
44 public DefaultDuplication() {
48 public DefaultDuplication(@Nullable SensorStorage storage) {
53 public DefaultDuplication originBlock(InputFile inputFile, int startLine, int endLine) {
54 Preconditions.checkArgument(inputFile != null, "InputFile can't be null");
55 validateLineArgument(inputFile, startLine, "startLine");
56 validateLineArgument(inputFile, endLine, "endLine");
57 originBlock = new Duplication.Block(((DefaultInputFile) inputFile).key(), startLine, endLine - startLine + 1);
62 public DefaultDuplication isDuplicatedBy(InputFile sameOrOtherFile, int startLine, int endLine) {
63 Preconditions.checkArgument(sameOrOtherFile != null, "InputFile can't be null");
64 validateLineArgument(sameOrOtherFile, startLine, "startLine");
65 validateLineArgument(sameOrOtherFile, endLine, "endLine");
66 return isDuplicatedBy(((DefaultInputFile) sameOrOtherFile).key(), startLine, endLine);
70 * For internal use. Global duplications are referencing files outside of current project so
71 * no way to manipulate an InputFile.
73 public DefaultDuplication isDuplicatedBy(String fileKey, int startLine, int endLine) {
74 Preconditions.checkNotNull(originBlock, "Call originBlock() first");
75 duplicates.add(new Duplication.Block(fileKey, startLine, endLine - startLine + 1));
80 public void doSave() {
81 Preconditions.checkNotNull(originBlock, "Call originBlock() first");
82 Preconditions.checkState(!duplicates.isEmpty(), "No duplicates. Call isDuplicatedBy()");
87 public Block originBlock() {
91 public DefaultDuplication setOriginBlock(Block originBlock) {
92 this.originBlock = originBlock;
97 public List<Block> duplicates() {
101 // Just for unit tests
103 public boolean equals(Object obj) {
110 if (obj.getClass() != getClass()) {
113 DefaultDuplication rhs = (DefaultDuplication) obj;
114 EqualsBuilder equalsBuilder = new EqualsBuilder()
115 .append(originBlock, rhs.originBlock)
116 .append(duplicates.size(), rhs.duplicates.size());
117 if (duplicates.size() == rhs.duplicates.size()) {
118 for (int i = 0; i < duplicates.size(); i++) {
119 equalsBuilder.append(duplicates.get(i), rhs.duplicates.get(i));
122 return equalsBuilder.isEquals();
126 public int hashCode() {
127 HashCodeBuilder hcBuilder = new HashCodeBuilder(17, 37)
129 .append(duplicates.size());
130 for (int i = 0; i < duplicates.size(); i++) {
131 hcBuilder.append(duplicates.get(i));
133 return hcBuilder.toHashCode();
137 public String toString() {
138 return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).
139 append("origin", originBlock).
140 append("duplicates", duplicates, true).