001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar 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     * Sonar 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
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.api.resources;
021    
022    import java.util.Locale;
023    
024    /**
025     * Inherit this class to define a new language like PLSQL, PHP or C#
026     *
027     * @since 1.10
028     */
029    public abstract class AbstractLanguage implements Language {
030      private final String key;
031      private String name;
032    
033      /**
034       * Better to use AbstractLanguage(key, name). In this case, key and name will be the same
035       */
036      public AbstractLanguage(String key) {
037        this(key, key);
038      }
039    
040      /**
041       * Should be the constructor used to build an AbstractLanguage.
042       *
043       * @param key the key that will be used to retrieve the language. This key is important as it will be used to teint rules repositories...
044       * @param name the display name of the language in the interface
045       */
046      public AbstractLanguage(String key, String name) {
047        this.key = key.toLowerCase(Locale.ENGLISH);
048        this.name = name;
049      }
050    
051      /**
052       * {@inheritDoc}
053       */
054      public String getKey() {
055        return key;
056      }
057    
058      /**
059       * {@inheritDoc}
060       */
061      public String getName() {
062        return name;
063      }
064    
065      /**
066       * Sets the language name
067       */
068      public void setName(String name) {
069        this.name = name;
070      }
071    
072      @Override
073      public boolean equals(Object o) {
074        if (this == o) {
075          return true;
076        }
077        if (o == null || getClass() != o.getClass()) {
078          return false;
079        }
080    
081        Language language = (Language) o;
082        return !(key != null ? !key.equals(language.getKey()) : language.getKey() != null);
083    
084      }
085    
086      @Override
087      public int hashCode() {
088        return (key != null ? key.hashCode() : 0);
089      }
090    
091      @Override
092      public String toString() {
093        return name;
094      }
095    }