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    import java.util.List;
023    
024    import com.google.common.collect.ImmutableList;
025    
026    /**
027     * Groups a set of related {@link ClonePart}s.
028     */
029    public class CloneGroup {
030    
031      private final ClonePart originPart;
032      private final int cloneLength;
033      private final List<ClonePart> parts;
034    
035      /**
036       * Cache for hash code.
037       */
038      private int hash;
039    
040      public CloneGroup(int cloneLength, ClonePart origin, List<ClonePart> parts) {
041        this.cloneLength = cloneLength;
042        this.originPart = origin;
043        this.parts = ImmutableList.copyOf(parts);
044      }
045    
046      public ClonePart getOriginPart() {
047        return originPart;
048      }
049    
050      /**
051       * @return clone length in units (not in lines)
052       */
053      public int getCloneUnitLength() {
054        return cloneLength;
055      }
056    
057      public List<ClonePart> getCloneParts() {
058        return parts;
059      }
060    
061      @Override
062      public String toString() {
063        StringBuilder builder = new StringBuilder();
064        for (ClonePart part : parts) {
065          builder.append(part).append(" - ");
066        }
067        builder.append(cloneLength);
068        return builder.toString();
069      }
070    
071      /**
072       * Two groups are equal, if they have same length, same origins and contain same parts in same order.
073       */
074      @Override
075      public boolean equals(Object object) {
076        if (!(object instanceof CloneGroup)) {
077          return false;
078        }
079        CloneGroup another = (CloneGroup) object;
080        if (another.cloneLength != cloneLength || parts.size() != another.parts.size()) {
081          return false;
082        }
083        if (!originPart.equals(another.originPart)) {
084          return false;
085        }
086        boolean result = true;
087        for (int i = 0; i < parts.size(); i++) {
088          result &= another.parts.get(i).equals(parts.get(i));
089        }
090        return result;
091      }
092    
093      @Override
094      public int hashCode() {
095        int h = hash;
096        if (h == 0 && cloneLength != 0) {
097          for (ClonePart part : parts) {
098            h = 31 * h + part.hashCode();
099          }
100          h = 31 * h + originPart.hashCode();
101          h = 31 * h + cloneLength;
102          hash = h;
103        }
104        return h;
105      }
106    
107    }