Changeset 17847 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2021-05-01T22:59:57+02:00 (3 years ago)
Author:
simon04
Message:

see #20829 - Avoid heap allocations in OsmDataLayer.toGpxData

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r17757 r17847  
    849849     */
    850850    public static String gpxVal(OsmPrimitive prim, String key) {
    851         return Optional.ofNullable(prim.get(GpxConstants.GPX_PREFIX + key)).orElse(prim.get(key));
     851        String val = prim.get(GpxConstants.GPX_PREFIX + key);
     852        return val != null ? val : prim.get(key);
    852853    }
    853854
     
    874875        // Position info
    875876
    876         addDoubleIfPresent(wpt, n, GpxConstants.PT_ELE);
     877        addDoubleIfPresent(wpt, n, GpxConstants.PT_ELE, null);
    877878
    878879        try {
     
    889890        }
    890891
    891         addDoubleIfPresent(wpt, n, GpxConstants.PT_MAGVAR);
    892         addDoubleIfPresent(wpt, n, GpxConstants.PT_GEOIDHEIGHT);
     892        addDoubleIfPresent(wpt, n, GpxConstants.PT_MAGVAR, null);
     893        addDoubleIfPresent(wpt, n, GpxConstants.PT_GEOIDHEIGHT, null);
    893894
    894895        // Description info
    895896
    896         addStringIfPresent(wpt, n, GpxConstants.GPX_NAME);
    897         addStringIfPresent(wpt, n, GpxConstants.GPX_DESC, "description");
    898         addStringIfPresent(wpt, n, GpxConstants.GPX_CMT, "comment");
     897        addStringIfPresent(wpt, n, GpxConstants.GPX_NAME, null, null);
     898        addStringIfPresent(wpt, n, GpxConstants.GPX_DESC, "description", null);
     899        addStringIfPresent(wpt, n, GpxConstants.GPX_CMT, "comment", null);
    899900        addStringIfPresent(wpt, n, GpxConstants.GPX_SRC, "source", "source:position");
    900901
     
    906907        wpt.put(GpxConstants.META_LINKS, links);
    907908
    908         addStringIfPresent(wpt, n, GpxConstants.PT_SYM, "wpt_symbol");
    909         addStringIfPresent(wpt, n, GpxConstants.PT_TYPE);
     909        addStringIfPresent(wpt, n, GpxConstants.PT_SYM, "wpt_symbol", null);
     910        addStringIfPresent(wpt, n, GpxConstants.PT_TYPE, null, null);
    910911
    911912        // Accuracy info
    912         addStringIfPresent(wpt, n, GpxConstants.PT_FIX, "gps:fix");
     913        addStringIfPresent(wpt, n, GpxConstants.PT_FIX, "gps:fix", null);
    913914        addIntegerIfPresent(wpt, n, GpxConstants.PT_SAT, "gps:sat");
    914915        addDoubleIfPresent(wpt, n, GpxConstants.PT_HDOP, "gps:hdop");
     
    933934    }
    934935
    935     private static void addIntegerIfPresent(WayPoint wpt, OsmPrimitive p, String gpxKey, String... osmKeys) {
    936         List<String> possibleKeys = new ArrayList<>(Arrays.asList(osmKeys));
    937         possibleKeys.add(0, gpxKey);
    938         for (String key : possibleKeys) {
    939             String value = gpxVal(p, key);
    940             if (value != null) {
    941                 try {
    942                     int i = Integer.parseInt(value);
    943                     // Sanity checks
    944                     if ((!GpxConstants.PT_SAT.equals(gpxKey) || i >= 0) &&
     936    private static void addIntegerIfPresent(WayPoint wpt, OsmPrimitive p, String gpxKey, String osmKey) {
     937        String value = gpxVal(p, gpxKey);
     938        if (value == null && osmKey != null) {
     939            value = gpxVal(p, osmKey);
     940        }
     941        if (value != null) {
     942            try {
     943                int i = Integer.parseInt(value);
     944                // Sanity checks
     945                if ((!GpxConstants.PT_SAT.equals(gpxKey) || i >= 0) &&
    945946                        (!GpxConstants.PT_DGPSID.equals(gpxKey) || (0 <= i && i <= 1023))) {
    946                         wpt.put(gpxKey, value);
    947                         break;
    948                     }
    949                 } catch (NumberFormatException e) {
    950                     Logging.trace(e);
     947                    wpt.put(gpxKey, value);
    951948                }
    952             }
    953         }
    954     }
    955 
    956     private static void addDoubleIfPresent(WayPoint wpt, OsmPrimitive p, String gpxKey, String... osmKeys) {
    957         List<String> possibleKeys = new ArrayList<>(Arrays.asList(osmKeys));
    958         possibleKeys.add(0, gpxKey);
    959         for (String key : possibleKeys) {
    960             String value = gpxVal(p, key);
    961             if (value != null) {
    962                 try {
    963                     double d = Double.parseDouble(value);
    964                     // Sanity checks
    965                     if (!GpxConstants.PT_MAGVAR.equals(gpxKey) || (0.0 <= d && d < 360.0)) {
    966                         wpt.put(gpxKey, value);
    967                         break;
    968                     }
    969                 } catch (NumberFormatException e) {
    970                     Logging.trace(e);
     949            } catch (NumberFormatException e) {
     950                Logging.trace(e);
     951            }
     952        }
     953    }
     954
     955    private static void addDoubleIfPresent(WayPoint wpt, OsmPrimitive p, String gpxKey, String osmKey) {
     956        String value = gpxVal(p, gpxKey);
     957        if (value == null && osmKey != null) {
     958            value = gpxVal(p, osmKey);
     959        }
     960        if (value != null) {
     961            try {
     962                double d = Double.parseDouble(value);
     963                // Sanity checks
     964                if (!GpxConstants.PT_MAGVAR.equals(gpxKey) || (0.0 <= d && d < 360.0)) {
     965                    wpt.put(gpxKey, value);
    971966                }
    972             }
    973         }
    974     }
    975 
    976     private static void addStringIfPresent(WayPoint wpt, OsmPrimitive p, String gpxKey, String... osmKeys) {
    977         Stream.concat(Stream.of(gpxKey), Arrays.stream(osmKeys))
    978                 .map(key -> gpxVal(p, key))
    979                 // Sanity checks
    980                 .filter(value -> value != null && (!GpxConstants.PT_FIX.equals(gpxKey) || GpxConstants.FIX_VALUES.contains(value)))
    981                 .findFirst()
    982                 .ifPresent(value -> wpt.put(gpxKey, value));
     967            } catch (NumberFormatException e) {
     968                Logging.trace(e);
     969            }
     970        }
     971    }
     972
     973    private static void addStringIfPresent(WayPoint wpt, OsmPrimitive p, String gpxKey, String osmKey, String osmKey2) {
     974        String value = gpxVal(p, gpxKey);
     975        if (value == null && osmKey != null) {
     976            value = gpxVal(p, osmKey);
     977        }
     978        if (value == null && osmKey2 != null) {
     979            value = gpxVal(p, osmKey2);
     980        }
     981        // Sanity checks
     982        if (value != null && (!GpxConstants.PT_FIX.equals(gpxKey) || GpxConstants.FIX_VALUES.contains(value))) {
     983            wpt.put(gpxKey, value);
     984        }
    983985    }
    984986
Note: See TracChangeset for help on using the changeset viewer.