001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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 org.apache.commons.lang.StringUtils;
023    import org.apache.commons.lang.builder.ToStringBuilder;
024    
025    /**
026     * @since 1.10
027     */
028    public abstract class AbstractResource<PARENT extends Resource> implements Resource {
029    
030      private String key;
031      private String name;
032      private String description;
033      private String scope;
034      private String qualifier;
035      private Language language;
036    
037      protected AbstractResource() {
038      }
039    
040      protected AbstractResource(String scope, String qualifier) {
041        this.scope = scope;
042        this.qualifier = qualifier;
043      }
044    
045      protected AbstractResource(String key, String scope, String qualifier) {
046        this.key = key;
047        this.scope = scope;
048        this.qualifier = qualifier;
049      }
050    
051      public String getKey() {
052        return key;
053      }
054    
055      public Resource<? extends PARENT> setKey(String key) {
056        this.key = StringUtils.trim(key);
057        return this;
058      }
059    
060      public String getName() {
061        return name;
062      }
063    
064      public Resource<? extends PARENT> setName(String name) {
065        this.name = name;
066        return this;
067      }
068    
069      public String getDescription() {
070        return description;
071      }
072    
073      public Resource<? extends PARENT> setDescription(String description) {
074        this.description = description;
075        return this;
076      }
077    
078      public String getScope() {
079        return scope;
080      }
081    
082      public void setQualifier(String qualifier) {
083        this.qualifier = qualifier;
084      }
085    
086      public String getQualifier() {
087        return qualifier;
088      }
089    
090      public Language getLanguage() {
091        return language;
092      }
093    
094      public Resource<? extends PARENT> setLanguage(Language language) {
095        this.language = language;
096        return this;
097      }
098    
099      public PARENT getParent() {
100        return null;
101      }
102    
103      @Override
104      public boolean equals(Object obj) {
105        if (!(obj instanceof Resource)) {
106          return false;
107        }
108        if (this == obj) {
109          return true;
110        }
111        Resource other = (Resource) obj;
112        return StringUtils.equals(key, other.getKey());
113      }
114    
115      @Override
116      public int hashCode() {
117        return StringUtils.defaultString(key).hashCode();
118      }
119    
120      @Override
121      public String toString() {
122        return new ToStringBuilder(this)
123            .append("key", key)
124            .append("scope", scope)
125            .append("qualifier", qualifier)
126            .append("name", name)
127            .append("language", language)
128            .append("description", description)
129            .toString();
130      }
131    }