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 */ 020 package org.sonar.api.batch.analyzer.issue.internal; 021 022 import com.google.common.base.Preconditions; 023 import com.google.common.base.Strings; 024 import org.apache.commons.lang.builder.ToStringBuilder; 025 import org.apache.commons.lang.builder.ToStringStyle; 026 import org.sonar.api.batch.analyzer.issue.AnalyzerIssue; 027 import org.sonar.api.batch.fs.InputFile; 028 import org.sonar.api.rule.RuleKey; 029 030 import javax.annotation.Nullable; 031 032 import java.io.Serializable; 033 import java.util.UUID; 034 035 public class DefaultAnalyzerIssue implements AnalyzerIssue, Serializable { 036 037 private final String key; 038 private final InputFile inputFile; 039 private final RuleKey ruleKey; 040 private final String message; 041 private final Integer line; 042 private final Double effortToFix; 043 044 DefaultAnalyzerIssue(DefaultAnalyzerIssueBuilder builder) { 045 Preconditions.checkNotNull(builder.ruleKey, "ruleKey is mandatory on issue"); 046 this.inputFile = builder.file; 047 this.ruleKey = builder.ruleKey; 048 this.message = builder.message; 049 this.line = builder.line; 050 this.effortToFix = builder.effortToFix; 051 this.key = builder.key == null ? UUID.randomUUID().toString() : builder.key; 052 Preconditions.checkState(!Strings.isNullOrEmpty(key), "Fail to generate issue key"); 053 } 054 055 public String key() { 056 return key; 057 } 058 059 @Override 060 @Nullable 061 public InputFile inputFile() { 062 return inputFile; 063 } 064 065 @Override 066 public RuleKey ruleKey() { 067 return ruleKey; 068 } 069 070 @Override 071 public String message() { 072 return message; 073 } 074 075 @Override 076 public Integer line() { 077 return line; 078 } 079 080 @Override 081 @Nullable 082 public Double effortToFix() { 083 return effortToFix; 084 } 085 086 @Override 087 public boolean equals(Object o) { 088 if (this == o) { 089 return true; 090 } 091 if (o == null || getClass() != o.getClass()) { 092 return false; 093 } 094 DefaultAnalyzerIssue that = (DefaultAnalyzerIssue) o; 095 return !key.equals(that.key); 096 } 097 098 @Override 099 public int hashCode() { 100 return key.hashCode(); 101 } 102 103 @Override 104 public String toString() { 105 return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); 106 } 107 108 }