You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by al...@apache.org on 2015/09/23 12:52:01 UTC

[09/10] incubator-brooklyn git commit: entity.policies() etc returns an Iterable

entity.policies() etc returns an Iterable

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

Branch: refs/heads/master
Commit: a63b8cf13ed62decb136bccd20eb35df706d5046
Parents: ebe942b
Author: Aled Sage <al...@gmail.com>
Authored: Wed Sep 23 11:10:04 2015 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Wed Sep 23 11:10:04 2015 +0100

----------------------------------------------------------------------
 .../org/apache/brooklyn/api/entity/Entity.java  | 89 ++++++++++----------
 .../brooklyn/core/entity/AbstractEntity.java    | 40 ++++++---
 .../brooklyn/core/entity/EntityInternal.java    | 22 +++++
 3 files changed, 92 insertions(+), 59 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a63b8cf1/api/src/main/java/org/apache/brooklyn/api/entity/Entity.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/org/apache/brooklyn/api/entity/Entity.java b/api/src/main/java/org/apache/brooklyn/api/entity/Entity.java
index 32f9c00..eb9f907 100644
--- a/api/src/main/java/org/apache/brooklyn/api/entity/Entity.java
+++ b/api/src/main/java/org/apache/brooklyn/api/entity/Entity.java
@@ -19,17 +19,16 @@
 package org.apache.brooklyn.api.entity;
 
 import java.util.Collection;
+import java.util.Iterator;
 import java.util.Map;
 
 import javax.annotation.Nullable;
 
 import org.apache.brooklyn.api.effector.Effector;
 import org.apache.brooklyn.api.location.Location;
-import org.apache.brooklyn.api.mgmt.SubscriptionContext;
-import org.apache.brooklyn.api.mgmt.SubscriptionHandle;
-import org.apache.brooklyn.api.mgmt.SubscriptionManager;
 import org.apache.brooklyn.api.mgmt.Task;
 import org.apache.brooklyn.api.objs.BrooklynObject;
+import org.apache.brooklyn.api.objs.EntityAdjunct;
 import org.apache.brooklyn.api.policy.Policy;
 import org.apache.brooklyn.api.policy.PolicySpec;
 import org.apache.brooklyn.api.sensor.AttributeSensor;
@@ -38,7 +37,6 @@ import org.apache.brooklyn.api.sensor.EnricherSpec;
 import org.apache.brooklyn.api.sensor.Feed;
 import org.apache.brooklyn.api.sensor.Sensor;
 import org.apache.brooklyn.api.sensor.SensorEvent;
-import org.apache.brooklyn.api.sensor.SensorEventListener;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.config.ConfigKey.HasConfigKey;
 import org.apache.brooklyn.util.guava.Maybe;
@@ -362,79 +360,80 @@ public interface Entity extends BrooklynObject {
         <T> void emit(Sensor<T> sensor, T value);
     }
     
-    @Beta
-    public interface PolicySupport {
+    public interface AdjunctSupport<T extends EntityAdjunct> extends Iterable<T> {
         /**
-         * @return an immutable thread-safe view of the policies.
+         * @return A read-only thread-safe iterator over all the instances.
          */
-        Collection<Policy> getPolicies();
+        Iterator<T> iterator();
         
         /**
-         * Adds the given policy to this entity. Also calls policy.setEntity if available.
+         * Adds an instance.
          */
-        void add(Policy policy);
+        void add(T val);
         
         /**
+         * Removes an instance.
+         */
+        boolean remove(T val);
+    }
+    
+    @Beta
+    public interface PolicySupport extends AdjunctSupport<Policy> {
+        /**
          * Adds the given policy to this entity. Also calls policy.setEntity if available.
          */
-        <T extends Policy> T add(PolicySpec<T> enricher);
+        @Override
+        void add(Policy policy);
         
         /**
          * Removes the given policy from this entity. 
          * @return True if the policy existed at this entity; false otherwise
          */
+        @Override
         boolean remove(Policy policy);
+        
+        /**
+         * Adds the given policy to this entity. Also calls policy.setEntity if available.
+         */
+        <T extends Policy> T add(PolicySpec<T> enricher);
     }
     
     @Beta
-    public interface EnricherSupport {
-        /**
-         * @return an immutable thread-safe view of the enrichers.
-         */
-        Collection<Enricher> getEnrichers();
-        
+    public interface EnricherSupport extends AdjunctSupport<Enricher> {
         /**
          * Adds the given enricher to this entity. Also calls enricher.setEntity if available.
          */
+        @Override
         void add(Enricher enricher);
         
         /**
-         * Adds the given enricher to this entity. Also calls enricher.setEntity if available.
-         */
-        <T extends Enricher> T add(EnricherSpec<T> enricher);
-        
-        /**
          * Removes the given enricher from this entity. 
          * @return True if the policy enricher at this entity; false otherwise
          */
+        @Override
         boolean remove(Enricher enricher);
-    }
-    
-    @Beta
-    public interface GroupSupport {
-        /**
-         * The {@link Collection} of {@link Group}s that this entity is a member of.
-         *
-         * Groupings can be used to allow easy management/monitoring of a group of entities.
-         */
-        Collection<Group> getGroups();
-
+        
         /**
-         * Add this entity as a member of the given {@link Group}. Called by framework.
-         * <p>
-         * Users should call {@link Group#addMember(Entity)} instead; this method will then 
-         * automatically be called. However, the reverse is not true (calling this method will 
-         * not tell the group; this behaviour may change in a future release!)
+         * Adds the given enricher to this entity. Also calls enricher.setEntity if available.
          */
-        void add(Group group);
+        <T extends Enricher> T add(EnricherSpec<T> enricher);
+    }
 
+    /**
+     * For managing/querying the group membership of this entity. 
+     * 
+     * Groupings can be used to allow easy management/monitoring of a group of entities.
+     * 
+     * To add/remove this entity from a group, users should call {@link Group#addMember(Entity)} 
+     * and {@link Group#removeMember(Entity)}. In a future release, add/remove methods may be
+     * added here.
+     */
+    @Beta
+    public interface GroupSupport extends Iterable<Group> {
         /**
-         * Removes this entity as a member of the given {@link Group}. Called by framework.
-         * <p>
-         * Users should call {@link Group#removeMember(Entity)} instead; this method will then 
-         * automatically be called. However, the reverse is not true (calling this method will 
-         * not tell the group; this behaviour may change in a future release!)
+         * A read-only thread-safe iterator over all the {@link Group}s that this entity is a member of.
          */
-        void remove(Group group);
+        @Override
+        Iterator<Group> iterator();
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a63b8cf1/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java b/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java
index 08b25f2..e432717 100644
--- a/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java
+++ b/core/src/main/java/org/apache/brooklyn/core/entity/AbstractEntity.java
@@ -705,7 +705,16 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E
      */
     @Beta
     // TODO revert to private when groups() is reverted to return GroupSupport
-    public class BasicGroupSupport implements GroupSupport {
+    public class BasicGroupSupport implements GroupSupportInternal {
+        @Override
+        public Iterator<Group> iterator() { 
+            return asList().iterator();
+        }
+        
+        protected List<Group> asList() { 
+            return ImmutableList.copyOf(groupsInternal);
+        }
+        
         @Override
         public void add(Group group) {
             boolean changed = groupsInternal.add(group);
@@ -725,11 +734,6 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E
                 emit(AbstractEntity.GROUP_REMOVED, group);
             }
         }
-        
-        @Override
-        public Collection<Group> getGroups() { 
-            return ImmutableList.copyOf(groupsInternal);
-        }
     }
     
     /**
@@ -751,12 +755,12 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E
     }
 
     /**
-     * @deprecated since 0.9.0; see {@link #groups()} and {@link GroupSupport#getGroups()}
+     * @deprecated since 0.9.0; see {@link #groups()} and {@link GroupSupport#iterator()}
      */
     @Override
     @Deprecated
     public Collection<Group> getGroups() { 
-        return groups().getGroups();
+        return groups().asList();
     }
     
     @Override
@@ -1607,7 +1611,11 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E
     public class BasicPolicySupport implements PolicySupportInternal {
         
         @Override
-        public Collection<Policy> getPolicies() {
+        public Iterator<Policy> iterator() {
+            return asList().iterator();
+        }
+
+        protected List<Policy> asList() {
             return ImmutableList.<Policy>copyOf(policiesInternal);
         }
 
@@ -1666,7 +1674,11 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E
     // TODO revert to private when config() is reverted to return SensorSupportInternal
     public class BasicEnricherSupport implements EnricherSupportInternal {
         @Override
-        public Collection<Enricher> getEnrichers() {
+        public Iterator<Enricher> iterator() {
+            return asList().iterator();
+        }
+
+        protected List<Enricher> asList() {
             return ImmutableList.<Enricher>copyOf(enrichersInternal);
         }
 
@@ -1716,12 +1728,12 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E
     }
     
     /**
-     * @deprecated since 0.9.0; see {@link BasicPolicySupport#getPolicies()}; e.g. {@code policies().getPolicies()}
+     * @deprecated since 0.9.0; see {@link BasicPolicySupport#iterator()}; e.g. {@code policies().iterator()}
      */
     @Override
     @Deprecated
     public Collection<Policy> getPolicies() {
-        return policies().getPolicies();
+        return policies().asList();
     }
 
     /**
@@ -1770,12 +1782,12 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E
     }
     
     /**
-     * @deprecated since 0.9.0; see {@link BasicEnricherSupport#getEnrichers()}; e.g. {@code enrichers().getEnrichers()}
+     * @deprecated since 0.9.0; see {@link BasicEnricherSupport#iterator()}; e.g. {@code enrichers().iterator()}
      */
     @Override
     @Deprecated
     public Collection<Enricher> getEnrichers() {
-        return enrichers().getEnrichers();
+        return enrichers().asList();
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a63b8cf1/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java b/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java
index c0eca7e..27b09a4 100644
--- a/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java
+++ b/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java
@@ -24,6 +24,7 @@ import java.util.Map;
 import org.apache.brooklyn.api.effector.Effector;
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.entity.EntityLocal;
+import org.apache.brooklyn.api.entity.Group;
 import org.apache.brooklyn.api.location.Location;
 import org.apache.brooklyn.api.mgmt.ExecutionContext;
 import org.apache.brooklyn.api.mgmt.ManagementContext;
@@ -244,4 +245,25 @@ public interface EntityInternal extends BrooklynObjectInternal, EntityLocal, Reb
          */
         boolean removeAll();
     }
+    
+    @Beta
+    public interface GroupSupportInternal extends Entity.GroupSupport {
+        /**
+         * Add this entity as a member of the given {@link Group}. Called by framework.
+         * <p>
+         * Users should call {@link Group#addMember(Entity)} instead; this method will then 
+         * automatically be called. However, the reverse is not true (calling this method will 
+         * not tell the group; this behaviour may change in a future release!)
+         */
+        void add(Group group);
+
+        /**
+         * Removes this entity as a member of the given {@link Group}. Called by framework.
+         * <p>
+         * Users should call {@link Group#removeMember(Entity)} instead; this method will then 
+         * automatically be called. However, the reverse is not true (calling this method will 
+         * not tell the group; this behaviour may change in a future release!)
+         */
+        void remove(Group group);
+    }
 }