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.resources; 021 022import com.google.common.base.Preconditions; 023 024import java.util.Locale; 025 026/** 027 * Inherit this class to define a new language like PLSQL, PHP or C# 028 * 029 * @since 1.10 030 */ 031public abstract class AbstractLanguage implements Language { 032 private final String key; 033 private String name; 034 035 /** 036 * Better to use AbstractLanguage(key, name). In this case, key and name will be the same 037 * 038 * @param key The key of the language. Must not be more than 20 chars. 039 */ 040 public AbstractLanguage(String key) { 041 this(key, key); 042 } 043 044 /** 045 * Should be the constructor used to build an AbstractLanguage. 046 * 047 * @param key the key that will be used to retrieve the language. Must not be more than 20 chars. This key is important as it will be used to teint rules repositories... 048 * @param name the display name of the language in the interface 049 */ 050 public AbstractLanguage(String key, String name) { 051 Preconditions.checkArgument(key.length() <= 20, "The following language key exceeds 20 characters: '" + key + "'"); 052 this.key = key.toLowerCase(Locale.ENGLISH); 053 this.name = name; 054 } 055 056 /** 057 * {@inheritDoc} 058 */ 059 @Override 060 public String getKey() { 061 return key; 062 } 063 064 /** 065 * {@inheritDoc} 066 */ 067 @Override 068 public String getName() { 069 return name; 070 } 071 072 /** 073 * Sets the language name 074 */ 075 public void setName(String name) { 076 this.name = name; 077 } 078 079 @Override 080 public boolean equals(Object o) { 081 if (this == o) { 082 return true; 083 } 084 if (o == null || !(o instanceof Language)) { 085 return false; 086 } 087 Language that = (Language) o; 088 return key.equals(that.getKey()); 089 090 } 091 092 @Override 093 public int hashCode() { 094 return key.hashCode(); 095 } 096 097 @Override 098 public String toString() { 099 return name; 100 } 101}