package atmsdriver.model; import java.util.Comparator; /** * Compare two stations by route and postmile. * Used by Highways.toJson() so json is listed by route then postmile * @author jdalbey * @version 4/2/2019 */ public final class StationComparator implements Comparator { /** * Compare two stations by route and postmile. */ @Override public int compare(Object a, Object b) { // check for identity if (a == b) { return 0; } // check that Object is of type Station, if not throw exception if (!(a instanceof Station && b instanceof Station)) { throw new ClassCastException("A Station object expected."); } Station statA = (Station) a; Station statB = (Station) b; // compare by direction field first int i = statA.direction.compareTo(statB.direction); if (i != 0) return i; // get difference of postmile values double val = statA.postmile - statB.postmile; // set appropriate comparable return value int retval = 0; if (val > 0) { retval = 1; } else if (val < 0) { retval = -1; } return retval; } }