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