001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar 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     * Sonar 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
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
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 void setRule(Rule rule) {
050        this.rule = rule;
051      }
052    
053      public RulePriority getRulePriority() {
054        return rulePriority;
055      }
056    
057      public void setRulePriority(RulePriority rulePriority) {
058        this.rulePriority = rulePriority;
059      }
060    
061    /**
062       * @deprecated since 2.5 See http://jira.codehaus.org/browse/SONAR-2007
063       */
064      @Deprecated
065      public Integer getRuleCategory() {
066        return null;
067      }
068    
069      /**
070       * @deprecated since 2.5 See http://jira.codehaus.org/browse/SONAR-2007
071       */
072      @Deprecated
073      public void setRuleCategory(Integer ruleCategory) {
074    
075      }
076    
077      @Override
078      public boolean equals(Object obj) {
079        if (!(obj.getClass().equals(RuleMeasure.class))) {
080          return false;
081        }
082        if (this == obj) {
083          return true;
084        }
085        RuleMeasure other = (RuleMeasure) obj;
086        return new EqualsBuilder()
087            .append(getMetric(), other.getMetric())
088            .append(rule, other.rule)
089            .append(rulePriority, other.rulePriority)
090            .isEquals();
091      }
092    
093      @Override
094      public RuleMeasure setValue(Double v) {
095        return (RuleMeasure) super.setValue(v);
096      }
097    
098      @Override
099      public int hashCode() {
100        return new HashCodeBuilder(17, 37).
101            append(getMetric()).
102            append(rule).
103            append(rulePriority).
104            toHashCode();
105      }
106    
107      @Override
108      public String toString() {
109        return new ToStringBuilder(this).
110            append("id", getId()).
111            append("metric", metric).
112            append("value", value).
113            append("data", data).
114            append("description", description).
115            append("alertStatus", alertStatus).
116            append("alertText", alertText).
117            append("tendency", tendency).
118            append("rule", rule).
119            append("priority", rulePriority).
120            toString();
121      }
122    
123      public static RuleMeasure createForRule(Metric metric, Rule rule, Double value) {
124        return (RuleMeasure) new RuleMeasure(metric, rule, null, null).setValue(value);
125      }
126    
127      public static RuleMeasure createForPriority(Metric metric, RulePriority priority, Double value) {
128        return (RuleMeasure) new RuleMeasure(metric, null, priority, null).setValue(value);
129      }
130    
131      /**
132       * @deprecated since 2.5 See http://jira.codehaus.org/browse/SONAR-2007
133       */
134      @Deprecated
135      public static RuleMeasure createForCategory(Metric metric, Integer category, Double value) {
136        return (RuleMeasure) new RuleMeasure(metric, null, null, category).setValue(value);
137      }
138    }