private ConverterFactory()

in cayenne/src/main/java/org/apache/cayenne/reflect/ConverterFactory.java [46:239]


    private ConverterFactory() {

        Converter<String> toStringConverter = (object, type) -> object != null ? object.toString() : null;

        Converter<Boolean> toBooleanConverter = (object, type) -> {
            if (object == null) {
                return type.isPrimitive() ? Boolean.FALSE : null;
            }

            if (object instanceof Boolean) {
                return (Boolean) object;
            } else if (object instanceof Integer || object instanceof Long || object instanceof Short || object instanceof Byte) {
                if (((Number) object).longValue() == 0) {
                    return Boolean.FALSE;
                } else if (((Number) object).longValue() == 1) {
                    return Boolean.TRUE;
                }
            }

            return "true".equalsIgnoreCase(object.toString())
                    ? Boolean.TRUE
                    : Boolean.FALSE;
        };

        Converter<Long> toLongConverter = (object, type) -> {
            if (object == null) {
                return type.isPrimitive() ? 0L : null;
            }

            if (object instanceof Long) {
                return (Long) object;
            }

            return Long.valueOf(object.toString());
        };

        Converter<Integer> toIntConverter = (object, type) -> {
            if (object == null) {
                return type.isPrimitive() ? 0 : null;
            }

            if (object instanceof Integer) {
                return (Integer) object;
            }

            return Integer.valueOf(object.toString());
        };

        Converter<Byte> toByteConverter = (object, type) -> {
            if (object == null) {
                return type.isPrimitive() ? (byte) 0 : null;
            }

            if (object instanceof Byte) {
                return (Byte) object;
            }

            return Byte.valueOf(object.toString());
        };

        Converter<Short> toShortConverter = (object, type) -> {
            if (object == null) {
                return type.isPrimitive() ? (short) 0 : null;
            }

            if (object instanceof Short) {
                return (Short) object;
            }

            return Short.valueOf(object.toString());
        };

        Converter<Character> toCharConverter = (object, type) -> {
            if (object == null) {
                return type.isPrimitive() ? (char) 0 : null;
            }

            if (object instanceof Character) {
                return (Character) object;
            }

            String string = object.toString();
            return string.length() > 0 ? string.charAt(0) : 0;
        };

        Converter<Double> toDoubleConverter = (object, type) -> {
            if (object == null) {
                return type.isPrimitive() ? 0.0d : null;
            }

            if (object instanceof Double) {
                return (Double) object;
            }

            return Double.valueOf(object.toString());
        };

        Converter<Float> toFloatConverter = (object, type) -> {
            if (object == null) {
                return type.isPrimitive() ? 0.0f : null;
            }

            if (object instanceof Float) {
                return (Float) object;
            }

            return Float.valueOf(object.toString());
        };

        Converter<BigDecimal> toBigDecimalConverter = (object, type) -> {
            if (object == null) {
                return null;
            }

            if (object instanceof BigDecimal) {
                return (BigDecimal) object;
            }

            return new BigDecimal(object.toString());
        };

        Converter<BigInteger> toBigIntegerConverter = (object, type) -> {
            if (object == null) {
                return null;
            }

            if (object instanceof BigInteger) {
                return (BigInteger) object;
            }

            return new BigInteger(object.toString());
        };

        Converter<Date> toDateConverter = (value, type) -> {
            if (value == null) {
                return null;
            }
            if (value instanceof Date) {
                return (Date) value;
            }
            if (value instanceof Number) {
                return new Date(((Number) value).longValue());
            }
            return new Date(value.toString());
        };

        Converter<Timestamp> toTimestampConverter = (value, type) -> {
            if (value == null) {
                return null;
            }
            if (value instanceof Timestamp) {
                return (Timestamp) value;
            }
            if (value instanceof Number) {
                return new Timestamp(((Number) value).longValue());
            }
            return new Timestamp(Date.parse(value.toString()));
        };

        // TODO: byte[] converter...

        converters = new HashMap<>();

        _addConverter(Boolean.class, toBooleanConverter);
        _addConverter(boolean.class, toBooleanConverter);

        _addConverter(Short.class, toShortConverter);
        _addConverter(short.class, toShortConverter);

        _addConverter(Byte.class, toByteConverter);
        _addConverter(byte.class, toByteConverter);

        _addConverter(Integer.class, toIntConverter);
        _addConverter(int.class, toIntConverter);

        _addConverter(Long.class, toLongConverter);
        _addConverter(long.class, toLongConverter);
        
        _addConverter(Double.class, toDoubleConverter);
        _addConverter(double.class, toDoubleConverter);

        _addConverter(Float.class, toFloatConverter);
        _addConverter(float.class, toFloatConverter);

        _addConverter(Character.class, toCharConverter);
        _addConverter(char.class, toCharConverter);

        _addConverter(BigDecimal.class, toBigDecimalConverter);
        _addConverter(BigInteger.class, toBigIntegerConverter);
        _addConverter(Number.class, toBigDecimalConverter);
        _addConverter(String.class, toStringConverter);
        _addConverter(Date.class, toDateConverter);
        _addConverter(Timestamp.class, toTimestampConverter);
    }