- Timestamp:
- 2021-09-11T17:00:13+02:00 (3 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/gpx/GpxData.java
r17845 r18207 19 19 import java.util.Objects; 20 20 import java.util.Optional; 21 import java.util.OptionalLong; 21 22 import java.util.Set; 22 23 import java.util.stream.Collectors; … … 33 34 import org.openstreetmap.josm.tools.ListenerList; 34 35 import org.openstreetmap.josm.tools.ListeningCollection; 36 import org.openstreetmap.josm.tools.Utils; 35 37 import org.openstreetmap.josm.tools.date.Interval; 36 38 … … 386 388 387 389 /** 388 * Get all tracks contained in this data set .390 * Get all tracks contained in this data set, without any guaranteed order. 389 391 * @return The tracks. 390 392 */ 391 393 public synchronized Collection<IGpxTrack> getTracks() { 392 394 return Collections.unmodifiableCollection(privateTracks); 395 } 396 397 /** 398 * Get all tracks contained in this data set, ordered chronologically. 399 * @return The tracks in chronological order. 400 * @since 18207 401 */ 402 public synchronized List<IGpxTrack> getOrderedTracks() { 403 return privateTracks.stream().sorted((t1, t2) -> { 404 boolean t1empty = Utils.isEmpty(t1.getSegments()); 405 boolean t2empty = Utils.isEmpty(t2.getSegments()); 406 if (t1empty && t2empty) { 407 return 0; 408 } else if (t1empty && !t2empty) { 409 return -1; 410 } else if (!t1empty && t2empty) { 411 return 1; 412 } else { 413 OptionalLong i1 = getTrackFirstWaypointMin(t1); 414 OptionalLong i2 = getTrackFirstWaypointMin(t2); 415 boolean i1absent = !i1.isPresent(); 416 boolean i2absent = !i2.isPresent(); 417 if (i1absent && i2absent) { 418 return 0; 419 } else if (i1absent && !i2absent) { 420 return 1; 421 } else if (!i1absent && i2absent) { 422 return -1; 423 } else { 424 return Long.compare(i1.getAsLong(), i2.getAsLong()); 425 } 426 } 427 }).collect(Collectors.toList()); 428 } 429 430 private static OptionalLong getTrackFirstWaypointMin(IGpxTrack track) { 431 return track.getSegments().stream().map(IGpxTrackSegment::getWayPoints) 432 .filter(Objects::nonNull).flatMap(Collection::stream) 433 .mapToLong(WayPoint::getTimeInMillis).min(); 393 434 } 394 435 -
trunk/src/org/openstreetmap/josm/io/GpxWriter.java
r18206 r18207 266 266 267 267 private void writeTracks() { 268 for (IGpxTrack trk : data.get Tracks()) {268 for (IGpxTrack trk : data.getOrderedTracks()) { 269 269 openln("trk"); 270 270 writeAttr(trk, RTE_TRK_KEYS); -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r18072 r18207 650 650 public static <T> List<T> toUnmodifiableList(Collection<T> collection) { 651 651 // Java 9: use List.of(...) 652 if ( collection == null || collection.isEmpty()) {652 if (isEmpty(collection)) { 653 653 return Collections.emptyList(); 654 654 } else if (collection.size() == 1) { … … 672 672 @SuppressWarnings("unchecked") 673 673 public static <K, V> Map<K, V> toUnmodifiableMap(Map<K, V> map) { 674 if ( map == null || map.isEmpty()) {674 if (isEmpty(map)) { 675 675 return Collections.emptyMap(); 676 676 } else if (map.size() == 1) { … … 689 689 690 690 /** 691 * Determines if a collection is null or empty. 692 * @param collection collection 693 * @return {@code true} if collection is null or empty 694 * @since 18207 695 */ 696 public static boolean isEmpty(Collection<?> collection) { 697 return collection == null || collection.isEmpty(); 698 } 699 700 /** 701 * Determines if a map is null or empty. 702 * @param map map 703 * @return {@code true} if map is null or empty 704 * @since 18207 705 */ 706 public static boolean isEmpty(Map<?, ?> map) { 707 return map == null || map.isEmpty(); 708 } 709 710 /** 711 * Determines if a string is null or empty. 712 * @param string string 713 * @return {@code true} if string is null or empty 714 * @since 18207 715 */ 716 public static boolean isEmpty(String string) { 717 return string == null || string.isEmpty(); 718 } 719 720 /** 691 721 * Returns the first not empty string in the given candidates, otherwise the default string. 692 722 * @param defaultString default string returned if all candidates would be empty if stripped … … 737 767 */ 738 768 public static String strip(final String str, final String skipChars) { 739 if ( str == null || str.isEmpty()) {769 if (isEmpty(str)) { 740 770 return str; 741 771 } … … 774 804 */ 775 805 public static String removeWhiteSpaces(String s) { 776 if ( s == null || s.isEmpty()) {806 if (isEmpty(s)) { 777 807 return s; 778 808 }
Note:
See TracChangeset
for help on using the changeset viewer.