aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2011-01-28 10:26:31 +0100
committersimonbrandhof <simon.brandhof@gmail.com>2011-01-28 13:18:45 +0100
commitec4d0e0ddcc24e2169e3ec82b04a0734979dbc65 (patch)
treeca38b3552c5ec1f66c5044fa764a9e07313aa5f0 /sonar-plugin-api
parent1c2189b45f0a81436fcaab886b7072aea77a7d01 (diff)
downloadsonarqube-ec4d0e0ddcc24e2169e3ec82b04a0734979dbc65.tar.gz
sonarqube-ec4d0e0ddcc24e2169e3ec82b04a0734979dbc65.zip
add methods Scopes#isHigherThan() and isHigherThanOrEquals()
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/Scopes.java33
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/resources/ScopesTest.java20
2 files changed, 48 insertions, 5 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Scopes.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Scopes.java
index d9fdf46fa97..ff4f05d4dc0 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Scopes.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Scopes.java
@@ -19,6 +19,7 @@
*/
package org.sonar.api.resources;
+import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
/**
@@ -63,24 +64,46 @@ public final class Scopes {
*/
public static final String BLOCK_UNIT = "BLU";
+ public static final String[] SORTED_SCOPES = {PROJECT, DIRECTORY, FILE, TYPE, BLOCK_UNIT};
+
public static boolean isProject(final Resource resource) {
- return resource!=null && StringUtils.equals(PROJECT, resource.getScope());
+ return StringUtils.equals(PROJECT, resource.getScope());
}
public static boolean isDirectory(final Resource resource) {
- return resource!=null && StringUtils.equals(DIRECTORY, resource.getScope());
+ return StringUtils.equals(DIRECTORY, resource.getScope());
}
public static boolean isFile(final Resource resource) {
- return resource!=null && StringUtils.equals(FILE, resource.getScope());
+ return StringUtils.equals(FILE, resource.getScope());
}
public static boolean isType(final Resource resource) {
- return resource!=null && StringUtils.equals(TYPE, resource.getScope());
+ return StringUtils.equals(TYPE, resource.getScope());
}
public static boolean isBlockUnit(final Resource resource) {
- return resource!=null && StringUtils.equals(BLOCK_UNIT, resource.getScope());
+ 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;
}
} \ No newline at end of file
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ScopesTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ScopesTest.java
index 66f5b7eec3a..71fff1d16e4 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ScopesTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ScopesTest.java
@@ -66,5 +66,25 @@ public class ScopesTest {
assertThat(Scopes.isType(resource), is(false));
}
+ @Test
+ public void shouldBeHigherThan() {
+ assertThat(Scopes.isHigherThan(Scopes.PROJECT, Scopes.PROJECT), is(false));
+ assertThat(Scopes.isHigherThan(Scopes.PROJECT, Scopes.DIRECTORY), is(true));
+ assertThat(Scopes.isHigherThan(Scopes.PROJECT, Scopes.BLOCK_UNIT), is(true));
+
+ assertThat(Scopes.isHigherThan(Scopes.FILE, Scopes.FILE), is(false));
+ assertThat(Scopes.isHigherThan(Scopes.FILE, Scopes.DIRECTORY), is(false));
+ assertThat(Scopes.isHigherThan(Scopes.FILE, Scopes.BLOCK_UNIT), is(true));
+ }
+ @Test
+ public void shouldBeHigherThanOrEquals() {
+ assertThat(Scopes.isHigherThanOrEquals(Scopes.PROJECT, Scopes.PROJECT), is(true));
+ assertThat(Scopes.isHigherThanOrEquals(Scopes.PROJECT, Scopes.DIRECTORY), is(true));
+ assertThat(Scopes.isHigherThanOrEquals(Scopes.PROJECT, Scopes.BLOCK_UNIT), is(true));
+
+ assertThat(Scopes.isHigherThanOrEquals(Scopes.FILE, Scopes.FILE), is(true));
+ assertThat(Scopes.isHigherThanOrEquals(Scopes.FILE, Scopes.DIRECTORY), is(false));
+ assertThat(Scopes.isHigherThanOrEquals(Scopes.FILE, Scopes.BLOCK_UNIT), is(true));
+ }
}