se.eris.util.string.StringWorker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of notnull-instrumenter-maven-plugin Show documentation
Show all versions of notnull-instrumenter-maven-plugin Show documentation
NotNull annotations instrumenter maven plugin
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