aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/main/java/org/sonar/api/resources/Scopes.java
blob: 1ed5aad8bcab9aeb198503b38151e7079be68a2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * SonarQube :: Plugin API
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.api.resources;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

/**
 * Resource scopes are used to group some types of resources. For example Java methods, Flex methods, C functions
 * and Cobol paragraphs are grouped in the scope "block unit".
 * <p/>
 * Scopes are generally used in UI to display/hide some services or in web services.
 * <p/>
 * Scopes are not extensible by plugins.
 *
 * @since 2.6
 */
public final class Scopes {

  private Scopes() {
    // only static methods
  }

  /**
   * For example view, subview, project, module or library. Persisted in database.
   */
  public static final String PROJECT = "PRJ";

  /**
   * For example directory or Java package. Persisted in database. A more generic term for this scope could
   * be "namespace"
   */
  public static final String DIRECTORY = "DIR";

  /**
   * For example a Java file. Persisted in database. A more generic term for this scope could
   * be "compilation unit". It's the lowest scope in file system units.
   */
  public static final String FILE = "FIL";

  /**
   * Types like Java classes/interfaces. Not persisted in database.
   * @deprecated since 4.3 resources under FILE level are no more be supported since 4.2.
   */
  @Deprecated
  public static final String PROGRAM_UNIT = "PGU";

  /**
   * Block units like methods, functions or Cobol paragraphs.
   * @deprecated since 4.3 resources under FILE level are no more be supported since 4.2.
   */
  @Deprecated
  public static final String BLOCK_UNIT = "BLU";

  public static final String[] SORTED_SCOPES = {PROJECT, DIRECTORY, FILE, PROGRAM_UNIT, BLOCK_UNIT};

  public static boolean isProject(final Resource resource) {
    return StringUtils.equals(PROJECT, resource.getScope());
  }

  public static boolean isDirectory(final Resource resource) {
    return StringUtils.equals(DIRECTORY, resource.getScope());
  }

  /**
   * This scope is sometimes called a "compilation unit".
   */
  public static boolean isFile(final Resource resource) {
    return StringUtils.equals(FILE, resource.getScope());
  }

  /**
   * A program unit can be a Java class.
   * @deprecated since 4.3 resources under FILE level are no more be supported since 4.2.
   */
  @Deprecated
  public static boolean isProgramUnit(final Resource resource) {
    return StringUtils.equals(PROGRAM_UNIT, resource.getScope());
  }

  /**
   * @deprecated since 4.3 resources under FILE level are no more be supported since 4.2.
   */
  @Deprecated
  public static boolean isBlockUnit(final Resource resource) {
    return StringUtils.equals(BLOCK_UNIT, resource.getScope());
  }

  public static boolean isHigherThan(final Resource resource, final String than) {
    return isHigherThan(resource.getScope(), than);
  }

  public static boolean isHigherThan(final String scope, final String than) {
    int index = ArrayUtils.indexOf(SORTED_SCOPES, scope);
    int thanIndex = ArrayUtils.indexOf(SORTED_SCOPES, than);
    return index < thanIndex;
  }

  public static boolean isHigherThanOrEquals(final Resource resource, final String than) {
    return isHigherThanOrEquals(resource.getScope(), than);
  }

  public static boolean isHigherThanOrEquals(final String scope, final String than) {
    int index = ArrayUtils.indexOf(SORTED_SCOPES, scope);
    int thanIndex = ArrayUtils.indexOf(SORTED_SCOPES, than);
    return index <= thanIndex;
  }
}