brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From geom...@apache.org
Subject [1/4] brooklyn-server git commit: Add Functionals.isSatisfied(subject, predicate)
Date Wed, 08 Mar 2017 15:12:06 GMT
Repository: brooklyn-server
Updated Branches:
  refs/heads/master b55e234b3 -> 40af35c42


Add Functionals.isSatisfied(subject, 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/24f4e0b1
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/24f4e0b1
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/24f4e0b1

Branch: refs/heads/master
Commit: 24f4e0b1ac3ed2d463c51b5b2126f0726b44436a
Parents: b55e234
Author: Alasdair Hodge <github@alasdairhodge.co.uk>
Authored: Wed Mar 8 13:21:20 2017 +0000
Committer: Alasdair Hodge <github@alasdairhodge.co.uk>
Committed: Wed Mar 8 13:21:20 2017 +0000

----------------------------------------------------------------------
 .../org/apache/brooklyn/util/guava/Functionals.java    | 12 ++++++++++++
 .../apache/brooklyn/util/guava/FunctionalsTest.java    | 13 ++++++++++++-
 2 files changed, 24 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/24f4e0b1/utils/common/src/main/java/org/apache/brooklyn/util/guava/Functionals.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/guava/Functionals.java b/utils/common/src/main/java/org/apache/brooklyn/util/guava/Functionals.java
index 4a9a8c4..18238d2 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/guava/Functionals.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/guava/Functionals.java
@@ -148,4 +148,16 @@ public class Functionals {
         return new FunctionAsPredicate();
     }
 
+    /**
+     * Simple adapter from {@link Predicate} to {@link Callable} by currying the passed <tt>subject</tt>
parameter.
+     */
+    public static <T> Callable<Boolean> isSatisfied(final T subject, final Predicate<T>
predicate) {
+        return new Callable<Boolean>() {
+            @Override
+            public Boolean call() {
+                return predicate.apply(subject);
+            }
+        };
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/24f4e0b1/utils/common/src/test/java/org/apache/brooklyn/util/guava/FunctionalsTest.java
----------------------------------------------------------------------
diff --git a/utils/common/src/test/java/org/apache/brooklyn/util/guava/FunctionalsTest.java
b/utils/common/src/test/java/org/apache/brooklyn/util/guava/FunctionalsTest.java
index d33552b..bb871c3 100644
--- a/utils/common/src/test/java/org/apache/brooklyn/util/guava/FunctionalsTest.java
+++ b/utils/common/src/test/java/org/apache/brooklyn/util/guava/FunctionalsTest.java
@@ -18,11 +18,11 @@
  */
 package org.apache.brooklyn.util.guava;
 
-import org.apache.brooklyn.util.guava.Functionals;
 import org.apache.brooklyn.util.math.MathFunctions;
 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.base.Suppliers;
 
@@ -55,4 +55,15 @@ public class FunctionalsTest {
         IfFunctionsTest.checkTF(Functionals.ifNotEquals(false).value("T").defaultValue("F").build(),
"T");
     }
 
+    @Test
+    public void testIsSatisfied() throws Exception {
+        Predicate<Integer> isEven = new Predicate<Integer>() {
+            @Override public boolean apply(Integer input) {
+                return (input % 2 == 0);
+            }
+        };
+        Assert.assertFalse(Functionals.isSatisfied(11, isEven).call());
+        Assert.assertTrue(Functionals.isSatisfied(22, isEven).call());
+    }
+
 }


Mime
View raw message