Changeset 8496 in josm for trunk/src/org/openstreetmap/josm/data
- Timestamp:
- 2015-06-19T20:23:18+02:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/SimplePrimitiveId.java
r8338 r8496 5 5 import java.util.ArrayList; 6 6 import java.util.List; 7 import java.util.regex.MatchResult; 7 8 import java.util.regex.Matcher; 8 9 import java.util.regex.Pattern; … … 15 16 private final OsmPrimitiveType type; 16 17 17 public static final Pattern ID_PATTERN = Pattern.compile("((n(ode)?|w(ay)?|r(el(ation)?)?)[ /]?)(\\d+)"); 18 public static final Pattern ID_PATTERN = Pattern.compile("(n|node|w|way|r|rel|relation)[ /]?(\\d+)"); 19 20 public static final Pattern MULTIPLE_IDS_PATTERN = Pattern.compile(ID_PATTERN.pattern() + "(-(\\d+))?"); 18 21 19 22 public SimplePrimitiveId(long id, OsmPrimitiveType type) { … … 80 83 final Matcher m = ID_PATTERN.matcher(s); 81 84 if (m.matches()) { 82 return new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())), 83 s.charAt(0) == 'n' 84 ? OsmPrimitiveType.NODE 85 : s.charAt(0) == 'w' 86 ? OsmPrimitiveType.WAY 87 : OsmPrimitiveType.RELATION); 85 return new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())), getOsmPrimitiveType(s.charAt(0))); 88 86 } else { 89 87 throw new IllegalArgumentException("The string " + s + " does not match the pattern " + ID_PATTERN); … … 92 90 93 91 /** 92 * Parses a range {@code SimplePrimitiveId} from the string {@code s}. 93 * @param s the string to be parsed, e.g., {@code node1}, {@code node1-7}, {@code node70-7}. 94 * @return the parsed {@code SimplePrimitiveId}s 95 * @throws IllegalArgumentException if the string does not match the pattern 96 */ 97 public static List<SimplePrimitiveId> multipleFromString(String s) { 98 final Matcher m = MULTIPLE_IDS_PATTERN.matcher(s); 99 if (m.matches()) { 100 return extractIdsInto(m, new ArrayList<SimplePrimitiveId>()); 101 } else { 102 throw new IllegalArgumentException("The string " + s + " does not match the pattern " + MULTIPLE_IDS_PATTERN); 103 } 104 } 105 106 /** 94 107 * Attempts to parse extract any primitive id from the string {@code s}. 95 * @param s the string to be parsed, e.g., {@code n1, w1}, {@code node1 and rel2}.108 * @param s the string to be parsed, e.g., {@code "n1, w1"}, {@code "node1 and rel2"}, {@code "node 123-29"}. 96 109 * @return the parsed list of {@code OsmPrimitiveType}s. 97 110 */ 98 111 public static List<SimplePrimitiveId> fuzzyParse(String s) { 99 112 final List<SimplePrimitiveId> ids = new ArrayList<>(); 100 final Matcher m = ID_PATTERN.matcher(s);113 final Matcher m = MULTIPLE_IDS_PATTERN.matcher(s); 101 114 while (m.find()) { 102 final char firstChar = s.charAt(m.start()); 103 ids.add(new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())), 104 firstChar == 'n' 105 ? OsmPrimitiveType.NODE 106 : firstChar == 'w' 107 ? OsmPrimitiveType.WAY 108 : OsmPrimitiveType.RELATION)); 115 extractIdsInto(m, ids); 109 116 } 110 117 return ids; 111 118 } 119 120 private static List<SimplePrimitiveId> extractIdsInto(MatchResult m, List<SimplePrimitiveId> ids) { 121 final OsmPrimitiveType type = getOsmPrimitiveType(m.group(1).charAt(0)); 122 final String firstId = m.group(2); 123 final String lastId = m.group(4); 124 if (lastId != null) { 125 final long lastIdParsed; 126 if (lastId.length() < firstId.length()) { 127 // parse ranges such as 123-25 or 123-5 128 lastIdParsed = Long.parseLong(firstId.substring(0, firstId.length() - lastId.length()) + lastId); 129 } else { 130 // parse ranges such as 123-125 or 998-1001 131 lastIdParsed = Long.parseLong(lastId); 132 } 133 for (long i = Long.parseLong(firstId); i <= lastIdParsed; i++) { 134 ids.add(new SimplePrimitiveId(i, type)); 135 } 136 } else { 137 ids.add(new SimplePrimitiveId(Long.parseLong(firstId), type)); 138 } 139 return ids; 140 } 141 142 private static OsmPrimitiveType getOsmPrimitiveType(char firstChar) { 143 return firstChar == 'n' ? OsmPrimitiveType.NODE : firstChar == 'w' ? OsmPrimitiveType.WAY : OsmPrimitiveType.RELATION; 144 } 112 145 }
Note:
See TracChangeset
for help on using the changeset viewer.