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