When trying to give generic objects priority in a Priority Queue what can I use to compare them? Can I define and use an overridden CompareTo method from the Comparable interface or an overridden Compare method from the Comparator interface? Or could I use one or the other? Thanks
Here are the instance variables, constructor of the class, and the current compareTo method.
private LocalTime scheduledTime; //the scheduled time of the flight
private Event.EventType eventType; //the event type of the flight (arrival or departure)
private String identifier; // the identifier of the flight
private LocalTime actualTime; //the actual time the flight uses a runway
private Runway runwayUsed; //the runway the flight used to arrive or depart
private int reserveTime; // time the flight uses to reserve a runway
private LocalTime runwayAvailableTime;
/**
* Constructor
* @param scheduledTime the scheduled time of the flight
* @param eventType the event of the flight (arrival or departure)
* @param identifier the identifier of the flight
*/
protected Flight(String scheduledTime, String eventType, String identifier) {
this.scheduledTime = LocalTime.parse(scheduledTime);
this.eventType = EventType.valueOf(eventType);
this.identifier = identifier;
this.actualTime = null;
this.runwayUsed = null;
}
//Here is the compareTo method I am currently using. Should I use compare //from the Comparator interface instead?
@Override
public int compareTo(Event otherFlight) {
Event tmpFlight = (Event) otherFlight;
if(this.scheduledTime.compareTo(tempFlight.getScheduledTime()) == 0) {
if(this.eventType.compareTo(tempFlight.getEvent()) == 0){
return 0;
}
else if(this.eventType.compareTo(tempFlight.getEvent()) > 0){
return 1;
}
else {
return -1;
}
}
else if(this.scheduledTime.compareTo(tempFlight.getScheduledTime()) < 0) {
return -1;
}
else {
return 1;
} }