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 shortName;
033 private String description;
034 private String scope;
035 private String qualifier;
036 private Language language;
037
038 protected AbstractResource() {
039 }
040
041 protected AbstractResource(String scope, String qualifier) {
042 this.scope = scope;
043 this.qualifier = qualifier;
044 }
045
046 protected AbstractResource(String key, String scope, String qualifier) {
047 this.key = key;
048 this.scope = scope;
049 this.qualifier = qualifier;
050 }
051
052 /**
053 * {@inheritDoc}
054 */
055 public String getKey() {
056 return key;
057 }
058
059 public Resource<? extends PARENT> setKey(String key) {
060 this.key = StringUtils.trim(key);
061 return this;
062 }
063
064 /**
065 * {@inheritDoc}
066 */
067 public String getName() {
068 return name;
069 }
070
071 public Resource<? extends PARENT> setName(String name) {
072 this.name = name;
073 return this;
074 }
075
076 /**
077 * {@inheritDoc}
078 */
079 public String getDescription() {
080 return description;
081 }
082
083 public Resource<? extends PARENT> setDescription(String description) {
084 this.description = description;
085 return this;
086 }
087
088 /**
089 * {@inheritDoc}
090 */
091 public String getScope() {
092 return scope;
093 }
094
095 public void setQualifier(String qualifier) {
096 this.qualifier = qualifier;
097 }
098
099 /**
100 * {@inheritDoc}
101 */
102 public String getQualifier() {
103 return qualifier;
104 }
105
106 /**
107 * {@inheritDoc}
108 */
109 public Language getLanguage() {
110 return language;
111 }
112
113 public Resource<? extends PARENT> setLanguage(Language language) {
114 this.language = language;
115 return this;
116 }
117
118 /**
119 * {@inheritDoc}
120 */
121 public PARENT getParent() {
122 return null;
123 }
124
125 @Override
126 public boolean equals(Object obj) {
127 if (!(obj instanceof Resource)) {
128 return false;
129 }
130 if (this == obj) {
131 return true;
132 }
133 Resource other = (Resource) obj;
134 return StringUtils.equals(key, other.getKey());
135 }
136
137 @Override
138 public int hashCode() {
139 return StringUtils.defaultString(key).hashCode();
140 }
141
142 @Override
143 public String toString() {
144 return new ToStringBuilder(this)
145 .append("key", key)
146 .append("scope", scope)
147 .append("qualifier", qualifier)
148 .append("name", name)
149 .append("language", language)
150 .append("description", description)
151 .toString();
152 }
153 }