001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.batch.sensor.duplication.internal;
021
022import com.google.common.base.Preconditions;
023import org.sonar.api.batch.fs.InputFile;
024import org.sonar.api.batch.fs.internal.DefaultInputFile;
025import org.sonar.api.batch.sensor.duplication.DuplicationBuilder;
026import org.sonar.api.batch.sensor.duplication.DuplicationGroup;
027
028import java.util.ArrayList;
029import java.util.List;
030
031public class DefaultDuplicationBuilder implements DuplicationBuilder {
032
033  private final InputFile inputFile;
034  private DuplicationGroup current = null;
035  private List<DuplicationGroup> duplications;
036
037  public DefaultDuplicationBuilder(InputFile inputFile) {
038    this.inputFile = inputFile;
039    duplications = new ArrayList<DuplicationGroup>();
040  }
041
042  @Override
043  public DuplicationBuilder originBlock(int startLine, int endLine) {
044    if (current != null) {
045      duplications.add(current);
046    }
047    current = new DuplicationGroup(new DuplicationGroup.Block(((DefaultInputFile) inputFile).key(), startLine, endLine - startLine + 1));
048    return this;
049  }
050
051  @Override
052  public DuplicationBuilder isDuplicatedBy(InputFile sameOrOtherFile, int startLine, int endLine) {
053    return isDuplicatedBy(((DefaultInputFile) sameOrOtherFile).key(), startLine, endLine);
054  }
055
056  /**
057   * For internal use. Global duplications are referencing files outside of current project so
058   * no way to manipulate an InputFile.
059   */
060  public DuplicationBuilder isDuplicatedBy(String fileKey, int startLine, int endLine) {
061    Preconditions.checkNotNull(current, "Call originBlock() first");
062    current.addDuplicate(new DuplicationGroup.Block(fileKey, startLine, endLine - startLine + 1));
063    return this;
064  }
065
066  @Override
067  public List<DuplicationGroup> build() {
068    Preconditions.checkNotNull(current, "Call originBlock() first");
069    duplications.add(current);
070    List<DuplicationGroup> result = duplications;
071    reset();
072    return result;
073  }
074
075  private void reset() {
076    duplications = new ArrayList<DuplicationGroup>();
077    current = null;
078  }
079}