001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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    public class ClonePart {
023    
024      private final String resourceId;
025      private final int unitStart;
026      private final int lineStart;
027      private final int lineEnd;
028    
029      /**
030       * Cache for hash code.
031       */
032      private int hash;
033    
034      public ClonePart(String resourceId, int unitStart, int lineStart, int lineEnd) {
035        this.resourceId = resourceId;
036        this.unitStart = unitStart;
037        this.lineStart = lineStart;
038        this.lineEnd = lineEnd;
039      }
040    
041      public String getResourceId() {
042        return resourceId;
043      }
044    
045      public int getUnitStart() {
046        return unitStart;
047      }
048    
049      public int getLineStart() {
050        return lineStart;
051      }
052    
053      public int getLineEnd() {
054        return lineEnd;
055      }
056    
057      public int getLines() {
058        return lineEnd - lineStart + 1;
059      }
060    
061      @Override
062      public boolean equals(Object obj) {
063        if (obj instanceof ClonePart) {
064          ClonePart another = (ClonePart) obj;
065          return another.resourceId.equals(resourceId)
066              && another.lineStart == lineStart
067              && another.lineEnd == lineEnd
068              && another.unitStart == unitStart;
069        }
070        return false;
071      }
072    
073      @Override
074      public int hashCode() {
075        int h = hash;
076        if (h == 0) {
077          h = resourceId.hashCode();
078          h = 31 * h + lineStart;
079          h = 31 * h + lineEnd;
080          h = 31 * h + unitStart;
081          hash = h;
082        }
083        return h;
084      }
085    
086      @Override
087      public String toString() {
088        return "'" + resourceId + "':[" + unitStart + "|" + lineStart + "-" + lineEnd + "]";
089      }
090    
091    }