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