From 535269bb6131bf816a21dfbb1dfd43611d7ca875 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Fri, 21 Sep 2012 18:20:46 +0200 Subject: [PATCH] SONAR-3754 Trying to remove duplication --- .../sonar/api/config/PropertyDefinitions.java | 61 +++++++++++-------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java index 6d7144a65e0..dfd1dfe5b55 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java @@ -40,8 +40,8 @@ import java.util.Map; */ public final class PropertyDefinitions implements BatchComponent, ServerComponent { - private Map definitions = Maps.newHashMap(); - private Map categories = Maps.newHashMap(); + private final Map definitions = Maps.newHashMap(); + private final Map categories = Maps.newHashMap(); public PropertyDefinitions(Object... components) { if (components != null) { @@ -99,14 +99,34 @@ public final class PropertyDefinitions implements BatchComponent, ServerComponen return definitions.values(); } - /** - * since 3.3 - */ - public Map> getGlobalPropertiesByCategory() { + static enum PropertyDefinitionFilter { + GLOBAL { + @Override + boolean accept(PropertyDefinition propertyDefinition) { + return propertyDefinition.isGlobal(); + } + }, + PROJECT { + @Override + boolean accept(PropertyDefinition propertyDefinition) { + return propertyDefinition.isOnProject(); + } + }, + MODULE { + @Override + boolean accept(PropertyDefinition propertyDefinition) { + return propertyDefinition.isOnModule(); + } + }; + + abstract boolean accept(PropertyDefinition propertyDefinition); + } + + private Map> getPropertiesByCategory(PropertyDefinitionFilter filter) { Multimap byCategory = ArrayListMultimap.create(); for (PropertyDefinition definition : getAll()) { - if (definition.isGlobal()) { + if (filter.accept(definition)) { byCategory.put(getCategory(definition.getKey()), definition); } } @@ -117,31 +137,22 @@ public final class PropertyDefinitions implements BatchComponent, ServerComponen /** * since 3.3 */ - public Map> getProjectPropertiesByCategory() { - Multimap byCategory = ArrayListMultimap.create(); - - for (PropertyDefinition definition : getAll()) { - if (definition.isOnProject()) { - byCategory.put(getCategory(definition.getKey()), definition); - } - } + public Map> getGlobalPropertiesByCategory() { + return getPropertiesByCategory(PropertyDefinitionFilter.GLOBAL); + } - return byCategory.asMap(); + /** + * since 3.3 + */ + public Map> getProjectPropertiesByCategory() { + return getPropertiesByCategory(PropertyDefinitionFilter.PROJECT); } /** * since 3.3 */ public Map> getModulePropertiesByCategory() { - Multimap byCategory = ArrayListMultimap.create(); - - for (PropertyDefinition definition : getAll()) { - if (definition.isOnModule()) { - byCategory.put(getCategory(definition.getKey()), definition); - } - } - - return byCategory.asMap(); + return getPropertiesByCategory(PropertyDefinitionFilter.MODULE); } public String getDefaultValue(String key) { -- 2.39.5