001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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     */
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       */
061      public static final String PROGRAM_UNIT = "PGU";
062    
063      /**
064       * Block units like methods, functions or Cobol paragraphs.
065       */
066      public static final String BLOCK_UNIT = "BLU";
067    
068      public static final String[] SORTED_SCOPES = {PROJECT, DIRECTORY, FILE, PROGRAM_UNIT, BLOCK_UNIT};
069    
070    
071      public static boolean isProject(final Resource resource) {
072        return StringUtils.equals(PROJECT, resource.getScope());
073      }
074    
075      public static boolean isDirectory(final Resource resource) {
076        return StringUtils.equals(DIRECTORY, resource.getScope());
077      }
078    
079      /**
080       * This scope is sometimes called a "compilation unit".
081       */
082      public static boolean isFile(final Resource resource) {
083        return StringUtils.equals(FILE, resource.getScope());
084      }
085    
086      /**
087       * A program unit can be a Java class.
088       */
089      public static boolean isProgramUnit(final Resource resource) {
090        return StringUtils.equals(PROGRAM_UNIT, resource.getScope());
091      }
092    
093      public static boolean isBlockUnit(final Resource resource) {
094        return StringUtils.equals(BLOCK_UNIT, resource.getScope());
095      }
096    
097      public static boolean isHigherThan(final Resource resource, final String than) {
098        return isHigherThan(resource.getScope(), than);
099      }
100    
101      public static boolean isHigherThan(final String scope, final String than) {
102        int index = ArrayUtils.indexOf(SORTED_SCOPES, scope);
103        int thanIndex = ArrayUtils.indexOf(SORTED_SCOPES, than);
104        return index < thanIndex;
105      }
106    
107      public static boolean isHigherThanOrEquals(final Resource resource, final String than) {
108        return isHigherThanOrEquals(resource.getScope(), than);
109      }
110    
111      public static boolean isHigherThanOrEquals(final String scope, final String than) {
112        int index = ArrayUtils.indexOf(SORTED_SCOPES, scope);
113        int thanIndex = ArrayUtils.indexOf(SORTED_SCOPES, than);
114        return index <= thanIndex;
115      }
116    }