Skip to content
Snippets Groups Projects
Droplet.java 2.28 KiB
Newer Older
  • Learn to ignore specific revisions
  • import java.util.List;
    import java.util.ArrayList;
    
    
    public class Droplet {
      private Position position;
      private DropletType type;
    
      public Droplet(DropletType type, Position position) {
        this.position = position;
        this.type = type;
    
        this.position.getChannel().addDroplet(this);
    
      }
    
      public void move() {
        position.increment(this);
      }
    
      public DropletType getType() {
        return type;
      }
    
    
      /* 
       * Unify the position using the faster channel
       */
      public int getNormalizedSteps() {
        Channel chan = this.position.getChannel();
        int pSteps = chan.getPSteps();
        int hSteps = chan.getHSteps();
        float factor = 1;
    
        if (pSteps < hSteps && this.type == DropletType.HEADER) {
          factor = (float)pSteps / hSteps;
        } else if (pSteps > hSteps && this.type == DropletType.PAYLOAD) {
          factor = (float)hSteps / pSteps;
        } 
        return (int)(this.position.getSteps() * factor);
      }
    
    
      public Position getPosition() {
        return position;
      }
    
      public boolean coalesce() {
    
        boolean coalesce = false;
    
        if (this.position.getChannel() instanceof Module ||
            this.position.getChannel() instanceof Sink ||
            this.position.getChannel() instanceof Pump) {
          coalesce = false;
        } else {
          List<Droplet> drlist = 
            this.position.getChannel().getNormalizedSortedDropletList();
          // Check if we are the last droplet in the channel
          if (drlist.indexOf(this) + 1 == drlist.size()) {
            List<Channel> childChanList = 
              this.position.getChannel().getChildrenList();
            
            // Check if the following channel is a sink
            if (childChanList.size() == 1 && 
                childChanList.get(0) instanceof Sink) {
              coalesce = false;
            } else { 
              // Next is no sink
              for (Channel chan : childChanList) {
                List<Droplet> childDrList = chan.getNormalizedSortedDropletList();
                //if (!childDrList.isEmpty() && childDrList.get(0). 
                
              }
            }
          }
        }
        
        return coalesce;
    
      public void setPosition(Position position) {
        this.position.getChannel().removeDroplet(this);
        this.position = position;
        this.position.getChannel().addDroplet(this);
      }
    
      public boolean isInSink() {
        return this.position.getChannel() instanceof Sink; 
      }