Skip to content

Instantly share code, notes, and snippets.

@arantesxyz
Last active May 26, 2022 01:36
Show Gist options
  • Select an option

  • Save arantesxyz/8fb8e770a394acb56f18a0179cef3cb1 to your computer and use it in GitHub Desktop.

Select an option

Save arantesxyz/8fb8e770a394acb56f18a0179cef3cb1 to your computer and use it in GitHub Desktop.
Replacer instance to improve code readability when replacing a lot of different strings with the same pattern.
/**
* Replacer instance to improve code readability when replacing a lot of different strings with the same pattern.
*
* @Author Gustavo Arantes (https://arantes.dev/)
*/
public class Replacer {
private Map<CharSequence, CharSequence> replacers;
public Replacer() {
this.replacers = new HashMap<>();
}
public Replacer add(CharSequence key, Object value) {
replacers.put(key, value.toString());
return this;
}
public String replace(String message) {
for (Map.Entry<CharSequence, CharSequence> entry : replacers.entrySet()) {
message = message.replace(entry.getKey(), entry.getValue());
}
return message;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment