Search in sources :

Example 1 with TimeInterval

use of org.apache.tapestry5.commons.util.TimeInterval in project tapestry-5 by apache.

the class BasicTypeCoercions method provideBasicTypeCoercions.

/**
 * Provides the basic type coercions to a {@link MappedConfiguration} instance.
 */
public static void provideBasicTypeCoercions(MappedConfiguration<CoercionTuple.Key, CoercionTuple> configuration) {
    add(configuration, Object.class, String.class, new Coercion<Object, String>() {

        @Override
        public String coerce(Object input) {
            return input.toString();
        }
    });
    add(configuration, Object.class, Boolean.class, new Coercion<Object, Boolean>() {

        @Override
        public Boolean coerce(Object input) {
            return input != null;
        }
    });
    add(configuration, String.class, Double.class, new Coercion<String, Double>() {

        @Override
        public Double coerce(String input) {
            return Double.valueOf(input);
        }
    });
    // String to BigDecimal is important, as String->Double->BigDecimal would lose
    // precision.
    add(configuration, String.class, BigDecimal.class, new Coercion<String, BigDecimal>() {

        @Override
        public BigDecimal coerce(String input) {
            return new BigDecimal(input);
        }
    });
    add(configuration, BigDecimal.class, Double.class, new Coercion<BigDecimal, Double>() {

        @Override
        public Double coerce(BigDecimal input) {
            return input.doubleValue();
        }
    });
    add(configuration, String.class, BigInteger.class, new Coercion<String, BigInteger>() {

        @Override
        public BigInteger coerce(String input) {
            return new BigInteger(input);
        }
    });
    add(configuration, String.class, Long.class, new Coercion<String, Long>() {

        @Override
        public Long coerce(String input) {
            return Long.valueOf(input);
        }
    });
    add(configuration, String.class, Integer.class, Integer::valueOf);
    add(configuration, Long.class, Byte.class, new Coercion<Long, Byte>() {

        @Override
        public Byte coerce(Long input) {
            return input.byteValue();
        }
    });
    add(configuration, Long.class, Short.class, new Coercion<Long, Short>() {

        @Override
        public Short coerce(Long input) {
            return input.shortValue();
        }
    });
    add(configuration, Long.class, Integer.class, new Coercion<Long, Integer>() {

        @Override
        public Integer coerce(Long input) {
            return input.intValue();
        }
    });
    add(configuration, Number.class, Long.class, new Coercion<Number, Long>() {

        @Override
        public Long coerce(Number input) {
            return input.longValue();
        }
    });
    add(configuration, Double.class, Float.class, new Coercion<Double, Float>() {

        @Override
        public Float coerce(Double input) {
            return input.floatValue();
        }
    });
    add(configuration, Long.class, Double.class, new Coercion<Long, Double>() {

        @Override
        public Double coerce(Long input) {
            return input.doubleValue();
        }
    });
    add(configuration, String.class, Boolean.class, new Coercion<String, Boolean>() {

        @Override
        public Boolean coerce(String input) {
            String trimmed = input == null ? "" : input.trim();
            if (trimmed.equalsIgnoreCase("false") || trimmed.length() == 0)
                return false;
            return true;
        }
    });
    add(configuration, Number.class, Boolean.class, new Coercion<Number, Boolean>() {

        @Override
        public Boolean coerce(Number input) {
            return input.longValue() != 0;
        }
    });
    add(configuration, Void.class, Boolean.class, new Coercion<Void, Boolean>() {

        @Override
        public Boolean coerce(Void input) {
            return false;
        }
    });
    add(configuration, Collection.class, Boolean.class, new Coercion<Collection, Boolean>() {

        @Override
        public Boolean coerce(Collection input) {
            return !input.isEmpty();
        }
    });
    add(configuration, Object.class, List.class, new Coercion<Object, List>() {

        @Override
        public List coerce(Object input) {
            return Collections.singletonList(input);
        }
    });
    add(configuration, Object[].class, List.class, new Coercion<Object[], List>() {

        @Override
        public List coerce(Object[] input) {
            return Arrays.asList(input);
        }
    });
    add(configuration, Object[].class, Boolean.class, new Coercion<Object[], Boolean>() {

        @Override
        public Boolean coerce(Object[] input) {
            return input != null && input.length > 0;
        }
    });
    add(configuration, Float.class, Double.class, new Coercion<Float, Double>() {

        @Override
        public Double coerce(Float input) {
            return input.doubleValue();
        }
    });
    Coercion primitiveArrayCoercion = new Coercion<Object, List>() {

        @Override
        public List<Object> coerce(Object input) {
            int length = Array.getLength(input);
            Object[] array = new Object[length];
            for (int i = 0; i < length; i++) {
                array[i] = Array.get(input, i);
            }
            return Arrays.asList(array);
        }
    };
    add(configuration, byte[].class, List.class, primitiveArrayCoercion);
    add(configuration, short[].class, List.class, primitiveArrayCoercion);
    add(configuration, int[].class, List.class, primitiveArrayCoercion);
    add(configuration, long[].class, List.class, primitiveArrayCoercion);
    add(configuration, float[].class, List.class, primitiveArrayCoercion);
    add(configuration, double[].class, List.class, primitiveArrayCoercion);
    add(configuration, char[].class, List.class, primitiveArrayCoercion);
    add(configuration, boolean[].class, List.class, primitiveArrayCoercion);
    add(configuration, String.class, File.class, new Coercion<String, File>() {

        @Override
        public File coerce(String input) {
            return new File(input);
        }
    });
    add(configuration, String.class, TimeInterval.class, new Coercion<String, TimeInterval>() {

        @Override
        public TimeInterval coerce(String input) {
            return new TimeInterval(input);
        }
    });
    add(configuration, TimeInterval.class, Long.class, new Coercion<TimeInterval, Long>() {

        @Override
        public Long coerce(TimeInterval input) {
            return input.milliseconds();
        }
    });
    add(configuration, Object.class, Object[].class, new Coercion<Object, Object[]>() {

        @Override
        public Object[] coerce(Object input) {
            return new Object[] { input };
        }
    });
    add(configuration, Collection.class, Object[].class, new Coercion<Collection, Object[]>() {

        @Override
        public Object[] coerce(Collection input) {
            return input.toArray();
        }
    });
    CoercionTuple<Flow, List> flowToListCoercion = CoercionTuple.create(Flow.class, List.class, Flow::toList);
    configuration.add(flowToListCoercion.getKey(), flowToListCoercion);
    CoercionTuple<Flow, Boolean> flowToBooleanCoercion = CoercionTuple.create(Flow.class, Boolean.class, (i) -> !i.isEmpty());
    configuration.add(flowToBooleanCoercion.getKey(), flowToBooleanCoercion);
}
Also used : List(java.util.List) BigInteger(java.math.BigInteger) Collection(java.util.Collection) File(java.io.File) TimeInterval(org.apache.tapestry5.commons.util.TimeInterval) BigDecimal(java.math.BigDecimal) StringToEnumCoercion(org.apache.tapestry5.commons.util.StringToEnumCoercion) Coercion(org.apache.tapestry5.commons.services.Coercion) Flow(org.apache.tapestry5.func.Flow) BigInteger(java.math.BigInteger)

Aggregations

File (java.io.File)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Collection (java.util.Collection)1 List (java.util.List)1 Coercion (org.apache.tapestry5.commons.services.Coercion)1 StringToEnumCoercion (org.apache.tapestry5.commons.util.StringToEnumCoercion)1 TimeInterval (org.apache.tapestry5.commons.util.TimeInterval)1 Flow (org.apache.tapestry5.func.Flow)1