@Test public void simplePattern() { String pattern = "/{a}/{b}/{c}"; final TokenParser parser = new TokenParser(pattern); log(parser); final Map<String, String> tokens = parser.parse("/yes/no/false"); assertThat(tokens, is(notNullValue())); assertThat(tokens.entrySet(), hasSize(3)); assertThat(tokens.get("a"), is(equalTo("yes"))); assertThat(tokens.get("b"), is(equalTo("no"))); assertThat(tokens.get("c"), is(equalTo("false"))); }
@Nullable public Map<String, String> parse(final String path) { final Matcher matcher = pattern.matcher(path); if (!matcher.matches()) { return null; } Map<String, String> values = new HashMap<>(); for (VariableToken variableToken : variables) { final String name = variableToken.getName(); final String value = matcher.group(variableToken.getRegexpGroupName()); if (values.containsKey(name)) { final String existingValue = values.get(name); if (!Objects.equals(existingValue, value)) { log.trace("Variable '{}' values mismatch: '{}' vs '{}'", name, existingValue, value); return null; } } else { values.put(name, value); } } return values; }
TokenParser extends ComponentSupport { @Nullable public Map<String, String> parse(final String path) { final Matcher matcher = pattern.matcher(path); if (!matcher.matches()) { return null; } Map<String, String> values = new HashMap<>(); for (VariableToken variableToken : variables) { final String name = variableToken.getName(); final String value = matcher.group(variableToken.getRegexpGroupName()); if (values.containsKey(name)) { final String existingValue = values.get(name); if (!Objects.equals(existingValue, value)) { log.trace("Variable '{}' values mismatch: '{}' vs '{}'", name, existingValue, value); return null; } } else { values.put(name, value); } } return values; } }
TokenParser extends ComponentSupport { @Nullable public Map<String, String> parse(final String path) { final Matcher matcher = pattern.matcher(path); if (!matcher.matches()) { return null; } Map<String, String> values = new HashMap<>(); for (VariableToken variableToken : variables) { final String name = variableToken.getName(); final String value = matcher.group(variableToken.getRegexpGroupName()); if (values.containsKey(name)) { final String existingValue = values.get(name); if (!Objects.equals(existingValue, value)) { log.trace("Variable '{}' values mismatch: '{}' vs '{}'", name, existingValue, value); return null; } } else { values.put(name, value); } } return values; } TokenParser(final String templatePattern); }
TokenParser extends ComponentSupport { @Nullable public Map<String, String> parse(final String path) { final Matcher matcher = pattern.matcher(path); if (!matcher.matches()) { return null; } Map<String, String> values = new HashMap<>(); for (VariableToken variableToken : variables) { final String name = variableToken.getName(); final String value = matcher.group(variableToken.getRegexpGroupName()); if (values.containsKey(name)) { final String existingValue = values.get(name); if (!Objects.equals(existingValue, value)) { log.trace("Variable '{}' values mismatch: '{}' vs '{}'", name, existingValue, value); return null; } } else { values.put(name, value); } } return values; } TokenParser(final String templatePattern); @Nullable Map<String, String> parse(final String path); String getPattern(); @Override String toString(); }
TokenParser extends ComponentSupport { @Nullable public Map<String, String> parse(final String path) { final Matcher matcher = pattern.matcher(path); if (!matcher.matches()) { return null; } Map<String, String> values = new HashMap<>(); for (VariableToken variableToken : variables) { final String name = variableToken.getName(); final String value = matcher.group(variableToken.getRegexpGroupName()); if (values.containsKey(name)) { final String existingValue = values.get(name); if (!Objects.equals(existingValue, value)) { log.trace("Variable '{}' values mismatch: '{}' vs '{}'", name, existingValue, value); return null; } } else { values.put(name, value); } } return values; } TokenParser(final String templatePattern); @Nullable Map<String, String> parse(final String path); String getPattern(); @Override String toString();  }