Ignore:
Timestamp:
2014-05-14T02:16:44+02:00 (12 years ago)
Author:
Don-vip
Message:

code refactoring/cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitiveComparator.java

    r7005 r7120  
    88import org.openstreetmap.josm.gui.DefaultNameFormatter;
    99
    10 /** Comparator, comparing by type and objects display names */
     10/**
     11 * Comparator, comparing pritimives by:<ul>
     12 * <li>type and ids in "quick" mode</li>
     13 * <li>type and objects display names instead</li>
     14 * </ul>
     15 * @since 4113
     16 */
    1117public class OsmPrimitiveComparator implements Comparator<OsmPrimitive> {
    12     private final Map<OsmPrimitive, String> cache= new HashMap<>();
    13     private final DefaultNameFormatter df = DefaultNameFormatter.getInstance();
    14     public boolean relationsFirst = false;
     18    private final Map<OsmPrimitive, String> cache = new HashMap<>();
     19    private final boolean relationsFirst;
     20    private final boolean quick;
     21
     22    /**
     23     * Constructs a new {@code OsmPrimitiveComparator}.
     24     */
     25    public OsmPrimitiveComparator() {
     26        this(false, false);
     27    }
     28
     29    /**
     30     * Constructs a new {@code OsmPrimitiveComparator}.
     31     * @param quick if {@code true}, sorts by type and ids (fast), otherwise sort by type and display names (slower)
     32     * @param relationsFirst if {@code true}, always list relations first
     33     */
     34    public OsmPrimitiveComparator(boolean quick, boolean relationsFirst) {
     35        this.quick = quick;
     36        this.relationsFirst = relationsFirst;
     37    }
    1538
    1639    private String cachedName(OsmPrimitive p) {
    1740        String name = cache.get(p);
    1841        if (name == null) {
    19             name = p.getDisplayName(df);
     42            name = p.getDisplayName(DefaultNameFormatter.getInstance());
    2043            cache.put(p, name);
    2144        }
     
    3659    }
    3760
     61    private static int compareId(OsmPrimitive a, OsmPrimitive b) {
     62        long idA = a.getUniqueId();
     63        long idB = b.getUniqueId();
     64        if (idA < idB) return -1;
     65        if (idA > idB) return 1;
     66        return 0;
     67    }
     68
    3869    private int compareType(OsmPrimitive a, OsmPrimitive b) {
    39         if(relationsFirst) {
     70        if (relationsFirst) {
    4071            // show relations before ways, then nodes
    4172            if (a.getType().equals(OsmPrimitiveType.RELATION)) return -1;
     
    5889    public int compare(OsmPrimitive a, OsmPrimitive b) {
    5990        if (a.getType().equals(b.getType()))
    60             return compareName(a, b);
     91            return quick ? compareId(a, b) : compareName(a, b);
    6192        return compareType(a, b);
    6293    }
Note: See TracChangeset for help on using the changeset viewer.