001/*
002 * SonarQube
003 * Copyright (C) 2009-2018 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program 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 * This program 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 java.util.List;
023import java.util.Objects;
024
025import static java.util.Arrays.asList;
026import static java.util.Collections.unmodifiableList;
027
028/**
029 * The qualifier determines the exact type of a resource.
030 * Plugins can define their own qualifiers.
031 *
032 * @since 2.6
033 */
034public final class Qualifiers {
035
036  /**
037   * Root portfolios. Scope of portfolios is Scopes.PROJECT
038   */
039  public static final String VIEW = "VW";
040
041  /**
042   * Sub-portfolios, defined in root portfolios. Scope of sub-portfolios is Scopes.PROJECT
043   */
044  public static final String SUBVIEW = "SVW";
045
046  /**
047   * Application portfolios. Scope of application is Scopes.PROJECT
048   */
049  public static final String APP = "APP";
050
051  /**
052   * Library, for example a JAR dependency of Java projects.
053   * Scope of libraries is Scopes.PROJECT
054   * @deprecated since 5.2 No more design features
055   */
056  @Deprecated
057  public static final String LIBRARY = "LIB";
058
059  /**
060   * Single project or root of multi-modules projects
061   * Scope is Scopes.PROJECT
062   */
063  public static final String PROJECT = "TRK";
064
065  /**
066   * Module of a multi-modules project. It's sometimes called "sub-project".
067   * Scope of modules is Scopes.PROJECT
068   */
069  public static final String MODULE = "BRC";
070
071  public static final String DIRECTORY = "DIR";
072  public static final String FILE = "FIL";
073
074  // ugly, should be replaced by "natures"
075  public static final String UNIT_TEST_FILE = "UTS";
076
077  /**
078   * List of qualifiers, ordered from bottom to up regarding position
079   * in tree of components
080   *
081   * @since 7.0
082   */
083  public static final List<String> ORDERED_BOTTOM_UP = unmodifiableList(asList(
084    FILE, UNIT_TEST_FILE, DIRECTORY, MODULE, PROJECT, APP, SUBVIEW, VIEW));
085
086  private Qualifiers() {
087    // only static methods
088  }
089
090  /**
091   * @param resource not nullable
092   */
093  public static boolean isView(final Resource resource, final boolean acceptSubViews) {
094    boolean isView = Objects.equals(VIEW, resource.getQualifier());
095    if (!isView && acceptSubViews) {
096      isView = Objects.equals(SUBVIEW, resource.getQualifier());
097    }
098
099    return isView;
100  }
101
102  /**
103   * @param resource not nullable
104   */
105  public static boolean isSubview(final Resource resource) {
106    return Objects.equals(SUBVIEW, resource.getScope());
107  }
108
109  /**
110   * @param resource not nullable
111   */
112  public static boolean isProject(final Resource resource, final boolean acceptModules) {
113    boolean isProject = Objects.equals(PROJECT, resource.getQualifier());
114    if (!isProject && acceptModules) {
115      isProject = Objects.equals(MODULE, resource.getQualifier());
116    }
117    return isProject;
118  }
119
120  /**
121   * @param resource not nullable
122   */
123  public static boolean isModule(final Resource resource) {
124    return Objects.equals(MODULE, resource.getQualifier());
125  }
126
127  /**
128   * @param resource not nullable
129   */
130  public static boolean isDirectory(final Resource resource) {
131    return Objects.equals(DIRECTORY, resource.getQualifier());
132  }
133
134  /**
135   * @param resource not nullable
136   */
137  public static boolean isFile(final Resource resource) {
138    return Objects.equals(FILE, resource.getQualifier());
139  }
140}