static T convert()

in java-client/src/main/java/co/elastic/clients/elasticsearch/_helpers/esql/jdbc/TypeConverter.java [123:202]


    static <T> T convert(Object val, EsType columnType, Class<T> type, String typeString) throws SQLException {
        if (type == null) {
            return (T) convert(val, columnType, typeString);
        }

        // if the value type is the same as the target, no conversion is needed
        // make sure though to check the internal type against the desired one
        // since otherwise the internal object format can leak out
        // (for example dates when longs are requested or intervals for strings)
        if (type.isInstance(val) && TypeUtils.classOf(columnType) == type) {
            try {
                return type.cast(val);
            } catch (ClassCastException cce) {
                failConversion(val, columnType, typeString, type, cce);
            }
        }

        if (type == String.class) {
            return (T) asString(convert(val, columnType, typeString));
        }
        if (type == Boolean.class) {
            return (T) asBoolean(val, columnType, typeString);
        }
        if (type == Byte.class) {
            return (T) asByte(val, columnType, typeString);
        }
        if (type == Short.class) {
            return (T) asShort(val, columnType, typeString);
        }
        if (type == Integer.class) {
            return (T) asInteger(val, columnType, typeString);
        }
        if (type == Long.class) {
            return (T) asLong(val, columnType, typeString);
        }
        if (type == BigInteger.class) {
            return (T) asBigInteger(val, columnType, typeString);
        }
        if (type == Float.class) {
            return (T) asFloat(val, columnType, typeString);
        }
        if (type == Double.class) {
            return (T) asDouble(val, columnType, typeString);
        }
        if (type == Date.class) {
            return (T) asDate(val, columnType, typeString);
        }
        if (type == Time.class) {
            return (T) asTime(val, columnType, typeString);
        }
        if (type == Timestamp.class) {
            return (T) asTimestamp(val, columnType, typeString);
        }
        if (type == byte[].class) {
            return (T) asByteArray(val, columnType, typeString);
        }
        if (type == BigDecimal.class) {
            return (T) asBigDecimal(val, columnType, typeString);
        }
        //
        // JDK 8 types
        //
        if (type == LocalDate.class) {
            return (T) asLocalDate(val, columnType, typeString);
        }
        if (type == LocalTime.class) {
            return (T) asLocalTime(val, columnType, typeString);
        }
        if (type == LocalDateTime.class) {
            return (T) asLocalDateTime(val, columnType, typeString);
        }
        if (type == OffsetTime.class) {
            return (T) asOffsetTime(val, columnType, typeString);
        }
        if (type == OffsetDateTime.class) {
            return (T) asOffsetDateTime(val, columnType, typeString);
        }

        return failConversion(val, columnType, typeString, type);
    }