Changeset 16771 in josm


Ignore:
Timestamp:
2020-07-15T18:58:09+02:00 (4 weeks ago)
Author:
stoecker
Message:

fix #19508 - better reversal function for number, patch by gaben

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/corrector/ReverseWayTagCorrector.java

    r16438 r16771  
    2828import org.openstreetmap.josm.data.osm.Tagged;
    2929import org.openstreetmap.josm.data.osm.Way;
     30import org.openstreetmap.josm.tools.Logging;
    3031import org.openstreetmap.josm.tools.UserCancelException;
    3132
     
    202203
    203204    /**
    204      * Inverts sign of a numeric value.
     205     * Inverts sign of a numeric value and converts decimal number to use decimal point.
     206     * Also removes sign from null value.
    205207     * @param value numeric value
    206208     * @return opposite numeric value
    207209     */
    208210    public static String invertNumber(String value) {
    209         Pattern pattern = Pattern.compile("^([+-]?)(\\d.*)$", Pattern.CASE_INSENSITIVE);
     211        Pattern pattern = Pattern.compile("^([+-]?)(\\d*[,.]?\\d*)(.*)$", Pattern.CASE_INSENSITIVE);
    210212        Matcher matcher = pattern.matcher(value);
    211213        if (!matcher.matches()) return value;
    212214        String sign = matcher.group(1);
    213         String rest = matcher.group(2);
     215        String number = matcher.group(2);
     216        String symbol = matcher.group(3);
    214217        sign = "-".equals(sign) ? "" : "-";
    215         return sign + rest;
     218
     219        if (!number.isEmpty()) {
     220            String fixedNum = number.replace(",", ".");
     221            try {
     222                double parsed = Double.parseDouble(fixedNum);
     223                if (parsed != 0) {
     224                    return sign + fixedNum + symbol;
     225                } else {
     226                    return fixedNum + symbol;
     227                }
     228            } catch (NumberFormatException e) {
     229                Logging.trace(e);
     230                return value;
     231            }
     232        }
     233
     234        return value;
    216235    }
    217236
  • trunk/test/unit/org/openstreetmap/josm/actions/corrector/ReverseWayTagCorrectorTest.java

    r16182 r16771  
    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"));
     
    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
Note: See TracChangeset for help on using the changeset viewer.