Changeset 11177 in josm
- Timestamp:
- 2016-10-27T00:32:55+02:00 (8 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/APIDataSet.java
r11176 r11177 21 21 import org.openstreetmap.josm.data.osm.Node; 22 22 import org.openstreetmap.josm.data.osm.OsmPrimitive; 23 import org.openstreetmap.josm.data.osm.OsmPrimitiveComparator; 23 24 import org.openstreetmap.josm.data.osm.PrimitiveId; 24 25 import org.openstreetmap.josm.data.osm.Relation; … … 76 77 } 77 78 } 78 final Comparator<OsmPrimitive> orderingNodesWaysRelations = Comparator.comparingInt(osm -> osm.getType().ordinal());79 final Comparator<OsmPrimitive> byUniqueId = Comparator.comparing(OsmPrimitive::getUniqueId);79 final Comparator<OsmPrimitive> orderingNodesWaysRelations = OsmPrimitiveComparator.orderingNodesWaysRelations(); 80 final Comparator<OsmPrimitive> byUniqueId = OsmPrimitiveComparator.comparingUniqueId(); 80 81 toAdd.sort(orderingNodesWaysRelations.thenComparing(byUniqueId)); 81 82 toUpdate.sort(orderingNodesWaysRelations.thenComparing(byUniqueId)); -
trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitiveComparator.java
r9929 r11177 2 2 package org.openstreetmap.josm.data.osm; 3 3 4 import java.io.Serializable; 4 import static java.util.Comparator.comparing; 5 import static java.util.Comparator.comparingInt; 6 5 7 import java.util.Comparator; 6 8 import java.util.HashMap; 9 import java.util.Map; 10 import java.util.function.Function; 7 11 8 12 import org.openstreetmap.josm.gui.DefaultNameFormatter; 13 import org.openstreetmap.josm.tools.AlphanumComparator; 9 14 10 15 /** 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 * Comparators for comparing primitives. 16 17 */ 17 public class OsmPrimitiveComparator implements Comparator<OsmPrimitive>, Serializable { 18 19 private static final long serialVersionUID = 1L; 20 21 private final HashMap<OsmPrimitive, String> cache = new HashMap<>(); 22 private final boolean relationsFirst; 23 private final boolean quick; 18 public final class OsmPrimitiveComparator { 24 19 25 20 /** 26 * Constructs a new {@code OsmPrimitiveComparator}. 21 * Returns a comparator comparing primitives by their name using {@link DefaultNameFormatter}. 22 * 23 * {@linkplain DefaultNameFormatter#format(OsmPrimitive) Formatted names} are cached. 24 * 25 * @return a comparator comparing primitives by their name using {@link DefaultNameFormatter} 27 26 */ 28 public OsmPrimitiveComparator() { 29 this(false, false); 27 public static Comparator<OsmPrimitive> comparingNames() { 28 final Comparator<String> digitsLast = comparing(str -> Character.isDigit(str.charAt(0)) ? 1 : 0); 29 return comparing(memoize(DefaultNameFormatter.getInstance()::format), 30 digitsLast.thenComparing(AlphanumComparator.getInstance())); 30 31 } 31 32 32 33 /** 33 * Constructs a new {@code OsmPrimitiveComparator}.34 * @param quick if {@code true}, sorts by type and ids (fast), otherwise sort by type and display names (slower)35 * @ param relationsFirst if {@code true}, always list relations first34 * Returns a comparator comparing primitives by their {@linkplain OsmPrimitive#getUniqueId unique id}. 35 * 36 * @return a comparator comparing primitives by their {@linkplain OsmPrimitive#getUniqueId unique id}. 36 37 */ 37 public OsmPrimitiveComparator(boolean quick, boolean relationsFirst) { 38 this.quick = quick; 39 this.relationsFirst = relationsFirst; 38 public static Comparator<OsmPrimitive> comparingUniqueId() { 39 return comparing(OsmPrimitive::getUniqueId); 40 40 } 41 41 42 private String cachedName(OsmPrimitive p) {43 String name = cache.get(p);44 if (name == null) {45 name = p.getDisplayName(DefaultNameFormatter.getInstance());46 cache.put(p, name);47 }48 return name;42 /** 43 * Returns a comparator ordering the primitives by type in the order NODE, WAY, RELATION 44 * 45 * @return a comparator ordering the primitives by type in the order NODE, WAY, RELATION 46 */ 47 public static Comparator<OsmPrimitive> orderingNodesWaysRelations() { 48 return comparingInt(osm -> osm.getType().ordinal()); 49 49 } 50 50 51 private int compareName(OsmPrimitive a, OsmPrimitive b) { 52 String an = cachedName(a); 53 String bn = cachedName(b); 54 // make sure display names starting with digits are the end of the list 55 if (Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0))) 56 return an.compareTo(bn); 57 else if (Character.isDigit(an.charAt(0)) && !Character.isDigit(bn.charAt(0))) 58 return 1; 59 else if (!Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0))) 60 return -1; 61 return an.compareTo(bn); 51 /** 52 * Returns a comparator ordering the primitives by type in the order WAY, RELATION, NODE 53 * 54 * @return a comparator ordering the primitives by type in the order WAY, RELATION, NODE 55 */ 56 public static Comparator<OsmPrimitive> orderingWaysRelationsNodes() { 57 return comparingInt(osm -> { 58 switch (osm.getType()) { 59 case WAY: 60 return 1; 61 case RELATION: 62 return 2; 63 case NODE: 64 return 3; 65 default: 66 throw new IllegalStateException(); 67 } 68 }); 62 69 } 63 70 64 private static int compareId(OsmPrimitive a, OsmPrimitive b) { 65 long idA = a.getUniqueId(); 66 long idB = b.getUniqueId(); 67 if (idA < idB) return -1; 68 if (idA > idB) return 1; 69 return 0; 71 private static <T, R> Function<T, R> memoize(Function<T, R> base) { 72 final Map<T, R> cache = new HashMap<>(); 73 return t -> cache.computeIfAbsent(t, base); 70 74 } 71 75 72 private int compareType(OsmPrimitive a, OsmPrimitive b) { 73 if (relationsFirst) { 74 // show relations before ways, then nodes 75 if (a.getType().equals(OsmPrimitiveType.RELATION)) return -1; 76 if (a.getType().equals(OsmPrimitiveType.NODE)) return 1; 77 // a is a way 78 if (b.getType().equals(OsmPrimitiveType.RELATION)) return 1; 79 // b is a node 80 } else { 81 // show ways before relations, then nodes 82 if (a.getType().equals(OsmPrimitiveType.WAY)) return -1; 83 if (a.getType().equals(OsmPrimitiveType.NODE)) return 1; 84 // a is a relation 85 if (b.getType().equals(OsmPrimitiveType.WAY)) return 1; 86 // b is a node 87 } 88 return -1; 89 } 90 91 @Override 92 public int compare(OsmPrimitive a, OsmPrimitive b) { 93 if (a.getType().equals(b.getType())) 94 return quick ? compareId(a, b) : compareName(a, b); 95 return compareType(a, b); 76 private OsmPrimitiveComparator() { 96 77 } 97 78 } -
trunk/src/org/openstreetmap/josm/gui/dialogs/InspectPrimitiveDialog.java
r10694 r11177 107 107 protected static String buildDataText(OsmDataLayer layer, List<OsmPrimitive> primitives) { 108 108 InspectPrimitiveDataText dt = new InspectPrimitiveDataText(layer); 109 primitives.sort(new OsmPrimitiveComparator()); 110 for (OsmPrimitive o : primitives) { 111 dt.addPrimitive(o); 112 } 109 primitives.stream() 110 .sorted(OsmPrimitiveComparator.orderingWaysRelationsNodes().thenComparing(OsmPrimitiveComparator.comparingNames())) 111 .forEachOrdered(dt::addPrimitive); 113 112 return dt.toString(); 114 113 } -
trunk/src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java
r11100 r11177 659 659 if (selection.size() <= Main.pref.getInteger("selection.no_sort_above", 100_000)) { 660 660 boolean quick = selection.size() > Main.pref.getInteger("selection.fast_sort_above", 10_000); 661 selection.sort(new OsmPrimitiveComparator(quick, false)); 661 selection.sort(OsmPrimitiveComparator.orderingWaysRelationsNodes().thenComparing(quick 662 ? OsmPrimitiveComparator.comparingUniqueId() 663 : OsmPrimitiveComparator.comparingNames())); 662 664 } 663 665 } -
trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java
r11033 r11177 646 646 * (Only works if the data layer has not been saved to and been loaded from an osm file before.) 647 647 */ 648 final List<Way> sortedWays = new ArrayList<>(ways); 649 sortedWays.sort(new OsmPrimitiveComparator(true, false)); // sort by OsmPrimitive#getUniqueId ascending 650 Collections.reverse(sortedWays); // sort by OsmPrimitive#getUniqueId descending 651 for (Way w : sortedWays) { 648 ways.stream() 649 .sorted(OsmPrimitiveComparator.comparingUniqueId().reversed()) 650 .forEachOrdered(w -> { 652 651 if (!w.isUsable()) { 653 continue;652 return; 654 653 } 655 654 Collection<Collection<WayPoint>> trk = new ArrayList<>(); … … 677 676 678 677 gpxData.tracks.add(new ImmutableGpxTrack(trk, trkAttr)); 679 } 678 }); 680 679 } 681 680 -
trunk/test/unit/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialogTest.java
r10962 r11177 34 34 private static String createSearchSetting(DataSet ds, boolean sameType) { 35 35 List<OsmPrimitive> sel = new ArrayList<>(ds.allPrimitives()); 36 Collections.sort(sel, new OsmPrimitiveComparator(true, false));36 Collections.sort(sel, OsmPrimitiveComparator.comparingUniqueId()); 37 37 return PropertiesDialog.createSearchSetting("foo", sel, sameType).text; 38 38 }
Note:
See TracChangeset
for help on using the changeset viewer.