Adds ConfigKeys.newConfigKeyWithPrefixRemoved

Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/bbecf366
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/bbecf366
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/bbecf366

Branch: refs/heads/master
Commit: bbecf36662d79719d37777bebbd43f983ee834e5
Parents: 531effb
Author: Aled Sage <[email protected]>
Authored: Tue Nov 10 00:34:20 2015 +0000
Committer: Aled Sage <[email protected]>
Committed: Thu Nov 12 13:11:42 2015 +0000

----------------------------------------------------------------------
 .../apache/brooklyn/core/config/ConfigKeys.java |  8 ++++++++
 .../brooklyn/core/config/ConfigKeysTest.java    | 21 ++++++++++++++++++--
 2 files changed, 27 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/bbecf366/core/src/main/java/org/apache/brooklyn/core/config/ConfigKeys.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/config/ConfigKeys.java 
b/core/src/main/java/org/apache/brooklyn/core/config/ConfigKeys.java
index df03c29..ecc1ec0 100644
--- a/core/src/main/java/org/apache/brooklyn/core/config/ConfigKeys.java
+++ b/core/src/main/java/org/apache/brooklyn/core/config/ConfigKeys.java
@@ -157,6 +157,14 @@ public class ConfigKeys {
         return newConfigKeyRenamed(prefix+key.getName(), key);
     }
 
+    public static <T> ConfigKey<T> newConfigKeyWithPrefixRemoved(String 
prefix, ConfigKey<T> key) {
+        if (key.getName().startsWith(prefix)) {
+            return 
newConfigKeyRenamed(key.getName().substring(prefix.length()), key);
+        } else {
+            throw new IllegalArgumentException("key "+key+" does not start 
with prefix "+prefix);
+        }
+    }
+
     /** converts the name of the key from one case-strategy (e.g. lowerCamel) 
to andother (e.g. lower-hyphen) */
     public static <T> ConfigKey<T> convert(ConfigKey<T> key, CaseFormat 
inputCaseStrategy, CaseFormat outputCaseStrategy) {
         return newConfigKeyRenamed(inputCaseStrategy.to(outputCaseStrategy, 
key.getName()), key);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/bbecf366/core/src/test/java/org/apache/brooklyn/core/config/ConfigKeysTest.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/java/org/apache/brooklyn/core/config/ConfigKeysTest.java 
b/core/src/test/java/org/apache/brooklyn/core/config/ConfigKeysTest.java
index 12f713f..bb5a323 100644
--- a/core/src/test/java/org/apache/brooklyn/core/config/ConfigKeysTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/config/ConfigKeysTest.java
@@ -19,11 +19,10 @@
 package org.apache.brooklyn.core.config;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.fail;
 
 import org.apache.brooklyn.config.ConfigInheritance;
 import org.apache.brooklyn.config.ConfigKey;
-import org.apache.brooklyn.core.config.BasicConfigKey;
-import org.apache.brooklyn.core.config.ConfigKeys;
 import org.testng.annotations.Test;
 
 import com.google.common.base.CaseFormat;
@@ -61,6 +60,24 @@ public class ConfigKeysTest {
     }
     
     @Test
+    public void testConfigKeyWithoutPrefix() throws Exception {
+        ConfigKey<String> key = ConfigKeys.newStringConfigKey("a.b.mykey", "my 
descr", "my default val");
+        ConfigKey<String> key2 = 
ConfigKeys.newConfigKeyWithPrefixRemoved("a.b.", key);
+        
+        assertEquals(key2.getName(), "mykey");
+        assertEquals(key2.getType(), String.class);
+        assertEquals(key2.getDescription(), "my descr");
+        assertEquals(key2.getDefaultValue(), "my default val");
+        
+        try {
+            ConfigKey<String> key3 = 
ConfigKeys.newConfigKeyWithPrefixRemoved("wrong.prefix.", key);
+            fail("key="+key3);
+        } catch (IllegalArgumentException e) {
+            // success
+        }
+    }
+    
+    @Test
     public void testConfigKeyBuilder() throws Exception {
         ConfigKey<String> key = ConfigKeys.builder(String.class, "mykey")
             .description("my descr")

Reply via email to