Ticket #19508: 19508v3.patch

File 19508v3.patch, 3.3 KB (added by gaben, 9 months ago)

Also remove sign from null value, test case updated

  • src/org/openstreetmap/josm/actions/corrector/ReverseWayTagCorrector.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    201201    }
    202202
    203203    /**
    204      * Inverts sign of a numeric value.
     204     * Inverts sign of a numeric value and converts decimal number to use decimal point.
     205     * Also removes sign from null value.
    205206     * @param value numeric value
    206207     * @return opposite numeric value
    207208     */
    208209    public static String invertNumber(String value) {
    209         Pattern pattern = Pattern.compile("^([+-]?)(\\d.*)$", Pattern.CASE_INSENSITIVE);
     210        Pattern pattern = Pattern.compile("^([+-]?)(\\d*[,.]?\\d*)(.*)$", Pattern.CASE_INSENSITIVE);
    210211        Matcher matcher = pattern.matcher(value);
    211212        if (!matcher.matches()) return value;
    212213        String sign = matcher.group(1);
    213         String rest = matcher.group(2);
     214        String number = matcher.group(2);
     215        String symbol = matcher.group(3);
    214216        sign = "-".equals(sign) ? "" : "-";
    215         return sign + rest;
     217
     218        if (!number.isEmpty()) {
     219            String fixedNum = number.replace(",", ".");
     220            double parsed = Double.parseDouble(fixedNum);
     221            if (parsed != 0) {
     222                return sign + fixedNum + symbol;
     223            } else {
     224                return fixedNum + symbol;
     225            }
     226        }
     227
     228        return value;
    216229    }
    217230
    218231    static List<TagCorrection> getTagCorrections(Tagged way) {
  • test/unit/org/openstreetmap/josm/actions/corrector/ReverseWayTagCorrectorTest.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    5858            assertSwitch(new Tag(k, "down"), new Tag(k, "up"));
    5959            assertSwitch(new Tag(k, "something"), new Tag(k, "something"));
    6060        }
     61        // numbered incline (see #19508)
     62        assertSwitch(new Tag("incline", "+0%"), new Tag("incline", "0%"));
     63        assertSwitch(new Tag("incline", ".1%"), new Tag("incline", "-.1%"));
     64        assertSwitch(new Tag("incline", "-10.0%"), new Tag("incline", "10.0%"));
     65        assertSwitch(new Tag("incline", "0,6°"), new Tag("incline", "-0.6°"));
    6166        // direction=forward/backward/...
    6267        assertSwitch(new Tag("direction", "forward"), new Tag("direction", "backward"));
    6368        assertSwitch(new Tag("direction", "backward"), new Tag("direction", "forward"));
     
    99104    }
    100105
    101106    private void assertSwitch(Tag oldTag, Tag newTag) {
    102         Assert.assertEquals(ReverseWayTagCorrector.TagSwitcher.apply(oldTag), newTag);
     107        Assert.assertEquals(newTag, ReverseWayTagCorrector.TagSwitcher.apply(oldTag));
    103108    }
    104109
    105110    private Map<OsmPrimitive, List<TagCorrection>> getTagCorrectionsForWay(String middleNodeTags) {