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; 021 022import com.google.common.annotations.Beta; 023import org.apache.commons.lang.builder.EqualsBuilder; 024import org.apache.commons.lang.builder.HashCodeBuilder; 025import org.apache.commons.lang.builder.ToStringBuilder; 026import org.apache.commons.lang.builder.ToStringStyle; 027import org.sonar.api.batch.sensor.SensorContext; 028 029import java.util.List; 030 031/** 032 * <p/> 033 * A {@link Duplication} is a list of duplicated {@link Block}s. One block 034 * is considered as the original code and all others are duplicates. 035 * Use {@link SensorContext#newDuplication()} to manually create a duplication. Use {@link SensorContext#duplicationTokenBuilder(org.sonar.api.batch.fs.InputFile)} 036 * to feed tokens and let the core compute duplications. 037 * @since 5.1 038 */ 039@Beta 040public interface Duplication { 041 042 public static class Block { 043 private final String resourceKey; 044 private final int startLine; 045 private final int length; 046 047 public Block(String resourceKey, int startLine, int length) { 048 this.resourceKey = resourceKey; 049 this.startLine = startLine; 050 this.length = length; 051 } 052 053 public String resourceKey() { 054 return resourceKey; 055 } 056 057 public int startLine() { 058 return startLine; 059 } 060 061 public int length() { 062 return length; 063 } 064 065 // Just for unit tests 066 @Override 067 public boolean equals(Object obj) { 068 if (obj == null) { 069 return false; 070 } 071 if (obj == this) { 072 return true; 073 } 074 if (obj.getClass() != getClass()) { 075 return false; 076 } 077 Block rhs = (Block) obj; 078 return new EqualsBuilder() 079 .append(resourceKey, rhs.resourceKey) 080 .append(startLine, rhs.startLine) 081 .append(length, rhs.length).isEquals(); 082 } 083 084 @Override 085 public int hashCode() { 086 return new HashCodeBuilder(13, 43) 087 .append(resourceKey) 088 .append(startLine) 089 .append(length).toHashCode(); 090 } 091 092 @Override 093 public String toString() { 094 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE). 095 append("resourceKey", resourceKey). 096 append("startLine", startLine). 097 append("length", length). 098 toString(); 099 } 100 } 101 102 Block originBlock(); 103 104 List<Block> duplicates(); 105 106}