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      private final String repository, rule;
034    
035      private RuleKey(String repository, String rule) {
036        this.repository = repository;
037        this.rule = rule;
038      }
039    
040      /**
041       * Create a key. Parameters are NOT null.
042       */
043      public static RuleKey of(String repository, String rule) {
044        Preconditions.checkArgument(!Strings.isNullOrEmpty(repository), "Repository must be set");
045        Preconditions.checkArgument(!Strings.isNullOrEmpty(rule), "Rule must be set");
046        return new RuleKey(repository, rule);
047      }
048    
049      /**
050       * Create a key from a string representation (see {@link #toString()}. An {@link IllegalArgumentException} is raised
051       * if the format is not valid.
052       */
053      public static RuleKey parse(String s) {
054        String[] split = s.split(":");
055        Preconditions.checkArgument(split.length == 2, "Bad format of rule key: " + s);
056        return RuleKey.of(split[0], split[1]);
057      }
058    
059      /**
060       * Never null
061       */
062      public String repository() {
063        return repository;
064      }
065    
066      /**
067       * Never null
068       */
069      public String rule() {
070        return rule;
071      }
072    
073      @Override
074      public boolean equals(Object o) {
075        if (this == o) {
076          return true;
077        }
078        if (o == null || getClass() != o.getClass()) {
079          return false;
080        }
081        RuleKey ruleKey = (RuleKey) o;
082        if (!repository.equals(ruleKey.repository)) {
083          return false;
084        }
085        if (!rule.equals(ruleKey.rule)) {
086          return false;
087        }
088        return true;
089      }
090    
091      @Override
092      public int hashCode() {
093        int result = repository.hashCode();
094        result = 31 * result + rule.hashCode();
095        return result;
096      }
097    
098      /**
099       * Format is "repository:rule", for example "squid:AvoidCycle"
100       */
101      @Override
102      public String toString() {
103        return String.format("%s:%s", repository, rule);
104      }
105    }