diff options
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/resources/Scopes.java | 33 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/resources/ScopesTest.java | 20 |
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)); + } } |