19 — Advanced
Type Conversion
ConverterRegistry manages type converters for automatic value conversion in rulii. It ships with 19 built-in text converters (e.g., String to Integer, String to LocalDate) and is part of the RuleContext, where it handles automatic parameter conversion when binding values don't match expected types.
Using the Default Registry
// Default registry with all built-in converters
ConverterRegistry registry = ConverterRegistry.builder().build();
// Find a converter
Converter<String, Integer> converter = registry.find(String.class, Integer.class);
Integer result = converter.convert("42", Integer.class);
// result == 42
Empty Registry
Pass false to the builder to create an empty registry with no built-in converters:
// Empty registry (no built-in converters)
ConverterRegistry empty = ConverterRegistry.builder(false).build();
Built-in Converters
The default registry includes 19 text-based converters:
| Converter | Conversion |
|---|---|
TextToIntegerConverter |
String to Integer |
TextToLongConverter |
String to Long |
TextToDoubleConverter |
String to Double |
TextToFloatConverter |
String to Float |
TextToBooleanConverter |
String to Boolean |
TextToLocalDateConverter |
String to LocalDate |
TextToLocalDateTimeConverter |
String to LocalDateTime |
TextToDateConverter |
String to Date |
TextToUrlConverter |
String to URL |
TextToBigDecimalConverter |
String to BigDecimal |
TextToBigIntegerConverter |
String to BigInteger |
TextToByteConverter |
String to Byte |
TextToShortConverter |
String to Short |
TextToEnumConverter |
String to Enum |
TextToUUIDConverter |
String to UUID |
TextToCurrencyConverter |
String to Currency |
TextToCharsetConverter |
String to Charset |
TextToStringConverter |
String to String (identity) |
Custom Converters
Register your own converters for custom type conversion logic:
ConverterRegistry registry = ConverterRegistry.builder(false).build();
registry.register(new Converter<String, Integer>() {
public Type getSourceType() { return String.class; }
public Type getTargetType() { return Integer.class; }
public boolean canConvert(Type from, Type to) {
return from == String.class && to == Integer.class;
}
public Integer convert(String value, Type toType) {
return Integer.parseInt(value);
}
});
Automatic Integration
Converters are used automatically by the rulii framework. When a binding parameter value does not match the expected type of a condition, action, or function parameter, the framework consults the ConverterRegistry in the current RuleContext to find and apply an appropriate converter. This means you can store a String in a binding and have it automatically converted to an Integer, LocalDate, or any other supported type when needed.