Search in sources :

Example 1 with TypeCoercerImpl

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());
}
Also used : TypeCoercerImpl(org.apache.tapestry5.commons.internal.services.TypeCoercerImpl)

Example 2 with TypeCoercerImpl

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);
}
Also used : HashMap(java.util.HashMap) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) CoercionTuple(org.apache.tapestry5.commons.services.CoercionTuple) TypeCoercerImpl(org.apache.tapestry5.commons.internal.services.TypeCoercerImpl) Test(org.testng.annotations.Test)

Aggregations

TypeCoercerImpl (org.apache.tapestry5.commons.internal.services.TypeCoercerImpl)2 HashMap (java.util.HashMap)1 CoercionTuple (org.apache.tapestry5.commons.services.CoercionTuple)1 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)1 Test (org.testng.annotations.Test)1