import java.util.Vector; public class Accelerator { // Field private int ACCELERATOR_SIZE; private Vector pairs ; private float velocity; private int maxVelocity; private boolean accelerated = false; private boolean connected = false; private boolean extract = false; private int m_input; private int m_output; private Accelerator m_acc; // Methods public void inject(Bunch bunch){ boolean locFilled = false; for(int i = 0 ; i < pairs.size() ; i++){ int injectionLocation = ((Pair)(pairs.elementAt(i))).location(); if(injectionLocation==m_input){ locFilled = true ; } } if(locFilled){ // Do nothing as we're not worried about merging Bunches // in this version. }else{ pairs.addElement(new Pair(bunch,m_input)); } } public void update(){ updateVelocity(); updateLocations(); } public float velocity(){ return velocity/(float)maxVelocity; } public void accelerate(){ accelerated = true; } public void stopAccelerate(){ accelerated = false ; velocity = 1F; } public void connectTo(Accelerator acc){ m_acc = acc; connected = true; } public void setTransfer(boolean value){ if(m_acc!=null)extract = value; } public boolean isTransfering(){ return extract; } public boolean isAccelerating(){ return accelerated; } public Vector contents( ) { return pairs; } public int size(){ return ACCELERATOR_SIZE; } private void updateVelocity(){ if(accelerated){ if((int)velocity < maxVelocity){ velocity += 0.05F; }else{ velocity = (float)maxVelocity; } } } private void updateLocations(){ int spaces = accelerated ? (int)velocity : 1; while(spaces >= ACCELERATOR_SIZE){ spaces -= ACCELERATOR_SIZE; } for(int i = 0 ; i < pairs.size() ; i++){ if(((Pair)(pairs.elementAt(i))).bunch().type().equalsIgnoreCase("electrons")){ moveOneWay(spaces,i); }else{ moveOtherWay(spaces,i); } } } private void moveOneWay(int spaces, int pairIndex){ int tempA = ((Pair)(pairs.elementAt(pairIndex))).location(); int tempB = tempA + spaces; if(extract && ((tempA<=m_output && tempB>m_output)||(tempA>m_output && spaces>=m_output))){ m_acc.inject(((Pair)(pairs.elementAt(pairIndex))).bunch()); pairs.removeElementAt(pairIndex); }else{ tempA=tempB>=ACCELERATOR_SIZE?tempB-ACCELERATOR_SIZE:tempB; ((Pair)(pairs.elementAt(pairIndex))).location(tempA); } } /*private void moveOtherWay(int spaces, int pairIndex){ int tempA = ((Pair)(pairs.elementAt(pairIndex))).location(); int tempB = tempA - spaces; if(extract && ((tempA>=m_output && tempB=tempA))){ m_acc.inject(((Pair)(pairs.elementAt(pairIndex))).bunch()); pairs.removeElementAt(pairIndex); }else{ tempA=tempB<=0?ACCELERATOR_SIZE+tempB-1:tempB; ((Pair)(pairs.elementAt(pairIndex))).location(tempA); } }*/ private void moveOtherWay(int spaces, int pairIndex){ int tempA = ((Pair)(pairs.elementAt(pairIndex))).location(); int tempB = tempA - spaces; int tempC = ACCELERATOR_SIZE - m_output + tempA; if(extract && ((tempA>=m_output && tempBtempC))){ m_acc.inject(((Pair)(pairs.elementAt(pairIndex))).bunch()); pairs.removeElementAt(pairIndex); }else{ tempA=tempB<0?ACCELERATOR_SIZE+tempB:tempB; ((Pair)(pairs.elementAt(pairIndex))).location(tempA); } } // Constructors public Accelerator(int size, int input,int output) { m_input = input; m_output = output; ACCELERATOR_SIZE = size; pairs = new Vector(size,1); velocity = 1F; maxVelocity = size/10+3; } public Accelerator(int size){ this(size,0,size-1); } public Accelerator(){ this(100); } }