panda.lang.escape.AggregateTranslator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of panda-core Show documentation
Show all versions of panda-core Show documentation
Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.
package panda.lang.escape;
import java.io.IOException;
import panda.lang.Arrays;
/**
* Executes a sequence of translators one after the other. Execution ends whenever
* the first translator consumes codepoints from the input.
*/
public class AggregateTranslator extends CharSequenceTranslator {
private final CharSequenceTranslator[] translators;
/**
* Specify the translators to be used at creation time.
*
* @param translators CharSequenceTranslator array to aggregate
*/
public AggregateTranslator(final CharSequenceTranslator... translators) {
this.translators = Arrays.clone(translators);
}
/**
* The first translator to consume codepoints from the input is the 'winner'. Execution stops
* with the number of consumed codepoints being returned. {@inheritDoc}
*/
@Override
public int translateChar(final CharSequence input, final int index, final Appendable out) throws IOException {
for (final CharSequenceTranslator translator : translators) {
final int consumed = translator.translateChar(input, index, out);
if (consumed != 0) {
return consumed;
}
}
return 0;
}
}