You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Block.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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.
  10. *
  11. * This program 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.
  15. *
  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.
  19. */
  20. package org.sonar.duplications.block;
  21. /**
  22. * Represents part of source code between two lines.
  23. * If two blocks have the same {@link #getBlockHash() hash}, then we assume that there is a duplication in a code, which they represent.
  24. */
  25. public final class Block {
  26. private final String resourceId;
  27. private final ByteArray blockHash;
  28. private final int indexInFile;
  29. private final int startLine;
  30. private final int endLine;
  31. private int startUnit;
  32. private int endUnit;
  33. /**
  34. * Cache for hash code.
  35. */
  36. private int hash;
  37. /**
  38. * @since 2.14
  39. */
  40. public static Builder builder() {
  41. return new Builder();
  42. }
  43. /**
  44. * <p>Instances can be reused - it is safe to call {@link #build}
  45. * multiple times to build multiple blocks in series.</p>
  46. *
  47. * @since 2.14
  48. */
  49. public static final class Builder {
  50. private String resourceId;
  51. private ByteArray blockHash;
  52. private int indexInFile;
  53. private int startLine;
  54. private int endLine;
  55. private int startUnit;
  56. private int endUnit;
  57. public Builder setResourceId(String resourceId) {
  58. this.resourceId = resourceId;
  59. return this;
  60. }
  61. public Builder setBlockHash(ByteArray blockHash) {
  62. this.blockHash = blockHash;
  63. return this;
  64. }
  65. public Builder setIndexInFile(int index) {
  66. this.indexInFile = index;
  67. return this;
  68. }
  69. public Builder setLines(int start, int end) {
  70. this.startLine = start;
  71. this.endLine = end;
  72. return this;
  73. }
  74. public Builder setUnit(int start, int end) {
  75. this.startUnit = start;
  76. this.endUnit = end;
  77. return this;
  78. }
  79. public Block build() {
  80. return new Block(this);
  81. }
  82. }
  83. private Block(Builder builder) {
  84. this.resourceId = builder.resourceId;
  85. this.blockHash = builder.blockHash;
  86. this.indexInFile = builder.indexInFile;
  87. this.startLine = builder.startLine;
  88. this.endLine = builder.endLine;
  89. this.startUnit = builder.startUnit;
  90. this.endUnit = builder.endUnit;
  91. }
  92. public String getHashHex() {
  93. return getBlockHash().toString();
  94. }
  95. public String getResourceId() {
  96. return resourceId;
  97. }
  98. public ByteArray getBlockHash() {
  99. return blockHash;
  100. }
  101. public int getIndexInFile() {
  102. return indexInFile;
  103. }
  104. public int getStartLine() {
  105. return startLine;
  106. }
  107. public int getEndLine() {
  108. return endLine;
  109. }
  110. /**
  111. * @since 2.14
  112. */
  113. public int getStartUnit() {
  114. return startUnit;
  115. }
  116. /**
  117. * @since 2.14
  118. */
  119. public int getEndUnit() {
  120. return endUnit;
  121. }
  122. @Override
  123. public boolean equals(Object obj) {
  124. if (!(obj instanceof Block)) {
  125. return false;
  126. }
  127. Block other = (Block) obj;
  128. return resourceId.equals(other.resourceId)
  129. && blockHash.equals(other.blockHash)
  130. && indexInFile == other.indexInFile
  131. && startLine == other.startLine
  132. && endLine == other.endLine;
  133. }
  134. @Override
  135. public int hashCode() {
  136. int h = hash;
  137. if (h == 0) {
  138. h = resourceId.hashCode();
  139. h = 31 * h + blockHash.hashCode();
  140. h = 31 * h + indexInFile;
  141. h = 31 * h + startLine;
  142. h = 31 * h + endLine;
  143. hash = h;
  144. }
  145. return h;
  146. }
  147. @Override
  148. public String toString() {
  149. return "'" + resourceId + "'[" + indexInFile + "|" + startLine + "-" + endLine + "]:" + blockHash;
  150. }
  151. }