001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.duplications.index;
021
022 import org.sonar.duplications.CodeFragment;
023
024 public class ClonePart implements CodeFragment {
025
026 private final String resourceId;
027 private final int startUnit;
028 private final int startLine;
029 private final int endLine;
030
031 /**
032 * Cache for hash code.
033 */
034 private int hash;
035
036 public ClonePart(String resourceId, int startUnit, int startLine, int endLine) {
037 this.resourceId = resourceId;
038 this.startUnit = startUnit;
039 this.startLine = startLine;
040 this.endLine = endLine;
041 }
042
043 public String getResourceId() {
044 return resourceId;
045 }
046
047 public int getUnitStart() {
048 return startUnit;
049 }
050
051 public int getStartLine() {
052 return startLine;
053 }
054
055 public int getEndLine() {
056 return endLine;
057 }
058
059 public int getLines() {
060 return endLine - startLine + 1;
061 }
062
063 @Override
064 public boolean equals(Object obj) {
065 if (obj instanceof ClonePart) {
066 ClonePart another = (ClonePart) obj;
067 return another.resourceId.equals(resourceId)
068 && another.startLine == startLine
069 && another.endLine == endLine
070 && another.startUnit == startUnit;
071 }
072 return false;
073 }
074
075 @Override
076 public int hashCode() {
077 int h = hash;
078 if (h == 0) {
079 h = resourceId.hashCode();
080 h = 31 * h + startLine;
081 h = 31 * h + endLine;
082 h = 31 * h + startUnit;
083 hash = h;
084 }
085 return h;
086 }
087
088 @Override
089 public String toString() {
090 return "'" + resourceId + "':[" + startUnit + "|" + startLine + "-" + endLine + "]";
091 }
092
093 }