All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.eris.util.string.StringWorker Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package se.eris.util.string;

import java.util.List;

class StringWorker {

    private final StringBuilder stringBuilder = new StringBuilder();
    private final StringAnalyzer stringAnalyzer;
    private int index;

    StringWorker(final String s) {
        this.stringAnalyzer = new StringAnalyzer(s);
        index = 0;
    }

    private void step() {
        stringBuilder.append(stringAnalyzer.charAt(index));
        index++;
    }

    public void insert(final String s) {
        stringBuilder.append(s);
    }

    private boolean isDone() {
        return index >= stringAnalyzer.length();
    }

    /**
     * Iterates character by character once replacing using the first matching replacement.
     * @param replacements
     */
    public void replaceInOrder(final List replacements) {
        while (!isDone()) {
            if (!replaceFirstMatching(replacements)) {
                step();
            }
        }
    }

    private boolean replaceFirstMatching(final List replacements) {
        for (final Replacement replacement : replacements) {
            if (replaceIfMatch(replacement)) {
                return true;
            }
        }
        return false;
    }

    private boolean replaceIfMatch(final Replacement replacement) {
        final String replace = replacement.getReplace();
        if (stringAnalyzer.isStringAt(index, replace)) {
            stringBuilder.append(replacement.getWith());
            index += replace.length();
            return true;
        }
        return false;
    }

    public String toString() {
        return stringBuilder.toString();
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy