Convenience functions to access attributes + config by name.

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

Branch: refs/heads/master
Commit: 33647865f41649f48ce38cbda41a8cd4d9b0e94b
Parents: ddb6acc
Author: Alasdair Hodge <[email protected]>
Authored: Fri Aug 5 14:25:48 2016 +0100
Committer: Alasdair Hodge <[email protected]>
Committed: Fri Aug 5 15:46:48 2016 +0100

----------------------------------------------------------------------
 .../brooklyn/core/entity/EntityFunctions.java   | 54 +++++++++++++++++++-
 .../core/entity/EntityFunctionsTest.java        | 14 +++--
 2 files changed, 63 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/33647865/core/src/main/java/org/apache/brooklyn/core/entity/EntityFunctions.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/brooklyn/core/entity/EntityFunctions.java 
b/core/src/main/java/org/apache/brooklyn/core/entity/EntityFunctions.java
index c65a176..7d8fa3d 100644
--- a/core/src/main/java/org/apache/brooklyn/core/entity/EntityFunctions.java
+++ b/core/src/main/java/org/apache/brooklyn/core/entity/EntityFunctions.java
@@ -31,7 +31,9 @@ import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.apache.brooklyn.api.objs.Identifiable;
 import org.apache.brooklyn.api.sensor.AttributeSensor;
 import org.apache.brooklyn.config.ConfigKey;
+import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic;
+import org.apache.brooklyn.core.sensor.Sensors;
 import org.apache.brooklyn.util.core.flags.TypeCoercions;
 import org.apache.brooklyn.util.guava.Functionals;
 
@@ -141,7 +143,11 @@ public class EntityFunctions {
         }
         return new AppsSupplier();
     }
-    
+
+    public static Function<Entity, Object> attribute(String attributeName) {
+        return attribute(Sensors.newSensor(Object.class, attributeName));
+    }
+
     public static <T> Function<Entity, T> attribute(AttributeSensor<T> 
attribute) {
         return new GetEntityAttributeFunction<T>(checkNotNull(attribute, 
"attribute"));
     }
@@ -156,6 +162,26 @@ public class EntityFunctions {
         }
     }
 
+    public static Function<Entity, String> attribute(String attributeName, 
String format) {
+        return attribute(Sensors.newSensor(Object.class, attributeName), 
format);
+    }
+
+    public static Function<Entity, String> attribute(AttributeSensor<?> 
attribute, String format) {
+        return new FormatEntityAttributeFunction(checkNotNull(attribute, 
"attribute"), checkNotNull(format, "format"));
+    }
+
+    protected static class FormatEntityAttributeFunction implements 
Function<Entity, String> {
+        private final AttributeSensor<?> attribute;
+        private final String format;
+        protected FormatEntityAttributeFunction(AttributeSensor<?> attribute, 
String format) {
+            this.attribute = attribute;
+            this.format = format;
+        }
+        @Override public String apply(Entity input) {
+            return (input == null) ? null : String.format(format, 
input.getAttribute(attribute));
+        }
+    }
+
     public static <T> Function<Object, T> attribute(Entity entity, 
AttributeSensor<T> attribute) {
         return new GetFixedEntityAttributeFunction<>(entity, attribute);
     }
@@ -172,6 +198,10 @@ public class EntityFunctions {
         }
     }
 
+    public static Function<Entity, Object> config(String keyName) {
+        return config(ConfigKeys.newConfigKey(Object.class, keyName));
+    }
+
     public static <T> Function<Entity, T> config(ConfigKey<T> key) {
         return new GetEntityConfigFunction<T>(checkNotNull(key, "key"));
     }
@@ -188,6 +218,28 @@ public class EntityFunctions {
         }
     }
 
+    public static Function<Entity, String> config(String keyName, String 
format) {
+        return config(ConfigKeys.newConfigKey(Object.class, keyName), format);
+    }
+
+    public static Function<Entity, String> config(ConfigKey<?> key, String 
format) {
+        return new FormatEntityConfigFunction(checkNotNull(key, "key"), 
checkNotNull(format, "format"));
+    }
+
+    protected static class FormatEntityConfigFunction implements 
Function<Entity, String> {
+        private final ConfigKey<?> key;
+        private final String format;
+
+        protected FormatEntityConfigFunction(ConfigKey<?> key, String format) {
+            this.key = key;
+            this.format = format;
+        }
+
+        @Override public String apply(Entity input) {
+            return (input == null) ? null : String.format(format, 
input.getConfig(key));
+        }
+    }
+
     public static Function<Entity, String> displayName() {
         return GetEntityDisplayName.INSTANCE;
     }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/33647865/core/src/test/java/org/apache/brooklyn/core/entity/EntityFunctionsTest.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/java/org/apache/brooklyn/core/entity/EntityFunctionsTest.java 
b/core/src/test/java/org/apache/brooklyn/core/entity/EntityFunctionsTest.java
index 6bb9e35..898e5ab 100644
--- 
a/core/src/test/java/org/apache/brooklyn/core/entity/EntityFunctionsTest.java
+++ 
b/core/src/test/java/org/apache/brooklyn/core/entity/EntityFunctionsTest.java
@@ -49,6 +49,9 @@ public class EntityFunctionsTest extends 
BrooklynAppUnitTestSupport {
     public void testAttribute() throws Exception {
         entity.sensors().set(TestEntity.NAME, "myname");
         assertEquals(EntityFunctions.attribute(TestEntity.NAME).apply(entity), 
"myname");
+        
assertEquals(EntityFunctions.attribute(TestEntity.NAME.getName()).apply(entity),
 "myname");
+        assertEquals(EntityFunctions.attribute(TestEntity.NAME, "%s - 
suffix").apply(entity), "myname - suffix");
+        assertEquals(EntityFunctions.attribute(TestEntity.NAME.getName(), "%s 
- suffix").apply(entity), "myname - suffix");
         
assertNull(EntityFunctions.attribute(TestEntity.SEQUENCE).apply(entity));
     }
 
@@ -57,24 +60,27 @@ public class EntityFunctionsTest extends 
BrooklynAppUnitTestSupport {
         entity.sensors().set(TestEntity.NAME, "myname");
         assertEquals(EntityFunctions.attribute(entity, 
TestEntity.NAME).apply(new Object()), "myname");
     }
-    
+
     @Test
     public void testConfig() throws Exception {
         entity.config().set(TestEntity.CONF_NAME, "myname");
         
assertEquals(EntityFunctions.config(TestEntity.CONF_NAME).apply(entity), 
"myname");
+        
assertEquals(EntityFunctions.config(TestEntity.CONF_NAME.getName()).apply(entity),
 "myname");
+        assertEquals(EntityFunctions.config(TestEntity.CONF_NAME, "%s - 
suffix").apply(entity), "myname - suffix");
+        assertEquals(EntityFunctions.config(TestEntity.CONF_NAME.getName(), 
"%s - suffix").apply(entity), "myname - suffix");
         
assertNull(EntityFunctions.config(TestEntity.CONF_OBJECT).apply(entity));
     }
-    
+
     @Test
     public void testDisplayName() throws Exception {
         assertEquals(EntityFunctions.displayName().apply(entity), 
"mydisplayname");
     }
-    
+
     @Test
     public void testId() throws Exception {
         assertEquals(EntityFunctions.id().apply(entity), entity.getId());
     }
-    
+
     @Test
     public void testLocationMatching() throws Exception {
         entity.addLocations(ImmutableList.of(loc));

Reply via email to