Last active
May 26, 2022 01:36
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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