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 */
020package org.sonar.api.resources;
021
022/**
023 * The interface to implement to create a resource in Sonar
024 * 
025 * @since 1.10
026 */
027public abstract class Resource<P extends Resource> {
028
029  /**
030   * @deprecated since 2.6. Use Scopes.PROJECT.
031   */
032  @Deprecated
033  public static final String SCOPE_SET = Scopes.PROJECT;
034
035  /**
036   * @deprecated since 2.6. Use Scopes.DIRECTORY.
037   */
038  @Deprecated
039  public static final String SCOPE_SPACE = Scopes.DIRECTORY;
040
041  /**
042   * @deprecated since 2.6. Use Scopes.FILE.
043   */
044  @Deprecated
045  public static final String SCOPE_ENTITY = Scopes.FILE;
046
047  /**
048   * @deprecated since 2.6. Use Qualifiers.VIEW.
049   */
050  @Deprecated
051  public static final String QUALIFIER_VIEW = Qualifiers.VIEW;
052
053  /**
054   * @deprecated since 2.6. Use Qualifiers.SUBVIEW.
055   */
056  @Deprecated
057  public static final String QUALIFIER_SUBVIEW = Qualifiers.SUBVIEW;
058
059  /**
060   * @deprecated since 2.6. Use Qualifiers.LIBRARY.
061   */
062  @Deprecated
063  public static final String QUALIFIER_LIB = Qualifiers.LIBRARY;
064
065  /**
066   * @deprecated since 2.6. Use Qualifiers.PROJECT.
067   */
068  @Deprecated
069  public static final String QUALIFIER_PROJECT = Qualifiers.PROJECT;
070
071  /**
072   * @deprecated since 2.6. Use Qualifiers.MODULE.
073   */
074  @Deprecated
075  public static final String QUALIFIER_MODULE = Qualifiers.MODULE;
076
077  /**
078   * @deprecated since 2.6. Use Qualifiers.PACKAGE.
079   */
080  @Deprecated
081  public static final String QUALIFIER_PACKAGE = Qualifiers.PACKAGE;
082
083  /**
084   * @deprecated since 2.6. Use Qualifiers.DIRECTORY.
085   */
086  @Deprecated
087  public static final String QUALIFIER_DIRECTORY = Qualifiers.DIRECTORY;
088
089  /**
090   * @deprecated since 2.6. Use Qualifiers.FILE.
091   */
092  @Deprecated
093  public static final String QUALIFIER_FILE = Qualifiers.FILE;
094
095  /**
096   * @deprecated since 2.6. Use Qualifiers.CLASS.
097   */
098  @Deprecated
099  public static final String QUALIFIER_CLASS = Qualifiers.CLASS;
100
101  /**
102   * @deprecated since 2.6. Use Qualifiers.FIELD.
103   */
104  @Deprecated
105  public static final String QUALIFIER_FIELD = Qualifiers.FIELD;
106
107  /**
108   * @deprecated since 2.6. Use Qualifiers.METHOD.
109   */
110  @Deprecated
111  public static final String QUALIFIER_METHOD = Qualifiers.METHOD;
112
113  /**
114   * @deprecated since 2.6. Use Qualifiers.UNIT_TEST_FILE.
115   */
116  @Deprecated
117  public static final String QUALIFIER_UNIT_TEST_CLASS = Qualifiers.UNIT_TEST_FILE;
118
119  private Integer id = null;
120
121  private String key = null;
122
123  private String effectiveKey = null;
124
125  private boolean isExcluded = false;
126
127  /**
128   * @return the resource key
129   */
130  public final String getKey() {
131    return key;
132  }
133
134  protected void setKey(String s) {
135    this.key = s;
136  }
137
138  /**
139   * @return the resource name
140   */
141  public abstract String getName();
142
143  /**
144   * @return the resource long name
145   */
146  public abstract String getLongName();
147
148  /**
149   * @return the resource description
150   */
151  public abstract String getDescription();
152
153  /**
154   * @return the language
155   */
156  public abstract Language getLanguage();
157
158  /**
159   * @return the scope
160   */
161  public abstract String getScope();
162
163  /**
164   * The qualifier tells the type of the resource. For example, it can be a File, a Class, a Project, a Unit Test...
165   *
166   * @return the qualifier
167   *
168   * @see org.sonar.api.resources.Qualifiers for the list of qualifiers
169   * @see org.sonar.api.resources.ResourceUtils to find out if a resource if a class, a unit test,... from its qualifier
170   */
171  public abstract String getQualifier();
172
173  /**
174   * The parent is used to build the resources tree, for example for relations between classes, packages and projects.
175   * <p>
176   * Return null if the parent is the project.
177   * </p>
178   */
179  public abstract P getParent();
180
181  /**
182   * Check resource against an Ant pattern, like mypackag?/*Foo.java. It's used for example to match resource exclusions.
183   * 
184   * @param antPattern Ant-like pattern (with **, * and ?). It includes file suffixes.
185   * @return true if the resource matches the Ant pattern
186   */
187  public abstract boolean matchFilePattern(String antPattern);
188
189  public final Integer getId() {
190    return id;
191  }
192
193  /**
194   * Internal use only
195   */
196  public Resource setId(Integer id) {
197    this.id = id;
198    return this;
199  }
200
201  public final String getEffectiveKey() {
202    return effectiveKey;
203  }
204
205  /**
206   * Internal use only
207   */
208  public final Resource setEffectiveKey(String effectiveKey) {
209    this.effectiveKey = effectiveKey;
210    return this;
211  }
212
213  /**
214   * @deprecated since 2.6 should use SensorContext#isExcluded(resource). It will make inheritance of Resource easier.
215   */
216  @Deprecated
217  public final boolean isExcluded() {
218    return isExcluded;
219  }
220
221  /**
222   * Internal use only
223   * @deprecated since 2.6 should use SensorContext#isExcluded(resource). It will make inheritance of Resource easier.
224   */
225  @Deprecated
226  public final Resource setExcluded(boolean b) {
227    isExcluded = b;
228    return this;
229  }
230
231  @Override
232  public boolean equals(Object o) {
233    if (this == o) {
234      return true;
235    }
236    if (o == null || getClass() != o.getClass()) {
237      return false;
238    }
239
240    Resource resource = (Resource) o;
241    return key.equals(resource.key);
242
243  }
244
245  @Override
246  public int hashCode() {
247    return key.hashCode();
248  }
249}