package backtype.storm.scheduler;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SchedulerAssignmentImpl implements SchedulerAssignment {
    /**
     * topology-id this assignment is for.
     */
    String topologyId;
    /**
     * assignment detail, a mapping from executor to <code>WorkerSlot</code>
     */
    Map<ExecutorDetails, WorkerSlot> executorToSlot;
    
    public SchedulerAssignmentImpl(String topologyId, Map<ExecutorDetails, WorkerSlot> executorToSlots) {
        this.topologyId = topologyId;
        this.executorToSlot = new HashMap<ExecutorDetails, WorkerSlot>(0);
        if (executorToSlots != null) {
            this.executorToSlot.putAll(executorToSlots);
        }
    }
    
    /**
     * Assign the slot to executors.
     * @param slot
     * @param executors
     */
    public void assign(WorkerSlot slot, Collection<ExecutorDetails> executors) {
        for (ExecutorDetails executor : executors) {
            this.executorToSlot.put(executor, slot);
        }
    }
 
    /**
     * Unassign the assignment for executor.
     */
    public void unassignByExecutor(ExecutorDetails executor) {
        this.executorToSlot.remove(executor);
    }
    
    public void unassignByExecutors(Collection<ExecutorDetails> executors) {
        for (ExecutorDetails executor : executors) {
            this.unassignByExecutor(executor);
        }
    }
    
    /**
     * Release the slot occupied by this assignment.
     * @param slot
     */
    public void unassignBySlot(WorkerSlot slot) {
        List<ExecutorDetails> executors = new ArrayList<ExecutorDetails>();
        for (ExecutorDetails executor : this.executorToSlot.keySet()) {
            WorkerSlot ws = this.executorToSlot.get(executor);
            if (ws.equals(slot)) {
                executors.add(executor);
            }
        }
        
        // remove
        for (ExecutorDetails executor : executors) {
            this.executorToSlot.remove(executor);
        }
    }

    /**
     * Does this slot occupied by this assignment?
     * @param slot
     * @return
     */
    public boolean isSlotOccupied(WorkerSlot slot) {
        return this.executorToSlot.containsValue(slot);
    }

    public boolean isExecutorAssigned(ExecutorDetails executor) {
        return this.executorToSlot.containsKey(executor);
    }
    
    public String getTopologyId() {
        return this.topologyId;
    }

    public Map<ExecutorDetails, WorkerSlot> getExecutorToSlot() {
        return this.executorToSlot;
    }

    /**
     * Return the executors covered by this assignments
     * @return
     */
    public Set<ExecutorDetails> getExecutors() {
        return this.executorToSlot.keySet();
    }
}
@@ -1158,397 +1158,8 @@
  %7D%0D%0A
- %0D%0A    /**%0D%0A     * Unassign the assignment for executor.%0D%0A     */%0D%0A    public void unassignByExecutor(ExecutorDetails executor) %7B%0D%0A        this.executorToSlot.remove(executor);%0D%0A    %7D%0D%0A    %0D%0A    public void unassignByExecutors(Collection%3CExecutorDetails%3E executors) %7B%0D%0A        for (ExecutorDetails executor : executors) %7B%0D%0A            this.unassignByExecutor(executor);%0D%0A        %7D%0D%0A    %7D%0D%0A
     

remove unassignByExecutor(s)