001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube 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     * SonarQube 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     */
020    package org.sonar.api.rule;
021    
022    import com.google.common.base.Preconditions;
023    import com.google.common.base.Strings;
024    
025    import java.io.Serializable;
026    
027    /**
028     * Key of a rule. Unique among all the rule repositories.
029     *
030     * @since 3.6
031     */
032    public class RuleKey implements Serializable {
033    
034      public static final String MANUAL_REPOSITORY_KEY = "manual";
035      private final String repository, rule;
036    
037      protected RuleKey(String repositoryKey, String ruleKey) {
038        this.repository = repositoryKey;
039        this.rule = ruleKey;
040      }
041    
042      /**
043       * Create a key. Parameters are NOT null.
044       */
045      public static RuleKey of(String repository, String rule) {
046        Preconditions.checkArgument(!Strings.isNullOrEmpty(repository), "Repository must be set");
047        Preconditions.checkArgument(!Strings.isNullOrEmpty(rule), "Rule must be set");
048        return new RuleKey(repository, rule);
049      }
050    
051      /**
052       * Create a key from a string representation (see {@link #toString()}. An {@link IllegalArgumentException} is raised
053       * if the format is not valid.
054       */
055      public static RuleKey parse(String s) {
056        int semiColonPos = s.indexOf(":");
057        Preconditions.checkArgument(semiColonPos > 0, "Invalid rule key: " + s);
058        String key = s.substring(0, semiColonPos);
059        String repo = s.substring(semiColonPos + 1);
060        return RuleKey.of(key, repo);
061      }
062    
063      /**
064       * Never null
065       */
066      public String repository() {
067        return repository;
068      }
069    
070      /**
071       * Never null
072       */
073      public String rule() {
074        return rule;
075      }
076    
077      public boolean isManual() {
078        return MANUAL_REPOSITORY_KEY.equals(repository);
079      }
080    
081      @Override
082      public boolean equals(Object o) {
083        if (this == o) {
084          return true;
085        }
086        if (o == null || getClass() != o.getClass()) {
087          return false;
088        }
089        RuleKey ruleKey = (RuleKey) o;
090        if (!repository.equals(ruleKey.repository)) {
091          return false;
092        }
093        if (!rule.equals(ruleKey.rule)) {
094          return false;
095        }
096        return true;
097      }
098    
099      @Override
100      public int hashCode() {
101        int result = repository.hashCode();
102        result = 31 * result + rule.hashCode();
103        return result;
104      }
105    
106      /**
107       * Format is "repository:rule", for example "squid:AvoidCycle"
108       */
109      @Override
110      public String toString() {
111        return String.format("%s:%s", repository, rule);
112      }
113    }