Skip to content
Snippets Groups Projects
Position.java 1.99 KiB
Newer Older
package nloc;

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

public class Position {
  
  private Channel chan;
  private int step;

  public Position (Channel chan, int step) {
    this.chan = chan;
    this.step = step;
  }

  public int getSteps() {
    return step;
  }

  public Channel getChannel() {
    return chan;
  }

  public void increment(Droplet droplet) {
    if (!droplet.isInSink()) {
      int chansteps = 0;
      if (droplet.getType() == DropletType.PAYLOAD) {
        chansteps = chan.getPSteps();
      } else {
        chansteps = chan.getHSteps();
      }
      if (this.step <  chansteps) {
        this.step++;
      } else {
        this.step = 1;
        List<Channel> possibleChannels = chan.getChildrenList();
        if (!possibleChannels.isEmpty() && possibleChannels.size() == 1) {
          this.chan.removeDroplet(droplet);
          this.chan = possibleChannels.get(0);
          this.chan.addDroplet(droplet);
        } else {
          Channel temp = this.chan;
          this.chan = getFollowingChannel(possibleChannels);
          temp.removeDroplet(droplet);
          this.chan.addDroplet(droplet);
        }
      }
    }
  }

  private Channel getFollowingChannel(List<Channel> possibleChannels) {
     * If possibleChannels.size() > 1 we are at a bifurcation.
     * The droplet takes the first Channel that doesn't contain a droplet.
     * The list of channels is already ordered by length and thus the 
     * priority. 
     */
    Channel following = null;
    for (Channel ch : possibleChannels) {
      if (!ch.containsDroplets()) {
        following = ch;
      } 
    }
    if (following == null) {
      ListIterator<Channel> iter = possibleChannels.listIterator();
      following = iter.next();
      while (iter.hasNext()) {
        Channel temp = iter.next();
        if (following.getLastDropletDistance() < temp.getLastDropletDistance()) 
        {
          following = temp;
        }
      }
    }
    return following;
  }
}