Adds EntityPredicates.hasInterfaceMatching

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

Branch: refs/heads/master
Commit: 7b57f99bbb04c7d66bafc251e49b8952350cd59a
Parents: 78776ca
Author: Sam Corbett <[email protected]>
Authored: Tue Apr 28 19:35:36 2015 +0100
Committer: Sam Corbett <[email protected]>
Committed: Tue May 5 12:14:56 2015 +0100

----------------------------------------------------------------------
 .../brooklyn/entity/basic/EntityPredicates.java | 35 ++++++++++++++++++--
 .../entity/basic/EntityPredicatesTest.java      | 11 ++++++
 2 files changed, 44 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7b57f99b/core/src/main/java/brooklyn/entity/basic/EntityPredicates.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/basic/EntityPredicates.java 
b/core/src/main/java/brooklyn/entity/basic/EntityPredicates.java
index 5906d78..96ecb7a 100644
--- a/core/src/main/java/brooklyn/entity/basic/EntityPredicates.java
+++ b/core/src/main/java/brooklyn/entity/basic/EntityPredicates.java
@@ -19,6 +19,7 @@
 package brooklyn.entity.basic;
 
 import java.util.Collection;
+import java.util.regex.Pattern;
 
 import javax.annotation.Nullable;
 
@@ -30,6 +31,7 @@ import brooklyn.event.AttributeSensor;
 import brooklyn.location.Location;
 import brooklyn.util.collections.CollectionFunctionals;
 import brooklyn.util.guava.SerializablePredicate;
+import brooklyn.util.javalang.Reflections;
 import brooklyn.util.text.StringPredicates;
 
 import com.google.common.base.Objects;
@@ -271,9 +273,38 @@ public class EntityPredicates {
     // ---------------------------
 
     /**
+     * @param typeRegex a regular expression
+     * @return true if any of the interfaces implemented by the entity 
(including those derived) match typeRegex.
+     */
+    public static Predicate<Entity> hasInterfaceMatching(String typeRegex) {
+        return new ImplementsInterface(typeRegex);
+    }
+
+    protected static class ImplementsInterface implements 
SerializablePredicate<Entity> {
+        protected final Pattern pattern;
+
+        public ImplementsInterface(String typeRegex) {
+            this.pattern = Pattern.compile(typeRegex);
+        }
+
+        @Override
+        public boolean apply(@Nullable Entity input) {
+            if (input == null) return false;
+            for (Class<?> cls : 
Reflections.getAllInterfaces(input.getClass())) {
+                if (pattern.matcher(cls.getName()).matches()) {
+                    return true;
+                }
+            }
+            return false;
+        }
+    }
+
+    // ---------------------------
+
+    /**
      * Returns a predicate that determines if a given entity is a direct child 
of this {@code parent}.
      */
-    public static <T> Predicate<Entity> isChildOf(final Entity parent) {
+    public static Predicate<Entity> isChildOf(final Entity parent) {
         return new IsChildOf(parent);
     }
 
@@ -307,7 +338,7 @@ public class EntityPredicates {
 
     // ---------------------------
     
-    public static <T> Predicate<Entity> isMemberOf(final Group group) {
+    public static Predicate<Entity> isMemberOf(final Group group) {
         return new IsMemberOf(group);
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7b57f99b/core/src/test/java/brooklyn/entity/basic/EntityPredicatesTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/basic/EntityPredicatesTest.java 
b/core/src/test/java/brooklyn/entity/basic/EntityPredicatesTest.java
index 9394b85..bfa6933 100644
--- a/core/src/test/java/brooklyn/entity/basic/EntityPredicatesTest.java
+++ b/core/src/test/java/brooklyn/entity/basic/EntityPredicatesTest.java
@@ -26,6 +26,7 @@ import org.testng.annotations.Test;
 
 import brooklyn.entity.BrooklynAppUnitTestSupport;
 import brooklyn.entity.proxying.EntitySpec;
+import brooklyn.entity.trait.Changeable;
 import brooklyn.location.Location;
 import brooklyn.test.entity.TestEntity;
 import brooklyn.util.text.StringPredicates;
@@ -113,4 +114,14 @@ public class EntityPredicatesTest extends 
BrooklynAppUnitTestSupport {
         assertTrue(EntityPredicates.locationsIncludes(loc).apply(entity));
         assertFalse(EntityPredicates.locationsIncludes(loc).apply(app));
     }
+
+    @Test
+    public void testHasInterfaceMatching() throws Exception {
+        assertTrue(EntityPredicates.hasInterfaceMatching(".*").apply(entity));
+        
assertTrue(EntityPredicates.hasInterfaceMatching(".*TestEntity").apply(entity));
+        
assertFalse(EntityPredicates.hasInterfaceMatching(".*TestEntity").apply(group));
+        
assertTrue(EntityPredicates.hasInterfaceMatching(Changeable.class.getName()).apply(group));
+        
assertTrue(EntityPredicates.hasInterfaceMatching(".*C.*able").apply(group));
+    }
+
 }

Reply via email to