001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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      private Integer ruleCategory;
036    
037      /**
038       * This constructor is for internal use only. Please use static methods createForXXX().
039       */
040      public RuleMeasure(Metric metric, Rule rule, RulePriority rulePriority, Integer ruleCategory) {
041        super(metric);
042        this.rule = rule;
043        this.rulePriority = rulePriority;
044        this.ruleCategory = ruleCategory;
045      }
046    
047      public Rule getRule() {
048        return rule;
049      }
050    
051      public void setRule(Rule rule) {
052        this.rule = rule;
053      }
054    
055      public RulePriority getRulePriority() {
056        return rulePriority;
057      }
058    
059      public void setRulePriority(RulePriority rulePriority) {
060        this.rulePriority = rulePriority;
061      }
062    
063      public Integer getRuleCategory() {
064        return ruleCategory;
065      }
066    
067      public void setRuleCategory(Integer ruleCategory) {
068        this.ruleCategory = ruleCategory;
069      }
070    
071      @Override
072      public boolean equals(Object obj) {
073        if (!(obj.getClass().equals(RuleMeasure.class))) {
074          return false;
075        }
076        if (this == obj) {
077          return true;
078        }
079        RuleMeasure other = (RuleMeasure) obj;
080        return new EqualsBuilder()
081            .append(getMetric(), other.getMetric())
082            .append(rule, other.rule)
083            .append(rulePriority, other.rulePriority)
084            .append(ruleCategory, other.ruleCategory)
085            .isEquals();
086      }
087    
088      @Override
089      public RuleMeasure setValue(Double v) {
090        return (RuleMeasure) super.setValue(v);
091      }
092    
093      @Override
094      public int hashCode() {
095        return new HashCodeBuilder(17, 37).
096            append(getMetric()).
097            append(rule).
098            append(rulePriority).
099            append(ruleCategory).
100            toHashCode();
101    
102      }
103    
104      @Override
105      public String toString() {
106        return new ToStringBuilder(this).
107            append("id", getId()).
108            append("metric", metric).
109            append("value", value).
110            append("data", data).
111            append("description", description).
112            append("alertStatus", alertStatus).
113            append("alertText", alertText).
114            append("tendency", tendency).
115            append("rule", rule).
116            append("category", ruleCategory).
117            append("priority", rulePriority).
118            toString();
119      }
120    
121      public static RuleMeasure createForRule(Metric metric, Rule rule, Double value) {
122        return (RuleMeasure) new RuleMeasure(metric, rule, null, null).setValue(value);
123      }
124    
125      public static RuleMeasure createForPriority(Metric metric, RulePriority priority, Double value) {
126        return (RuleMeasure) new RuleMeasure(metric, null, priority, null).setValue(value);
127      }
128    
129      public static RuleMeasure createForCategory(Metric metric, Integer category, Double value) {
130        return (RuleMeasure) new RuleMeasure(metric, null, null, category).setValue(value);
131      }
132    }