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.resources;
021    
022    import com.google.common.base.Preconditions;
023    
024    import 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     */
031    public 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 instanceof Language)) {
085          return false;
086        }
087    
088        Language language = (Language) o;
089        return !(key != null ? !key.equals(language.getKey()) : language.getKey() != null);
090    
091      }
092    
093      @Override
094      public int hashCode() {
095        return key != null ? key.hashCode() : 0;
096      }
097    
098      @Override
099      public String toString() {
100        return name;
101      }
102    }