All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.ydb.jdbc.settings.PropertyConverter Maven / Gradle / Ivy

There is a newer version: 2.3.5
Show newest version
package tech.ydb.jdbc.settings;

import java.sql.SQLException;
import java.time.Duration;
import java.time.format.DateTimeParseException;
import java.util.Locale;

import tech.ydb.jdbc.exception.YdbConfigurationException;

interface PropertyConverter {
    T convert(String value) throws SQLException;

    static PropertyConverter stringValue() {
        return value -> value;
    }

    static > PropertyConverter enumValue(Class clazz) {
        return value -> {
            for (E v: clazz.getEnumConstants()) {
                if (value.equalsIgnoreCase(v.name())) {
                    return v;
                }
            }
            return null;
        };
    }

    static PropertyConverter durationValue() {
        return value -> {
            String targetValue = "PT" + value.replace(" ", "").toUpperCase(Locale.ROOT);
            try {
                return Duration.parse(targetValue);
            } catch (DateTimeParseException e) {
                throw new YdbConfigurationException("Unable to parse value [" + value + "] -> [" +
                        targetValue + "] as Duration: " + e.getMessage(), e);
            }
        };
    }

    static PropertyConverter integerValue() {
        return value -> {
            try {
                return Integer.valueOf(value);
            } catch (NumberFormatException e) {
                throw new YdbConfigurationException("Unable to parse value [" + value + "] as Integer: " +
                        e.getMessage(), e);
            }
        };
    }

    static PropertyConverter booleanValue() {
        return Boolean::valueOf;
    }

    static PropertyConverter stringFileReference() {
        return YdbLookup::stringFileReference;
    }

    static PropertyConverter byteFileReference() {
        return YdbLookup::byteFileReference;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy