use of org.apache.tapestry5.commons.internal.services.TypeCoercerImpl in project tapestry-5 by apache.
the class BeanModelSourceBuilder method createTypeCoercer.
private void createTypeCoercer() {
CoercionTupleConfiguration configuration = new CoercionTupleConfiguration();
BasicTypeCoercions.provideBasicTypeCoercions(configuration);
BasicTypeCoercions.provideJSR310TypeCoercions(configuration);
typeCoercer = new TypeCoercerImpl(configuration.getTuples());
}
use of org.apache.tapestry5.commons.internal.services.TypeCoercerImpl in project tapestry-5 by apache.
the class EnumValueEncoderTest method roundtrip_with_custom_coercer.
@Test
public // TAP5-2496
void roundtrip_with_custom_coercer() {
CoercionTuple<Stooge, String> stoogeToString = CoercionTuple.create(Stooge.class, String.class, new Coercion<Stooge, String>() {
@Override
public String coerce(Stooge input) {
return String.valueOf(input.ordinal());
}
});
CoercionTuple<String, Stooge> stringToStooge = CoercionTuple.create(String.class, Stooge.class, new Coercion<String, Stooge>() {
@Override
public Stooge coerce(String input) {
return Stooge.values()[Integer.parseInt(input)];
}
});
Map<CoercionTuple.Key, CoercionTuple> map = new HashMap<>();
map.put(stoogeToString.getKey(), stoogeToString);
map.put(stringToStooge.getKey(), stringToStooge);
TypeCoercer typeCoercer = new TypeCoercerImpl(map);
EnumValueEncoder<Stooge> encoder = new EnumValueEncoder<Stooge>(typeCoercer, Stooge.class);
Stooge serverValue = Stooge.LARRY;
String clientValue = encoder.toClient(serverValue);
Stooge convertedBack = encoder.toValue(clientValue);
assertEquals(convertedBack, serverValue);
}
Aggregations