Ignore:
Timestamp:
2014-01-12T10:41:20+01:00 (10 years ago)
Author:
simon04
Message:

Make strings like foo relation/123 and way/345 but also node/789 paste-able in "Download object", some refactoring of related code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/SimplePrimitiveId.java

    r6084 r6674  
    33
    44import java.io.Serializable;
     5import java.util.ArrayList;
     6import java.util.List;
    57import java.util.regex.Matcher;
    68import java.util.regex.Pattern;
     
    911    private final long id;
    1012    private final OsmPrimitiveType type;
     13
     14    public static final Pattern ID_PATTERN = Pattern.compile("((n(ode)?|w(ay)?|r(el(ation)?)?)/?)(\\d+)");
    1115
    1216    public SimplePrimitiveId(long id, OsmPrimitiveType type) {
     
    6468
    6569    /**
    66      * Parses a {@code OsmPrimitiveType} from the string {@code s}.
     70     * Parses a {@code SimplePrimitiveId} from the string {@code s}.
    6771     * @param s the string to be parsed, e.g., {@code n1}, {@code node1},
    6872     * {@code w1}, {@code way1}, {@code r1}, {@code rel1}, {@code relation1}.
    69      * @return the parsed {@code OsmPrimitiveType}
     73     * @return the parsed {@code SimplePrimitiveId}
    7074     * @throws IllegalArgumentException if the string does not match the pattern
    7175     */
    7276    public static SimplePrimitiveId fromString(String s) {
    73         final Pattern p = Pattern.compile("((n(ode)?|w(ay)?|r(el(ation)?)?)/?)(\\d+)");
    74         final Matcher m = p.matcher(s);
     77        final Matcher m = ID_PATTERN.matcher(s);
    7578        if (m.matches()) {
    7679            return new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())),
    77                     s.charAt(0) == 'n' ? OsmPrimitiveType.NODE
    78                     : s.charAt(0) == 'w' ? OsmPrimitiveType.WAY
    79                     : OsmPrimitiveType.RELATION);
     80                    s.charAt(0) == 'n'
     81                            ? OsmPrimitiveType.NODE
     82                            : s.charAt(0) == 'w'
     83                            ? OsmPrimitiveType.WAY
     84                            : OsmPrimitiveType.RELATION);
    8085        } else {
    81             throw new IllegalArgumentException("The string " + s + " does not match the pattern " + p);
     86            throw new IllegalArgumentException("The string " + s + " does not match the pattern " + ID_PATTERN);
    8287        }
    8388    }
     89
     90    /**
     91     * Attempts to parse extract any primitive id from the string {@code s}.
     92     * @param s the string to be parsed, e.g., {@code n1, w1}, {@code node1 and rel2}.
     93     * @return the parsed list of {@code OsmPrimitiveType}s.
     94     */
     95    public static List<SimplePrimitiveId> fuzzyParse(String s) {
     96        final ArrayList<SimplePrimitiveId> ids = new ArrayList<SimplePrimitiveId>();
     97        final Matcher m = ID_PATTERN.matcher(s);
     98        while (m.find()) {
     99            final char firstChar = s.charAt(m.start());
     100            ids.add(new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())),
     101                    firstChar == 'n'
     102                            ? OsmPrimitiveType.NODE
     103                            : firstChar == 'w'
     104                            ? OsmPrimitiveType.WAY
     105                            : OsmPrimitiveType.RELATION));
     106        }
     107        return ids;
     108    }
    84109}
Note: See TracChangeset for help on using the changeset viewer.