Repository: brooklyn-server
Updated Branches:
  refs/heads/master 2aba7433d -> 4d256e402


Adds coercion for simple string->predicate

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

Branch: refs/heads/master
Commit: ec01ecbf49698888e17f1f40a69e54f7e3bd27e1
Parents: c10fd58
Author: Aled Sage <aled.s...@gmail.com>
Authored: Fri Jul 14 15:24:13 2017 +0100
Committer: Aled Sage <aled.s...@gmail.com>
Committed: Fri Jul 14 15:24:13 2017 +0100

----------------------------------------------------------------------
 .../javalang/coerce/CommonAdaptorTypeCoercions.java | 14 ++++++++++++++
 .../util/javalang/coerce/TypeCoercionsTest.java     | 16 ++++++++++++++++
 2 files changed, 30 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ec01ecbf/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java
----------------------------------------------------------------------
diff --git 
a/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java
 
b/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java
index b74548e..ae4c8b3 100644
--- 
a/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java
+++ 
b/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java
@@ -48,6 +48,8 @@ import org.apache.brooklyn.util.time.Time;
 import org.apache.brooklyn.util.yaml.Yamls;
 
 import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
@@ -273,6 +275,18 @@ public class CommonAdaptorTypeCoercions {
                 return new Date(input);
             }
         });
+        registerAdapter(String.class, Predicate.class, new 
Function<String,Predicate>() {
+            @Override
+            public Predicate apply(final String input) {
+                switch (input) {
+                case "alwaysFalse" : return Predicates.alwaysFalse();
+                case "alwaysTrue" :  return Predicates.alwaysTrue();
+                case "isNull" :      return Predicates.isNull();
+                case "notNull" :     return Predicates.notNull();
+                default: throw new IllegalArgumentException("Cannot convert 
string '" + input + "' to predicate");
+                }
+            }
+        });
     }
     
     @SuppressWarnings("rawtypes")

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ec01ecbf/utils/common/src/test/java/org/apache/brooklyn/util/javalang/coerce/TypeCoercionsTest.java
----------------------------------------------------------------------
diff --git 
a/utils/common/src/test/java/org/apache/brooklyn/util/javalang/coerce/TypeCoercionsTest.java
 
b/utils/common/src/test/java/org/apache/brooklyn/util/javalang/coerce/TypeCoercionsTest.java
index e8ff59d..74312ff 100644
--- 
a/utils/common/src/test/java/org/apache/brooklyn/util/javalang/coerce/TypeCoercionsTest.java
+++ 
b/utils/common/src/test/java/org/apache/brooklyn/util/javalang/coerce/TypeCoercionsTest.java
@@ -45,6 +45,7 @@ import org.testng.Assert;
 import org.testng.annotations.Test;
 
 import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
@@ -474,6 +475,21 @@ public class TypeCoercionsTest {
         assertEquals(coerce("1.0", Number.class), Double.valueOf(1.0));
     }
 
+    @Test
+    public void testCoerceStringToPredicate() {
+        assertEquals(coerce("alwaysFalse", Predicate.class), 
Predicates.alwaysFalse());
+        assertEquals(coerce("alwaysTrue", Predicate.class), 
Predicates.alwaysTrue());
+        assertEquals(coerce("isNull", Predicate.class), Predicates.isNull());
+        assertEquals(coerce("notNull", Predicate.class), Predicates.notNull());
+        
+        try {
+            coerce("wrongInput", Predicate.class);
+            Asserts.shouldHaveFailedPreviously();
+        } catch (RuntimeException e) {
+            Asserts.expectedFailureContains(e, "Cannot convert string 
'wrongInput' to predicate");
+        }
+    }
+
     @Test(expectedExceptions = 
org.apache.brooklyn.util.javalang.coerce.ClassCoercionException.class)
     public void testInvalidCoercionThrowsClassCoercionException() {
         coerce(new Object(), TypeToken.of(Integer.class));

Reply via email to