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 */ 020package org.sonar.api.rule; 021 022import com.google.common.base.Preconditions; 023import com.google.common.base.Strings; 024 025import java.io.Serializable; 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 { 035 036 public static final String MANUAL_REPOSITORY_KEY = "manual"; 037 private final String repository; 038 private final String rule; 039 040 protected RuleKey(String repositoryKey, String ruleKey) { 041 this.repository = repositoryKey; 042 this.rule = ruleKey; 043 } 044 045 /** 046 * Create a key. Parameters are NOT null. 047 */ 048 public static RuleKey of(String repository, String rule) { 049 Preconditions.checkArgument(!Strings.isNullOrEmpty(repository), "Repository must be set"); 050 Preconditions.checkArgument(!Strings.isNullOrEmpty(rule), "Rule must be set"); 051 return new RuleKey(repository, rule); 052 } 053 054 /** 055 * Create a key from a string representation (see {@link #toString()}. An {@link IllegalArgumentException} is raised 056 * if the format is not valid. 057 */ 058 public static RuleKey parse(String s) { 059 int semiColonPos = s.indexOf(":"); 060 Preconditions.checkArgument(semiColonPos > 0, "Invalid rule key: " + s); 061 String key = s.substring(0, semiColonPos); 062 String repo = s.substring(semiColonPos + 1); 063 return RuleKey.of(key, repo); 064 } 065 066 /** 067 * Never null 068 */ 069 public String repository() { 070 return repository; 071 } 072 073 /** 074 * Never null 075 */ 076 public String rule() { 077 return rule; 078 } 079 080 public boolean isManual() { 081 return MANUAL_REPOSITORY_KEY.equals(repository); 082 } 083 084 @Override 085 public boolean equals(Object o) { 086 if (this == o) { 087 return true; 088 } 089 if (o == null || getClass() != o.getClass()) { 090 return false; 091 } 092 RuleKey ruleKey = (RuleKey) o; 093 if (!repository.equals(ruleKey.repository)) { 094 return false; 095 } 096 return rule.equals(ruleKey.rule); 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}