@Test public void multiple() { Map<String, String> map = new HashMap<>(); map.put("m1", "Hello1"); map.put("m2", "Hello2"); map.put("m3", "Hello3"); VariableResolver resolver = new VariableResolver(map); assertThat(resolver.replace("${m1}, ${m2}, ${m3}!", true), is("Hello1, Hello2, Hello3!")); }
public String replace(String string, boolean strict) { if (string == null) { throw new IllegalArgumentException("string must not be null"); } StringBuilder buf = new StringBuilder(); int start = 0; Matcher matcher = VARIABLE.matcher(string); while (matcher.find(start)) { String placeholder = matcher.group(1); String replacement = resolve(placeholder); if (replacement == null) { if (strict) { throw new IllegalArgumentException(MessageFormat.format( "parameter \"{0}\" is not defined in the list: {1}", placeholder, this)); } else { buf.append(string.substring(start, matcher.start() + 1)); } start = matcher.start() + 1; } else { buf.append(string.substring(start, matcher.start())); buf.append(replacement); start = matcher.end(); } } buf.append(string.substring(start)); return buf.toString(); }
VariableResolver { public String replace(String string, boolean strict) { if (string == null) { throw new IllegalArgumentException("string must not be null"); } StringBuilder buf = new StringBuilder(); int start = 0; Matcher matcher = VARIABLE.matcher(string); while (matcher.find(start)) { String placeholder = matcher.group(1); String replacement = resolve(placeholder); if (replacement == null) { if (strict) { throw new IllegalArgumentException(MessageFormat.format( "parameter \"{0}\" is not defined in the list: {1}", placeholder, this)); } else { buf.append(string.substring(start, matcher.start() + 1)); } start = matcher.start() + 1; } else { buf.append(string.substring(start, matcher.start())); buf.append(replacement); start = matcher.end(); } } buf.append(string.substring(start)); return buf.toString(); } }
VariableResolver { public String replace(String string, boolean strict) { if (string == null) { throw new IllegalArgumentException("string must not be null"); } StringBuilder buf = new StringBuilder(); int start = 0; Matcher matcher = VARIABLE.matcher(string); while (matcher.find(start)) { String placeholder = matcher.group(1); String replacement = resolve(placeholder); if (replacement == null) { if (strict) { throw new IllegalArgumentException(MessageFormat.format( "parameter \"{0}\" is not defined in the list: {1}", placeholder, this)); } else { buf.append(string.substring(start, matcher.start() + 1)); } start = matcher.start() + 1; } else { buf.append(string.substring(start, matcher.start())); buf.append(replacement); start = matcher.end(); } } buf.append(string.substring(start)); return buf.toString(); } VariableResolver(Map<String, String> entries); }
VariableResolver { public String replace(String string, boolean strict) { if (string == null) { throw new IllegalArgumentException("string must not be null"); } StringBuilder buf = new StringBuilder(); int start = 0; Matcher matcher = VARIABLE.matcher(string); while (matcher.find(start)) { String placeholder = matcher.group(1); String replacement = resolve(placeholder); if (replacement == null) { if (strict) { throw new IllegalArgumentException(MessageFormat.format( "parameter \"{0}\" is not defined in the list: {1}", placeholder, this)); } else { buf.append(string.substring(start, matcher.start() + 1)); } start = matcher.start() + 1; } else { buf.append(string.substring(start, matcher.start())); buf.append(replacement); start = matcher.end(); } } buf.append(string.substring(start)); return buf.toString(); } VariableResolver(Map<String, String> entries); static VariableResolver system(); String replace(String string, boolean strict); @Override String toString(); }
VariableResolver { public String replace(String string, boolean strict) { if (string == null) { throw new IllegalArgumentException("string must not be null"); } StringBuilder buf = new StringBuilder(); int start = 0; Matcher matcher = VARIABLE.matcher(string); while (matcher.find(start)) { String placeholder = matcher.group(1); String replacement = resolve(placeholder); if (replacement == null) { if (strict) { throw new IllegalArgumentException(MessageFormat.format( "parameter \"{0}\" is not defined in the list: {1}", placeholder, this)); } else { buf.append(string.substring(start, matcher.start() + 1)); } start = matcher.start() + 1; } else { buf.append(string.substring(start, matcher.start())); buf.append(replacement); start = matcher.end(); } } buf.append(string.substring(start)); return buf.toString(); } VariableResolver(Map<String, String> entries); static VariableResolver system(); String replace(String string, boolean strict); @Override String toString();  }