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.measures;
021
022import org.apache.commons.lang.builder.EqualsBuilder;
023import org.apache.commons.lang.builder.HashCodeBuilder;
024import org.apache.commons.lang.builder.ToStringBuilder;
025import org.sonar.api.rule.RuleKey;
026import org.sonar.api.rules.Rule;
027import org.sonar.api.rules.RulePriority;
028
029import javax.annotation.Nullable;
030
031/**
032 * @since 1.10
033 */
034public class RuleMeasure extends Measure {
035
036  private RuleKey ruleKey;
037  private RulePriority rulePriority;
038
039  /**
040   * This constructor is for internal use only. Please use static methods createForXXX().
041   * @deprecated since 4.4 use {@link #RuleMeasure(Metric, RuleKey, RulePriority, Integer)}
042   */
043  @Deprecated
044  public RuleMeasure(Metric metric, @Nullable Rule rule, @Nullable RulePriority rulePriority, @Nullable Integer ruleCategory) {
045    this(metric, rule != null ? rule.ruleKey() : null, rulePriority, ruleCategory);
046  }
047
048  /**
049   * This constructor is for internal use only. Please use static methods createForXXX().
050   */
051  public RuleMeasure(Metric metric, @Nullable RuleKey ruleKey, @Nullable RulePriority rulePriority, @Nullable Integer ruleCategory) {
052    super(metric);
053    this.ruleKey = ruleKey;
054    this.rulePriority = rulePriority;
055  }
056
057  public RuleKey ruleKey() {
058    return ruleKey;
059  }
060
061  public RuleMeasure setRuleKey(RuleKey ruleKey) {
062    this.ruleKey = ruleKey;
063    return this;
064  }
065
066  /**
067   * @deprecated since 4.4 use {@link #ruleKey()}
068   */
069  @Deprecated
070  public Rule getRule() {
071    return Rule.create(ruleKey.repository(), ruleKey.rule());
072  }
073
074  /**
075   * @deprecated since 4.4 use {@link #setRuleKey()}
076   */
077  @Deprecated
078  public RuleMeasure setRule(Rule rule) {
079    this.ruleKey = rule.ruleKey();
080    return this;
081  }
082
083  /**
084   * @deprecated since 2.14 use {@link #getSeverity()} instead. See SONAR-1829.
085   */
086  @Deprecated
087  public RulePriority getRulePriority() {
088    return rulePriority;
089  }
090
091  /**
092   * @since 2.14
093   */
094  public RulePriority getSeverity() {
095    return rulePriority;
096  }
097
098  /**
099   * @deprecated since 2.14 use {@link #setSeverity(org.sonar.api.rules.RulePriority)} instead. See SONAR-1829.
100   */
101  @Deprecated
102  public RuleMeasure setRulePriority(RulePriority rulePriority) {
103    this.rulePriority = rulePriority;
104    return this;
105  }
106
107  /**
108   * @since 2.14
109   */
110  public RuleMeasure setSeverity(RulePriority severity) {
111    this.rulePriority = severity;
112    return this;
113  }
114
115  /**
116   * @deprecated since 2.5. See SONAR-2007.
117   */
118  @Deprecated
119  public Integer getRuleCategory() {
120    return null;
121  }
122
123  /**
124   * @deprecated since 2.5. See SONAR-2007.
125   */
126  @Deprecated
127  public void setRuleCategory(Integer ruleCategory) {
128    // nothing
129  }
130
131  @Override
132  public boolean equals(Object obj) {
133    if (obj == null) {
134      return false;
135    }
136    if (!(obj.getClass().equals(RuleMeasure.class))) {// NOSONAR should be refactored but kept in the current state
137      // for the moment.
138      return false;
139    }
140    if (this == obj) {
141      return true;
142    }
143    RuleMeasure other = (RuleMeasure) obj;
144    return new EqualsBuilder()
145      .append(getMetric(), other.getMetric())
146      .append(personId, other.personId)
147      .append(ruleKey, other.ruleKey)
148      .isEquals();
149  }
150
151  @Override
152  public RuleMeasure setValue(@Nullable Double v) {
153    return (RuleMeasure) super.setValue(v);
154  }
155
156  @Override
157  public int hashCode() {
158    return new HashCodeBuilder(17, 37)
159      .append(getMetric())
160      .append(personId)
161      .append(ruleKey)
162      .toHashCode();
163  }
164
165  @Override
166  public String toString() {
167    return new ToStringBuilder(this)
168      .append("metric", metric)
169      .append("personId", personId)
170      .append("ruleKey", ruleKey)
171      .append("value", value)
172      .append("data", data)
173      .append("description", description)
174      .append("alertStatus", alertStatus)
175      .append("alertText", alertText)
176      .append("tendency", tendency)
177      .append("severity", rulePriority)
178      .toString();
179  }
180
181  /**
182   * @deprecated since 4.4 use {@link #createForRule(Metric, RuleKey, Double)}
183   */
184  @Deprecated
185  public static RuleMeasure createForRule(Metric metric, Rule rule, @Nullable Double value) {
186    return new RuleMeasure(metric, rule, null, null).setValue(value);
187  }
188
189  public static RuleMeasure createForRule(Metric metric, RuleKey ruleKey, @Nullable Double value) {
190    return new RuleMeasure(metric, ruleKey, null, null).setValue(value);
191  }
192
193  public static RuleMeasure createForPriority(Metric metric, RulePriority priority, @Nullable Double value) {
194    return new RuleMeasure(metric, (RuleKey) null, priority, null).setValue(value);
195  }
196
197  /**
198   * @deprecated since 2.5. See SONAR-2007.
199   */
200  @Deprecated
201  public static RuleMeasure createForCategory(Metric metric, Integer category, @Nullable Double value) {
202    return new RuleMeasure(metric, (RuleKey) null, null, category).setValue(value);
203  }
204}