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        String[] split = s.split(":");
057        Preconditions.checkArgument(split.length == 2, "Invalid rule key: " + s);
058        return RuleKey.of(split[0], split[1]);
059      }
060    
061      /**
062       * Never null
063       */
064      public String repository() {
065        return repository;
066      }
067    
068      /**
069       * Never null
070       */
071      public String rule() {
072        return rule;
073      }
074    
075      public boolean isManual() {
076        return MANUAL_REPOSITORY_KEY.equals(repository);
077      }
078    
079      @Override
080      public boolean equals(Object o) {
081        if (this == o) {
082          return true;
083        }
084        if (o == null || getClass() != o.getClass()) {
085          return false;
086        }
087        RuleKey ruleKey = (RuleKey) o;
088        if (!repository.equals(ruleKey.repository)) {
089          return false;
090        }
091        if (!rule.equals(ruleKey.rule)) {
092          return false;
093        }
094        return true;
095      }
096    
097      @Override
098      public int hashCode() {
099        int result = repository.hashCode();
100        result = 31 * result + rule.hashCode();
101        return result;
102      }
103    
104      /**
105       * Format is "repository:rule", for example "squid:AvoidCycle"
106       */
107      @Override
108      public String toString() {
109        return String.format("%s:%s", repository, rule);
110      }
111    }