1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.duplications.block;
import org.sonar.duplications.CodeFragment;
/**
* Represents part of source code between two lines.
* If two blocks have the same {@link #getBlockHash() hash}, then we assume that there is a duplication in a code, which they represent.
*/
public final class Block implements CodeFragment {
private final String resourceId;
private final ByteArray blockHash;
private final int indexInFile;
private final int startLine;
private final int endLine;
private int startUnit;
private int endUnit;
/**
* Cache for hash code.
*/
private int hash;
/**
* @since 2.14
*/
public static Builder builder() {
return new Builder();
}
/**
* <p>Instances can be reused - it is safe to call {@link #build}
* multiple times to build multiple blocks in series.</p>
*
* @since 2.14
*/
public static final class Builder {
private String resourceId;
private ByteArray blockHash;
private int indexInFile;
private int startLine;
private int endLine;
private int startUnit;
private int endUnit;
public Builder setResourceId(String resourceId) {
this.resourceId = resourceId;
return this;
}
public Builder setBlockHash(ByteArray blockHash) {
this.blockHash = blockHash;
return this;
}
public Builder setIndexInFile(int index) {
this.indexInFile = index;
return this;
}
public Builder setLines(int start, int end) {
this.startLine = start;
this.endLine = end;
return this;
}
public Builder setUnit(int start, int end) {
this.startUnit = start;
this.endUnit = end;
return this;
}
public Block build() {
return new Block(this);
}
}
private Block(Builder builder) {
this.resourceId = builder.resourceId;
this.blockHash = builder.blockHash;
this.indexInFile = builder.indexInFile;
this.startLine = builder.startLine;
this.endLine = builder.endLine;
this.startUnit = builder.startUnit;
this.endUnit = builder.endUnit;
}
public String getHashHex() {
return getBlockHash().toString();
}
public String getResourceId() {
return resourceId;
}
public ByteArray getBlockHash() {
return blockHash;
}
public int getIndexInFile() {
return indexInFile;
}
@Override
public int getStartLine() {
return startLine;
}
@Override
public int getEndLine() {
return endLine;
}
/**
* @since 2.14
*/
public int getStartUnit() {
return startUnit;
}
/**
* @since 2.14
*/
public int getEndUnit() {
return endUnit;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Block)) {
return false;
}
Block other = (Block) obj;
return resourceId.equals(other.resourceId)
&& blockHash.equals(other.blockHash)
&& indexInFile == other.indexInFile
&& startLine == other.startLine
&& endLine == other.endLine;
}
@Override
public int hashCode() {
int h = hash;
if (h == 0) {
h = resourceId.hashCode();
h = 31 * h + blockHash.hashCode();
h = 31 * h + indexInFile;
h = 31 * h + startLine;
h = 31 * h + endLine;
hash = h;
}
return h;
}
@Override
public String toString() {
return "'" + resourceId + "'[" + indexInFile + "|" + startLine + "-" + endLine + "]:" + blockHash;
}
}
|