software.amazon.event.ruler.input.SuffixParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of event-ruler Show documentation
Show all versions of event-ruler Show documentation
Event Ruler is a Java library that allows matching Rules to Events. An event is a list of fields,
which may be given as name/value pairs or as a JSON object. A rule associates event field names with lists of
possible values. There are two reasons to use Ruler: 1/ It's fast; the time it takes to match Events doesn't
depend on the number of Rules. 2/ Customers like the JSON "query language" for expressing rules.
package software.amazon.event.ruler.input;
import java.nio.charset.StandardCharsets;
/**
* A parser to be used specifically for suffix rules.
*
* This undoes the `reverse()` from {@code software.amazon.event.ruler.Patterns} intentionally
* to ensure we can correctly reverse utf-8 characters with 2+ bytes like '大' and '雨'.
*/
public class SuffixParser implements StringValueParser {
SuffixParser() { }
@Override
public InputCharacter[] parse(String value) {
final byte[] utf8bytes = new StringBuilder(value).reverse()
.toString().getBytes(StandardCharsets.UTF_8);
final InputCharacter[] result = new InputCharacter[utf8bytes.length];
for (int i = 0; i < utf8bytes.length; i++) {
byte utf8byte = utf8bytes[utf8bytes.length - i - 1];
result[i] = new InputByte(utf8byte);
}
return result;
}
}