LoadBalancingState.java :  » Media » projectopenfarm » openfarmmanager » manager » workflow » states » Java Open Source

Java Open Source » Media » projectopenfarm 
projectopenfarm » openfarmmanager » manager » workflow » states » LoadBalancingState.java
/**
 * Copyright (c) 2009 - 2010 - OpenFARM
 * Licence: MIT
 *
 * @Author Filipe Martins
 * @Date 11-May-2010 
 */
package openfarmmanager.manager.workflow.states;

import java.util.ArrayList;
import java.util.List;

import openfarmmanager.beans.idx.InterpreterDefinitionBean;
import openfarmmanager.manager.workflow.MaterialWorkflow;

public class LoadBalancingState implements IWorkFlow
{
  private MaterialWorkflow materialWorkflow;
  
  public LoadBalancingState(MaterialWorkflow materialWorkflow)
  {
    this.materialWorkflow = materialWorkflow;
  }
  
  @Override
  public void processMaterial()
  {
    this.distributeMaterialPerInterpreter();
  }

  @Override
  public void nextState()
  {
    this.materialWorkflow.setState(null);
  }
  
  /**Distribute the list of videos throughout the interpreters that can run them*/  
  public void distributeMaterialPerInterpreter()
  {
    for(List<String> materialList : this.materialWorkflow.getVideosInterpretersMap().keySet())
    {
      List<InterpreterDefinitionBean> interpretersList =  this.materialWorkflow.getVideosInterpretersMap().get(materialList);      
      int index = interpretersList.size()-1;      
      
      for(String material : materialList)
      {  
        if(index<0)
          index = interpretersList.size()-1;
        
        InterpreterDefinitionBean interpreter = interpretersList.get(index);
        
        if(!this.materialWorkflow.getMaterialPerInterpreterMap().containsKey(interpreter))
        {
          List<String> interpreterMaterialList = new ArrayList<String>();
          interpreterMaterialList.add(material);
          this.materialWorkflow.getMaterialPerInterpreterMap().put(interpreter, interpreterMaterialList);
          
        }else
        {
          this.materialWorkflow.getMaterialPerInterpreterMap().get(interpreter).add(material);
        }
        
        index--;        
      }      
    }    
  }  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.