Changeset 6674 in josm for trunk/src/org/openstreetmap/josm/data/osm/SimplePrimitiveId.java
- Timestamp:
- 2014-01-12T10:41:20+01:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/SimplePrimitiveId.java
r6084 r6674 3 3 4 4 import java.io.Serializable; 5 import java.util.ArrayList; 6 import java.util.List; 5 7 import java.util.regex.Matcher; 6 8 import java.util.regex.Pattern; … … 9 11 private final long id; 10 12 private final OsmPrimitiveType type; 13 14 public static final Pattern ID_PATTERN = Pattern.compile("((n(ode)?|w(ay)?|r(el(ation)?)?)/?)(\\d+)"); 11 15 12 16 public SimplePrimitiveId(long id, OsmPrimitiveType type) { … … 64 68 65 69 /** 66 * Parses a {@code OsmPrimitiveType} from the string {@code s}.70 * Parses a {@code SimplePrimitiveId} from the string {@code s}. 67 71 * @param s the string to be parsed, e.g., {@code n1}, {@code node1}, 68 72 * {@code w1}, {@code way1}, {@code r1}, {@code rel1}, {@code relation1}. 69 * @return the parsed {@code OsmPrimitiveType}73 * @return the parsed {@code SimplePrimitiveId} 70 74 * @throws IllegalArgumentException if the string does not match the pattern 71 75 */ 72 76 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); 75 78 if (m.matches()) { 76 79 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); 80 85 } 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); 82 87 } 83 88 } 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 } 84 109 }
Note:
See TracChangeset
for help on using the changeset viewer.