001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 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.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  public String getKey() {
060    return key;
061  }
062
063  /**
064   * {@inheritDoc}
065   */
066  public String getName() {
067    return name;
068  }
069
070  /**
071   * Sets the language name
072   */
073  public void setName(String name) {
074    this.name = name;
075  }
076
077  @Override
078  public boolean equals(Object o) {
079    if (this == o) {
080      return true;
081    }
082    if (!(o instanceof Language)) {
083      return false;
084    }
085
086    Language language = (Language) o;
087    return !(key != null ? !key.equals(language.getKey()) : language.getKey() != null);
088
089  }
090
091  @Override
092  public int hashCode() {
093    return key != null ? key.hashCode() : 0;
094  }
095
096  @Override
097  public String toString() {
098    return name;
099  }
100}