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.rule;
021
022import com.google.common.base.Preconditions;
023import java.io.Serializable;
024import javax.annotation.Nullable;
025import javax.annotation.concurrent.Immutable;
026
027import static org.apache.commons.lang.StringUtils.isEmpty;
028
029/**
030 * Key of a rule. Unique among all the rule repositories.
031 *
032 * @since 3.6
033 */
034@Immutable
035public class RuleKey implements Serializable, Comparable<RuleKey> {
036
037  /**
038   * @deprecated since 5.5, manual rule feature has been dropped
039   */
040  @Deprecated
041  public static final String MANUAL_REPOSITORY_KEY = "manual";
042
043  private final String repository;
044  private final String rule;
045
046  protected RuleKey(String repositoryKey, String ruleKey) {
047    this.repository = repositoryKey;
048    this.rule = ruleKey;
049  }
050
051  /**
052   * Create a key. Parameters are NOT null.
053   */
054  public static RuleKey of(String repository, String rule) {
055    Preconditions.checkArgument(!isEmpty(repository), "Repository must be set");
056    Preconditions.checkArgument(!isEmpty(rule), "Rule must be set");
057    return new RuleKey(repository, rule);
058  }
059
060  /**
061   * Create a key from a string representation (see {@link #toString()}. An {@link IllegalArgumentException} is raised
062   * if the format is not valid.
063   */
064  public static RuleKey parse(String s) {
065    int semiColonPos = s.indexOf(':');
066    Preconditions.checkArgument(semiColonPos > 0, "Invalid rule key: " + s);
067    String key = s.substring(0, semiColonPos);
068    String repo = s.substring(semiColonPos + 1);
069    return RuleKey.of(key, repo);
070  }
071
072  /**
073   * Never null
074   */
075  public String repository() {
076    return repository;
077  }
078
079  /**
080   * Never null
081   */
082  public String rule() {
083    return rule;
084  }
085
086  /**
087   * @deprecated since 5.5, manual rule feature has been dropped
088   */
089  @Deprecated
090  public boolean isManual() {
091    return false;
092  }
093
094  @Override
095  public boolean equals(@Nullable Object o) {
096    if (this == o) {
097      return true;
098    }
099    if (o == null || getClass() != o.getClass()) {
100      return false;
101    }
102    RuleKey ruleKey = (RuleKey) o;
103    return repository.equals(ruleKey.repository) && rule.equals(ruleKey.rule);
104  }
105
106  @Override
107  public int hashCode() {
108    int result = repository.hashCode();
109    result = 31 * result + rule.hashCode();
110    return result;
111  }
112
113  /**
114   * Format is "repository:rule", for example "squid:AvoidCycle"
115   */
116  @Override
117  public String toString() {
118    return String.format("%s:%s", repository, rule);
119  }
120
121  @Override
122  public int compareTo(RuleKey o) {
123    int compareRepositories = this.repository.compareTo(o.repository);
124    if (compareRepositories == 0) {
125      return this.rule.compareTo(o.rule);
126    }
127    return compareRepositories;
128  }
129}