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     */
020    package org.sonar.api.resources;
021    
022    import org.apache.commons.lang.ArrayUtils;
023    import org.apache.commons.lang.StringUtils;
024    
025    /**
026     * Resource scopes are used to group some types of resources. For example Java methods, Flex methods, C functions
027     * and Cobol paragraphs are grouped in the scope "block unit".
028     * <p/>
029     * Scopes are generally used in UI to display/hide some services or in web services.
030     * <p/>
031     * Scopes are not extensible by plugins.
032     *
033     * @since 2.6
034     */
035    public final class Scopes {
036    
037      private Scopes() {
038        // only static methods
039      }
040    
041      /**
042       * For example view, subview, project, module or library. Persisted in database.
043       */
044      public static final String PROJECT = "PRJ";
045    
046      /**
047       * For example directory or Java package. Persisted in database. A more generic term for this scope could
048       * be "namespace"
049       */
050      public static final String DIRECTORY = "DIR";
051    
052      /**
053       * For example a Java file. Persisted in database. A more generic term for this scope could
054       * be "compilation unit". It's the lowest scope in file system units.
055       */
056      public static final String FILE = "FIL";
057    
058      /**
059       * Types like Java classes/interfaces. Not persisted in database.
060       * @deprecated since 4.3 resources under FILE level are no more be supported since 4.2.
061       */
062      @Deprecated
063      public static final String PROGRAM_UNIT = "PGU";
064    
065      /**
066       * Block units like methods, functions or Cobol paragraphs.
067       * @deprecated since 4.3 resources under FILE level are no more be supported since 4.2.
068       */
069      @Deprecated
070      public static final String BLOCK_UNIT = "BLU";
071    
072      public static final String[] SORTED_SCOPES = {PROJECT, DIRECTORY, FILE, PROGRAM_UNIT, BLOCK_UNIT};
073    
074      public static boolean isProject(final Resource resource) {
075        return StringUtils.equals(PROJECT, resource.getScope());
076      }
077    
078      public static boolean isDirectory(final Resource resource) {
079        return StringUtils.equals(DIRECTORY, resource.getScope());
080      }
081    
082      /**
083       * This scope is sometimes called a "compilation unit".
084       */
085      public static boolean isFile(final Resource resource) {
086        return StringUtils.equals(FILE, resource.getScope());
087      }
088    
089      /**
090       * A program unit can be a Java class.
091       * @deprecated since 4.3 resources under FILE level are no more be supported since 4.2.
092       */
093      @Deprecated
094      public static boolean isProgramUnit(final Resource resource) {
095        return StringUtils.equals(PROGRAM_UNIT, resource.getScope());
096      }
097    
098      /**
099       * @deprecated since 4.3 resources under FILE level are no more be supported since 4.2.
100       */
101      @Deprecated
102      public static boolean isBlockUnit(final Resource resource) {
103        return StringUtils.equals(BLOCK_UNIT, resource.getScope());
104      }
105    
106      public static boolean isHigherThan(final Resource resource, final String than) {
107        return isHigherThan(resource.getScope(), than);
108      }
109    
110      public static boolean isHigherThan(final String scope, final String than) {
111        int index = ArrayUtils.indexOf(SORTED_SCOPES, scope);
112        int thanIndex = ArrayUtils.indexOf(SORTED_SCOPES, than);
113        return index < thanIndex;
114      }
115    
116      public static boolean isHigherThanOrEquals(final Resource resource, final String than) {
117        return isHigherThanOrEquals(resource.getScope(), than);
118      }
119    
120      public static boolean isHigherThanOrEquals(final String scope, final String than) {
121        int index = ArrayUtils.indexOf(SORTED_SCOPES, scope);
122        int thanIndex = ArrayUtils.indexOf(SORTED_SCOPES, than);
123        return index <= thanIndex;
124      }
125    }