001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program 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 * This program 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.rules;
021
022import org.apache.commons.lang.StringUtils;
023import org.apache.commons.lang.builder.ToStringBuilder;
024
025public class RuleParam {
026
027  private Integer id;
028  private Rule rule;
029  private String key;
030  private String description;
031  private String type = "STRING";
032  private String defaultValue;
033
034  /**
035   * @deprecated since 2.3 use the factory method Rule.setParameter()
036   */
037  @Deprecated
038  public RuleParam() {
039  }
040
041  /**
042   * @deprecated since 2.3 use the factory method setParameter()
043   */
044  @Deprecated
045  public RuleParam(Rule rule, String key, String description, String type) {
046    this.rule = rule;
047    this.key = key;
048    this.description = description;
049    this.type = type;
050  }
051
052  public Integer getId() {
053    return id;
054  }
055
056  public Rule getRule() {
057    return rule;
058  }
059
060  RuleParam setRule(Rule rule) {
061    this.rule = rule;
062    return this;
063  }
064
065  public String getKey() {
066    return key;
067  }
068
069  public RuleParam setKey(String key) {
070    this.key = key;
071    return this;
072  }
073
074  public String getDescription() {
075    return description;
076  }
077
078  public RuleParam setDescription(String s) {
079    this.description = StringUtils.defaultString(s, "");
080    return this;
081  }
082
083  public String getType() {
084    return type;
085  }
086
087  public RuleParam setType(String type) {
088    this.type = type;
089    return this;
090  }
091
092  public String getDefaultValue() {
093    return defaultValue;
094  }
095
096  public Boolean getDefaultValueAsBoolean() {
097    if (defaultValue != null) {
098      return Boolean.parseBoolean(defaultValue);
099    }
100    return null;
101  }
102
103  public Integer getDefaultValueAsInteger() {
104    if (defaultValue != null) {
105      return Integer.parseInt(defaultValue);
106    }
107    return null;
108  }
109
110  public RuleParam setDefaultValue(String s) {
111    this.defaultValue = s;
112    return this;
113  }
114
115  @Override
116  public boolean equals(Object obj) {
117    if (!(obj instanceof RuleParam)) {
118      return false;
119    }
120    if (this == obj) {
121      return true;
122    }
123    RuleParam other = (RuleParam) obj;
124    return other.key.equals(key);
125  }
126
127  @Override
128  public int hashCode() {
129    return key.hashCode();
130  }
131
132  @Override
133  public String toString() {
134    return new ToStringBuilder(this)
135      .append("id", id)
136      .append("key", key)
137      .append("desc", description)
138      .append("type", type)
139      .toString();
140  }
141}