Expert
This code
public class ArgumentReplacer {
public static final String BEGINING_MUSTACH = "\\{\\{\\s*";
public static final String ENDING_MUSTACH = "\\s*\\}\\}";
public static String replace(String template, Map<String, String> arguments) {
if (needNoReplace(template, arguments)) {
return template;
}
return arguments.entrySet().stream().reduce(template, ArgumentReplacer::replaceEntry, keepFirst());
}
private static boolean needNoReplace(String template, Map<String, String> arguments) {
return template == null || arguments == null;
}
private static String replaceEntry(String result, Map.Entry<String, String> entry) {
return result.replaceAll(BEGINING_MUSTACH + entry.getKey() + ENDING_MUSTACH, entry.getValue());
}
private static BinaryOperator<String> keepFirst() {
return (k, v) -> v;
}
}
Author: Clément DevosStatus: PublishedQuestion passed 445 times
Edit
1
Community EvaluationsNo one has reviewed this question yet, be the first!
1
A Java class that converts Arabic numbers to Roman numerals.4
This code allows to randomly get numbers between 1 to 31 in results. Should have declard SimpleDateFormat in the Thread.1
What is the name of the design pattern used to structure complex applications by considering the problem domain?2
Write a function that returns the first character of a string in Java1
A code kata is a small exercice thought to train a certain development competence1
What does SRP stand for?1
Write a Java implementation of the FizzBuzz code kata.