Created
May 29, 2022 16:41
-
-
Save hamza-cskn/3ea04baa594cc2a42661754d45fc960e to your computer and use it in GitHub Desktop.
PlaceholderUtil
This file contains 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
package mc.obliviate.masterduels.utils.placeholder; | |
public class InternalPlaceholder { | |
private final String placeholder; | |
private final String value; | |
protected InternalPlaceholder(final String placeholder, final String value) { | |
this.placeholder = placeholder; | |
this.value = value; | |
} | |
public String getPlaceholder() { | |
return placeholder; | |
} | |
public String getValue() { | |
if (value == null) return ""; | |
return value; | |
} | |
} |
This file contains 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
package mc.obliviate.masterduels.utils.placeholder; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class PlaceholderUtil { | |
private final List<InternalPlaceholder> placeholders = new ArrayList<>(); | |
public PlaceholderUtil add(final String key, final String value) { | |
placeholders.add(new InternalPlaceholder(key, value)); | |
return this; | |
} | |
public List<InternalPlaceholder> getPlaceholders() { | |
return placeholders; | |
} | |
public List<String> apply(final List<String> texts) { | |
final List<String> result = new ArrayList<>(); | |
for (final String text : texts) { | |
result.add(apply(text)); | |
} | |
return result; | |
} | |
public String apply(String text) { | |
for (final InternalPlaceholder placeholder : placeholders) { | |
text = text.replace(placeholder.getPlaceholder(), placeholder.getValue()); | |
} | |
return text; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compatible with Message Utils gist.
Example Usage: