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 */
020package org.sonar.duplications.detector;
021
022import java.util.Comparator;
023
024import org.sonar.duplications.index.ClonePart;
025import org.sonar.duplications.utils.FastStringComparator;
026
027/**
028 * Allows to determine if ClonePart includes another ClonePart.
029 * Inclusion is the partial order, so in fact this class violates contracts of {@link Comparator},
030 * however it allows to use {@link org.sonar.duplications.utils.SortedListsUtils} for efficient filtering.
031 */
032public final class ContainsInComparator implements Comparator<ClonePart> {
033
034  /**
035   * Defines order by resourceId.
036   */
037  public static final Comparator<ClonePart> RESOURCE_ID_COMPARATOR = new Comparator<ClonePart>() {
038    public int compare(ClonePart o1, ClonePart o2) {
039      return FastStringComparator.INSTANCE.compare(o1.getResourceId(), o2.getResourceId());
040    }
041  };
042
043  /**
044   * Defines order by resourceId and by unitStart.
045   */
046  public static final Comparator<ClonePart> CLONEPART_COMPARATOR = new Comparator<ClonePart>() {
047    public int compare(ClonePart o1, ClonePart o2) {
048      int c = RESOURCE_ID_COMPARATOR.compare(o1, o2);
049      if (c == 0) {
050        return o1.getUnitStart() - o2.getUnitStart();
051      }
052      return c;
053    }
054  };
055
056  private final int l1, l2;
057
058  /**
059   * Constructs new comparator for two parts with lengths {@code l1} and {@code l2} respectively.
060   */
061  public ContainsInComparator(int l1, int l2) {
062    this.l1 = l1;
063    this.l2 = l2;
064  }
065
066  /**
067   * Compares two parts on inclusion.
068   * part1 includes part2 if {@code (part1.resourceId == part2.resourceId) && (part1.unitStart <= part2.unitStart) && (part2.unitEnd <= part1.unitEnd)}.
069   * 
070   * @return 0 if part1 includes part2,
071   *         1 if resourceId of part1 is greater than resourceId of part2 or if unitStart of part1 is greater than unitStart of part2,
072   *         -1 in all other cases
073   */
074  public int compare(ClonePart part1, ClonePart part2) {
075    int c = RESOURCE_ID_COMPARATOR.compare(part1, part2);
076    if (c == 0) {
077      if (part1.getUnitStart() <= part2.getUnitStart()) {
078        if (part2.getUnitStart() + l2 <= part1.getUnitStart() + l1) {
079          return 0; // part1 contains part2
080        } else {
081          return -1; // SortedListsUtils#contains should continue search
082        }
083      } else {
084        return 1; // unitStart of part1 is less than unitStart of part2 - SortedListsUtils#contains should stop search
085      }
086    } else {
087      return c;
088    }
089  }
090
091}