Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package brooklyn.policy.loadbalancing;
  
  import java.util.Map;
  
  import org.slf4j.Logger;
  
 
A group of items that are contained within a given (dynamically changing) set of containers. The setContainers(Group) sets the group of containers. The membership of that group is dynamically tracked. When containers are added/removed, or when an items is added/removed, or when an Moveable item is moved then the membership of this group of items is automatically updated accordingly. For example: in Monterey, this could be used to track the actors that are within a given cluster of venues.
 
 public class ItemsInContainersGroup extends DynamicGroupImpl {
 
     // TODO Inefficient: will not scale to many 1000s of items
 
     private static final Logger LOG = LoggerFactory.getLogger(ItemsInContainersGroup.class);
     
     // FIXME Can't set default value in fiel declaration, because super sets it before this class initializes the field values!
     @SetFromFlag
 
     private Group containerGroup;
     
     private final SensorEventListener<ObjecteventHandler = new SensorEventListener<Object>() {
         @Override
         public void onEvent(SensorEvent<Objectevent) {
             Entity source = event.getSource();
             Object value = event.getValue();
             Sensor sensor = event.getSensor();
             
             if (sensor.equals(.)) {
                     onContainerAdded((Entityvalue);
             } else if (sensor.equals(.)) {
                 onContainerRemoved((Entityvalue);
             } else if (sensor.equals(.)) {
                 onItemMoved((Movable)source, (BalanceableContainer<?>) value);
             } else {
                 throw new IllegalStateException("Unhandled event type "+sensor+": "+event);
             }
         }
     };
 
     public ItemsInContainersGroup() {
         this(MutableMap.of(), null);
     }
     public ItemsInContainersGroup(Map props) {
         this(propsnull);
     }
     public ItemsInContainersGroup(Entity parent) {
         this(MutableMap.of(), parent);
     }
     public ItemsInContainersGroup(Map propsEntity parent) {
         super(propsparent);
         setEntityFilter(new Predicate<Entity>() {
             @Override public boolean apply(Entity e) {
                 return acceptsEntity(e);
             }});
         if ( == null = Predicates.alwaysTrue();
     }
 
     protected boolean acceptsEntity(Entity e) {
         if (e instanceof Movable) {
             return acceptsItem((Movable)e, ((Movable)e).getAttribute(.));
         }
         return false;
     }
 
     boolean acceptsItem(Movable eBalanceableContainer c) {
         return ( != null && c != null) ? .apply(e) && .hasMember(c) : false;
     }
 
     public void setContainers(Group containerGroup) {
         this. = containerGroup;
         subscribe(containerGroup.);
         subscribe(containerGroup.);
         subscribe(null.);
         
         if (.isTraceEnabled()) .trace("{} scanning entities on container group set"this);
         rescanEntities();
    }
    
    private void onContainerAdded(Entity newContainer) {
        if (.isTraceEnabled()) .trace("{} rescanning entities on container {} added"thisnewContainer);
        rescanEntities();
    }
    
    private void onContainerRemoved(Entity oldContainer) {
        if (.isTraceEnabled()) .trace("{} rescanning entities on container {} removed"thisoldContainer);
        rescanEntities();
    }
    
    protected void onEntityAdded(Entity item) {
        if (acceptsEntity(item)) {
            if (.isDebugEnabled()) .debug("{} adding new item {}"thisitem);
            addMember(item);
        }
    }
    
    protected void onEntityRemoved(Entity item) {
        if (removeMember(item)) {
            if (.isDebugEnabled()) .debug("{} removing deleted item {}"thisitem);
        }
    }
    
    private void onItemMoved(Movable itemBalanceableContainer container) {
        if (.isTraceEnabled()) .trace("{} processing moved item {}, to container {}"new Object[] {thisitemcontainer});
        if (hasMember(item)) {
            if (!acceptsItem(itemcontainer)) {
                if (.isDebugEnabled()) .debug("{} removing moved item {} from group, as new container {} is not a member"new Object[] {thisitemcontainer});
                removeMember(item);
            }
        } else {
            if (acceptsItem(itemcontainer)) {
                if (.isDebugEnabled()) .debug("{} adding moved item {} to group, as new container {} is a member"new Object[] {thisitemcontainer});
                addMember(item);
            }
        }
    }
New to GrepCode? Check out our FAQ X