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(org.sonar.api.rule.RuleKey)}
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  @Override
116  public boolean equals(Object obj) {
117    if (obj == null) {
118      return false;
119    }
120    if (!(obj.getClass().equals(RuleMeasure.class))) {
121      // for the moment.
122      return false;
123    }
124    if (this == obj) {
125      return true;
126    }
127    RuleMeasure other = (RuleMeasure) obj;
128    return new EqualsBuilder()
129      .append(getMetric(), other.getMetric())
130      .append(personId, other.personId)
131      .append(ruleKey, other.ruleKey)
132      .isEquals();
133  }
134
135  @Override
136  public RuleMeasure setValue(@Nullable Double v) {
137    return (RuleMeasure) super.setValue(v);
138  }
139
140  @Override
141  public int hashCode() {
142    return new HashCodeBuilder(17, 37)
143      .append(getMetric())
144      .append(personId)
145      .append(ruleKey)
146      .toHashCode();
147  }
148
149  @Override
150  public String toString() {
151    return new ToStringBuilder(this)
152      .append("metric", metric)
153      .append("personId", personId)
154      .append("ruleKey", ruleKey)
155      .append("value", value)
156      .append("data", data)
157      .append("description", description)
158      .append("alertStatus", alertStatus)
159      .append("alertText", alertText)
160      .append("tendency", tendency)
161      .append("severity", rulePriority)
162      .toString();
163  }
164
165  /**
166   * @deprecated since 4.4 use {@link #createForRule(Metric, RuleKey, Double)}
167   */
168  @Deprecated
169  public static RuleMeasure createForRule(Metric metric, Rule rule, @Nullable Double value) {
170    return new RuleMeasure(metric, rule, null, null).setValue(value);
171  }
172
173  public static RuleMeasure createForRule(Metric metric, RuleKey ruleKey, @Nullable Double value) {
174    return new RuleMeasure(metric, ruleKey, null, null).setValue(value);
175  }
176
177  public static RuleMeasure createForPriority(Metric metric, RulePriority priority, @Nullable Double value) {
178    return new RuleMeasure(metric, (RuleKey) null, priority, null).setValue(value);
179  }
180
181  /**
182   * @deprecated since 2.5. See SONAR-2007.
183   */
184  @Deprecated
185  public static RuleMeasure createForCategory(Metric metric, Integer category, @Nullable Double value) {
186    return new RuleMeasure(metric, (RuleKey) null, null, category).setValue(value);
187  }
188}