001package jmri.util;
002
003import java.util.Comparator;
004import jmri.LocoAddress;
005
006/**
007 * Comparator for LocoAddress objects.
008 * <p>
009 * Compares objects based on protocol and then address.
010 *
011 * @author Paul Bender Copyright (C) 2015
012 */
013public class LocoAddressComparator implements Comparator<LocoAddress> {
014
015    public LocoAddressComparator() {
016    }
017
018    @Override
019    public int compare(LocoAddress l1, LocoAddress l2) {
020         if( l1.getProtocol() == l2.getProtocol() ){
021             // protocol is the same, compare the number fields
022             return Integer.signum(l1.getNumber() - l2.getNumber());
023         } else {
024             return Integer.signum((l1.getProtocol().getShortName().compareTo(l2.getProtocol().getShortName())));
025         }
026    }
027}