title: PriorityQueue not 'polling' priority object or I have logical error in compareTo method
body: <p>Either my understanding of how Priority Queue works is incorrect or I have a logical error in my <code>compareTo</code> method which overrides the <code>Comparable</code> interface. I am trying to assign runways with the highest priority to airplanes that are landing or taking off. In the following scenario, there are four flights and four runways are available. So, each flight lands or departs at its scheduled time.</p>
  
  <p>The format is: </p>
  
  <pre><code>scheduledTime|eventType|flightIdentifier|runwayUsed
  00:01|ARRIVAL|A001|null
  00:00|DEPARTURE|D001|null
  00:01|DEPARTURE|D002|null
  00:00|DEPARTURE|D003|null
  </code></pre>
  
  <p>After assigning flights to priority runways('polling' priority runways from priority queue of runway objects), the result is</p>
  
  <pre><code> 00:01|ARRIVAL|A001| 1
   00:00|DEPARTURE|D001| 4 
   00:01|DEPARTURE|D002| 2 
   00:00|DEPARTURE|D003| 3
  </code></pre>
  
  <p>However, assuming I don't have a logical error in my <code>compareTo</code> method, the output should be:</p>
  
  <pre><code>00:01|ARRIVAL|A001| 1
  00:00|DEPARTURE|D001| 2
  00:01|DEPARTURE|D002| 3
  00:00|DEPARTURE|D003| 4
  </code></pre>
  
  <p>Here is the Runway class which contains the compareTo method:</p>
  
  <pre><code>public class Runway implements Comparable&lt;Runway&gt;{
  
  //instance or class variables
  
  private LocalTime whenAvailable; //when the runway is available
  private LocalTime actualTime; //actual time the plane arrived or departed
  private List&lt;Flight&gt; flights; //the flights that have been assigned to the runway
  private Integer runwayNumber; // the number of the runway. for ex., 1 = Runway 1.
  private LocalTime previousSchedTime; //the most recent previous time that the runway was scheduled for arrival or departure. This is just used
  // for testing purposes.
  
  /**
   * Constructor
   */
  protected Runway() {
      whenAvailable = null;
      flights = new ArrayList&lt;Flight&gt;();
  } 
  
  
  
  /**
   * Determine if the runway is available
   * @param currentTime The scheduled time of the flight
   * @return true if the runway is available or false if it is not
   */
  protected boolean isAvailable(LocalTime currentTime) {
      if(currentTime == null) {
          throw new IllegalArgumentException();
      } 
  
      return whenAvailable == null ||  !currentTime.isBefore(whenAvailable); // currentTime &gt;= whenAvailable
  
  } 
  
  
  
  /**
   * Assign flight to the runway, i.e., set the actual time a flight uses the runway and the time ruwnay will be available again
   * @param flight The flight being assigned to the runway
   * @param scheduledTime The scheduled time of the flight
   * @param reserveTime The arrival or departure reserve times of the flight
   */
  protected void assignFlight(Flight flight, LocalTime scheduledTime, int reserveTime) {
  
      //if the runway is available
      if(isAvailable(scheduledTime)) {
  
  
          //Set the actual time of the flight and when the runway will be available again
  
          //if the runway is null and available
          if(whenAvailable == null) { 
              actualTime = scheduledTime;
              whenAvailable = scheduledTime.plusMinutes(reserveTime);
          } 
  
          //if runway is available and the scheduled time of flight is equal to when the runway is available
          else if(scheduledTime == whenAvailable || scheduledTime.isBefore(whenAvailable)) {
              actualTime = whenAvailable;
              whenAvailable = actualTime.plusMinutes(reserveTime);
          } 
  
  
  
  
          else { //if scheduledTime &gt; whenAvailable 
  
  
              actualTime = scheduledTime;
              whenAvailable = actualTime.plusMinutes(reserveTime);
  
  
  
          } 
      } 
  
      //if the runway is not available aka currentTime &lt; whenAvailable
      else {
          actualTime = whenAvailable;
          whenAvailable = actualTime.plusMinutes(reserveTime);
      } 
  
      flights.add(flight);
      previousSchedTime = scheduledTime; //update previousSchedTime to scheduledTime
  
  
  
  } 
  
  
  
  /**
   * 
   * @return acutialTime the runway was used by flight
   */
  protected LocalTime getActualTimeRunway() {
      return actualTime;
  } 
  
  /**
   * 
   * @return the time the runway is available
   */
  protected LocalTime getWhenAvailable() {
      return whenAvailable;
  } 
  
  
  /**
   * 
   * @return List of all the flights that used the runway 
   */
  protected List&lt;Flight&gt; getFlights(){
  
      List&lt;Flight&gt; tmpList = new LinkedList&lt;Flight&gt;();
      for(Flight f : flights) {
          tmpList.add(f);
      } 
      return tmpList;
  } 
  
  
  /**
   * 
   * @param runwayNumber set the number of the runway
   */
  protected void setRunwayNumber(int runwayNumber) {
      if(runwayNumber &lt; 1) {
          throw new IllegalArgumentException();
      } //end of if
      this.runwayNumber = runwayNumber;
  } 
  
  /**
   * 
   * @return the number of the runway
   */
  protected Integer getRunwayNumber() {
      return runwayNumber;
  } 
  
  
  /**
   * 
   * @return the most recent previous time that the runway was scheduled for arrival or departure. This is used
  // as a condition in the assignFlight method below
   */
  protected LocalTime getPreviousSchedTime() {
      return previousSchedTime;
  } 
  
  /**
   * NOTE: this is intended to only be used for testing in other classes when used with reflection
   * @param previousSchedTime sets the previousScedTime
   */
  private void setPreviousSchedTime(LocalTime previousSchedTime) {
      this.previousSchedTime = previousSchedTime;
  } 
  
  
  /**
   * Override compareTo method of Comparable interface
   * Set priority of runway instance when compared other runway instances
   */
  @Override
  public int compareTo(Runway other) {
      if(this.getWhenAvailable() == null &amp;&amp; other.getWhenAvailable() == null) {
          return 0;
      } 
      else if(this.getWhenAvailable() == null &amp;&amp; other.getWhenAvailable() != null) {
          return -1;
      } 
      else if(this.getWhenAvailable() !=null &amp;&amp; other.getWhenAvailable() == null) {
          return 1; 
      } 
      else if(this.getWhenAvailable() !=null &amp;&amp; other.getWhenAvailable() != null) {
          if(this.getWhenAvailable().equals(other.getWhenAvailable())) {
              return 0;
          } 
          else if(this.getWhenAvailable().isBefore(other.getWhenAvailable())) {
              return -1;
          } 
          else if(this.getWhenAvailable().isAfter(other.getWhenAvailable())) {
              return 1;
          } 
      } 
  
      return 0;
  } 
  
  /**
   * Intended use is only for JUnit testing when used with reflection
   * @param wA set whenAvailable time
   */
  private void setWhenAvailable(LocalTime wA) {
      this.whenAvailable = wA;
  } 
  </code></pre>
  
  <p>} //end of Runway class ----------------------------------------</p>
  
  <p>Below is a class that implements the compareTo method through Priority Queues:</p>
  
  <pre><code>public class stackExchange {
  
  
  public static void main(String[] args) {
  
      Flight f1 = new Flight("00:01", "ARRIVAL","A001");
      Flight f2 = new Flight("00:00", "DEPARTURE","D001");
      Flight f3 = new Flight("00:01", "DEPARTURE","D002");
      Flight f4 = new Flight("00:00", "DEPARTURE","D003");
  
  
      PriorityQueue&lt;Flight&gt; flightsPQ = new PriorityQueue&lt;Flight&gt;();
      flightsPQ.add(f1);
      flightsPQ.add(f2);
      flightsPQ.add(f3);
      flightsPQ.add(f4);
  
      Runway r1 = new Runway();
      r1.setRunwayNumber(1);
  
      Runway r2 = new Runway();
      r2.setRunwayNumber(2);
  
      Runway r3 = new Runway();
      r3.setRunwayNumber(3);
  
      Runway r4 = new Runway();
      r4.setRunwayNumber(4);
  
      PriorityQueue&lt;Runway&gt; runwaysPQ = new PriorityQueue&lt;Runway&gt;();
      runwaysPQ.add(r1);
      runwaysPQ.add(r2);
      runwaysPQ.add(r3);
      runwaysPQ.add(r4);
  
      while(!flightsPQ.isEmpty()) {
          Flight tmpFlight = flightsPQ.poll(); //remove priority flight from flightsPQ
  
          Runway tmpRunway = runwaysPQ.poll(); //remove priority runway from runwaysPQ
          tmpRunway.assignFlight(tmpFlight, tmpFlight.getScheduledTime(), tmpFlight.getReserveTime()); //assign the priority flight to the runwy
          tmpFlight.setActualTime(tmpRunway.getActualTimeRunway()); //set the actual time the flight was used
          tmpFlight.setRunwayUsed(tmpRunway); //tell the flight which runway it used
  
          //print out the flight data that used the runway and the number of the runway used
          //format: scheduledTime of flight | eventType | identifier | actualTime the flight used the runway | the number of the runway used (used to distinguish runway over
           // other runways
          System.out.println(tmpFlight.getScheduledTime() + "|" + tmpFlight.getEvent() + "|" + tmpFlight.getIdent() + "|" + tmpFlight.getActualTime()
                  + "|" + tmpFlight.getRunwayUsed().getRunwayNumber());
  
          runwaysPQ.add(tmpRunway); //add the runway back into runwaysPQ
      } 
  
  
  
  
  
  } //end of main
  </code></pre>
  
  <p>} //end of stackExchange class</p>