Adds a stateful StringReplacer for complex tasks
This commit is contained in:
		@@ -0,0 +1,76 @@
 | 
			
		||||
package net.knarcraft.knarlib.formatting;
 | 
			
		||||
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * A stateful string replacer
 | 
			
		||||
 *
 | 
			
		||||
 * <p>Add placeholder -> value pairs before running replace on the wanted string</p>
 | 
			
		||||
 */
 | 
			
		||||
@SuppressWarnings("unused")
 | 
			
		||||
public class StringReplacer {
 | 
			
		||||
 | 
			
		||||
    private final Map<String, String> replacements = new HashMap<>();
 | 
			
		||||
    private String replacementInput;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Instantiates a new string replacer
 | 
			
		||||
     */
 | 
			
		||||
    public StringReplacer() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Instantiates a new string replacer
 | 
			
		||||
     *
 | 
			
		||||
     * @param replacementInput <p>The input string to replace placeholders for</p>
 | 
			
		||||
     */
 | 
			
		||||
    public StringReplacer(String replacementInput) {
 | 
			
		||||
        this.replacementInput = replacementInput;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Adds a new string replacement
 | 
			
		||||
     *
 | 
			
		||||
     * @param placeholder <p>The placeholder to replace</p>
 | 
			
		||||
     * @param value       <p>The replacement value</p>
 | 
			
		||||
     */
 | 
			
		||||
    public void add(String placeholder, String value) {
 | 
			
		||||
        this.replacements.put(placeholder, value);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Removes a string replacement
 | 
			
		||||
     *
 | 
			
		||||
     * @param placeholder <p>The placeholder to remove</p>
 | 
			
		||||
     */
 | 
			
		||||
    public void remove(String placeholder) {
 | 
			
		||||
        this.replacements.remove(placeholder);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Replaces previously added placeholders in the string given in the constructor
 | 
			
		||||
     *
 | 
			
		||||
     * <p>You must use the constructor with a non-null string to use this method.</p>
 | 
			
		||||
     *
 | 
			
		||||
     * @return <p>The string with placeholders replaced</p>
 | 
			
		||||
     */
 | 
			
		||||
    public String replace() {
 | 
			
		||||
        if (replacementInput == null) {
 | 
			
		||||
            throw new IllegalStateException("This method cannot be run without a defined string");
 | 
			
		||||
        }
 | 
			
		||||
        return replace(replacementInput);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Replaces previously added placeholders in the given string
 | 
			
		||||
     *
 | 
			
		||||
     * @param input <p>The string to replace placeholders in</p>
 | 
			
		||||
     * @return <p>The string with placeholders replaced</p>
 | 
			
		||||
     */
 | 
			
		||||
    public String replace(String input) {
 | 
			
		||||
        return StringFormatter.replacePlaceholders(input, replacements.keySet().toArray(new String[0]),
 | 
			
		||||
                replacements.values().toArray(new String[0]));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user