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 */
020package org.sonar.api.resources;
021
022import org.apache.commons.lang.StringUtils;
023
024/**
025 * @since 1.10
026 */
027public final class ResourceUtils {
028
029  private ResourceUtils() {
030  }
031
032  /**
033   * @return whether the resource is a view
034   */
035  public static boolean isView(Resource resource) {
036    return isSet(resource) && Qualifiers.VIEW.equals(resource.getQualifier());
037  }
038
039  /**
040   * @return whether the resource is a subview (in the view tree)
041   */
042  public static boolean isSubview(Resource resource) {
043    return isSet(resource) && Qualifiers.SUBVIEW.equals(resource.getQualifier());
044  }
045
046  /**
047   * @return whether the resource is the root project
048   */
049  public static boolean isRootProject(Resource resource) {
050    return Qualifiers.PROJECT.equals(resource.getQualifier());
051  }
052
053  /**
054   * @return whether a resource is a maven module of project
055   */
056  public static boolean isModuleProject(Resource resource) {
057    return Qualifiers.MODULE.equals(resource.getQualifier());
058  }
059
060  /**
061   * @deprecated since 4.2 Package are now directory. Use {@link #isDirectory(Resource)}
062   */
063  @Deprecated
064  public static boolean isPackage(Resource resource) {
065    return resource != null && Qualifiers.PACKAGE.equals(resource.getQualifier());
066  }
067
068  /**
069   * @return whether a resource is a set
070   */
071  public static boolean isSet(Resource resource) {
072    return resource != null && Scopes.PROJECT.equals(resource.getScope());
073  }
074
075  /**
076   * @return whether a resource is a space
077   */
078  public static boolean isSpace(Resource resource) {
079    return resource != null && Scopes.DIRECTORY.equals(resource.getScope());
080  }
081
082  /**
083   * @return whether a resource is an entity.
084   */
085  public static boolean isEntity(Resource resource) {
086    return resource != null && Scopes.FILE.equals(resource.getScope());
087  }
088
089  /**
090   * This method equal isRootProject(resource) or isModuleProject(resource)
091   */
092  public static boolean isProject(Resource resource) {
093    return isSet(resource);
094  }
095
096  /**
097   * Alias for {@link #isSpace(Resource)}
098   */
099  public static boolean isDirectory(Resource resource) {
100    return isSpace(resource);
101  }
102
103  /**
104   * Alias for {@link #isEntity(Resource)}
105   */
106  public static boolean isFile(Resource resource) {
107    return isEntity(resource);
108  }
109
110  /* QUALIFIERS */
111
112  /**
113   * @return whether a resource is a class
114   * @deprecated since 4.2 CLA qualifier is deprecated
115   */
116  @Deprecated
117  public static boolean isClass(Resource resource) {
118    return Qualifiers.CLASS.equals(resource.getQualifier());
119  }
120
121  /**
122   * @return whether a resource is a unit test class
123   * @deprecated since 5.1 use {@link #isUnitTestFile(Resource)}
124   */
125  @Deprecated
126  public static boolean isUnitTestClass(Resource resource) {
127    return isUnitTestFile(resource);
128  }
129
130  /**
131   * @return whether a resource is a unit test class
132   */
133  public static boolean isUnitTestFile(Resource resource) {
134    return Qualifiers.UNIT_TEST_FILE.equals(resource.getQualifier());
135  }
136
137  /**
138   * @return whether a resource is a library
139   */
140  public static boolean isLibrary(Resource resource) {
141    return Qualifiers.LIBRARY.equals(resource.getQualifier());
142  }
143
144  /**
145   * @param resource not nullable
146   * @return true if this type of resource is persisted in database
147   * @since 2.6
148   */
149  public static boolean isPersistable(Resource resource) {
150    return StringUtils.equals(Scopes.PROJECT, resource.getScope()) || StringUtils.equals(Scopes.DIRECTORY, resource.getScope()) ||
151      StringUtils.equals(Scopes.FILE, resource.getScope());
152  }
153
154}