Changeset 7120 in josm for trunk


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

code refactoring/cleanup

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

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

    r7005 r7120  
    7676            }
    7777        }
    78         OsmPrimitiveComparator c = new OsmPrimitiveComparator();
    79         c.relationsFirst = true;
     78        OsmPrimitiveComparator c = new OsmPrimitiveComparator(false, true);
    8079        Collections.sort(toDelete, c);
    8180        Collections.sort(toAdd, c);
  • 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    }
  • trunk/src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java

    r7021 r7120  
    1515import java.util.Collection;
    1616import java.util.Collections;
    17 import java.util.Comparator;
    1817import java.util.HashSet;
    1918import java.util.Iterator;
     
    4443import org.openstreetmap.josm.data.osm.OsmPrimitive;
    4544import org.openstreetmap.josm.data.osm.OsmPrimitiveComparator;
    46 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    4745import org.openstreetmap.josm.data.osm.Relation;
    4846import org.openstreetmap.josm.data.osm.Way;
     
    585583         */
    586584        public void sort() {
    587             if (this.selection.size()>Main.pref.getInteger("selection.no_sort_above",100000)) return;
    588             if (this.selection.size()>Main.pref.getInteger("selection.fast_sort_above",10000)) {
    589                 Collections.sort(this.selection, new OsmPrimitiveQuickComparator());
    590             } else {
    591                 Collections.sort(this.selection, new OsmPrimitiveComparator());
     585            if (this.selection.size() <= Main.pref.getInteger("selection.no_sort_above", 100000)) {
     586                boolean quick = this.selection.size() > Main.pref.getInteger("selection.fast_sort_above", 10000);
     587                Collections.sort(this.selection, new OsmPrimitiveComparator(quick, false));
    592588            }
    593589        }
     
    778774        }
    779775    }
    780 
    781     /** Quicker comparator, comparing just by type and ID's */
    782     private static class OsmPrimitiveQuickComparator implements Comparator<OsmPrimitive> {
    783 
    784         private int compareId(OsmPrimitive a, OsmPrimitive b) {
    785             long id_a=a.getUniqueId();
    786             long id_b=b.getUniqueId();
    787             if (id_a<id_b) return -1;
    788             if (id_a>id_b) return 1;
    789             return 0;
    790         }
    791 
    792         private int compareType(OsmPrimitive a, OsmPrimitive b) {
    793             // show ways before relations, then nodes
    794             if (a.getType().equals(OsmPrimitiveType.WAY)) return -1;
    795             if (a.getType().equals(OsmPrimitiveType.NODE)) return 1;
    796             // a is a relation
    797             if (b.getType().equals(OsmPrimitiveType.WAY)) return 1;
    798             // b is a node
    799             return -1;
    800         }
    801 
    802         @Override
    803         public int compare(OsmPrimitive a, OsmPrimitive b) {
    804             if (a.getType().equals(b.getType()))
    805                 return compareId(a, b);
    806             return compareType(a, b);
    807         }
    808     }
    809 
    810776}
Note: See TracChangeset for help on using the changeset viewer.