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
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   * @return whether a resource is a package
062   */
063  public static boolean isPackage(Resource resource) {
064    return resource != null && Qualifiers.PACKAGE.equals(resource.getQualifier());
065  }
066
067  /**
068   * @return whether a resource is a set
069   */
070  public static boolean isSet(Resource resource) {
071    return resource != null && Scopes.PROJECT.equals(resource.getScope());
072  }
073
074  /**
075   * @return whether a resource is a space
076   */
077  public static boolean isSpace(Resource resource) {
078    return resource != null && Scopes.DIRECTORY.equals(resource.getScope());
079  }
080
081  /**
082   * @return whether a resource is an entity.
083   */
084  public static boolean isEntity(Resource resource) {
085    return resource != null && Scopes.FILE.equals(resource.getScope());
086  }
087
088  /**
089   * This method equal isRootProject(resource) or isModuleProject(resource)
090   */
091  public static boolean isProject(Resource resource) {
092    return isSet(resource);
093  }
094
095  /**
096   * Alias for {@link #isSpace(Resource)}
097   */
098  public static boolean isDirectory(Resource resource) {
099    return isSpace(resource);
100  }
101
102  /**
103   * Alias for {@link #isEntity(Resource)}
104   */
105  public static boolean isFile(Resource resource) {
106    return isEntity(resource);
107  }
108
109  /* QUALIFIERS */
110
111  /**
112   * @return whether a resource is a class
113   */
114  public static boolean isClass(Resource resource) {
115    return Qualifiers.CLASS.equals(resource.getQualifier());
116  }
117
118  /**
119   * @return whether a resource is a unit test class
120   */
121  public static boolean isUnitTestClass(Resource resource) {
122    return Qualifiers.UNIT_TEST_FILE.equals(resource.getQualifier());
123  }
124
125  /**
126   * @return whether a resource is a library
127   */
128  public static boolean isLibrary(Resource resource) {
129    return Qualifiers.LIBRARY.equals(resource.getQualifier());
130  }
131
132  /**
133   * @param resource not nullable
134   * @return true if this type of resource is persisted in database
135   * @since 2.6
136   */
137  public static boolean isPersistable(Resource resource) {
138    return StringUtils.equals(Scopes.PROJECT, resource.getScope()) || StringUtils.equals(Scopes.DIRECTORY, resource.getScope()) ||
139        StringUtils.equals(Scopes.FILE, resource.getScope());
140  }
141}