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

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

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

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



class YdbProperty {
    private interface Parser {
        T parse(String value) throws SQLException;
    }

    private final String name;
    private final String description;
    private final String defaultValue;
    private final Class clazz;
    private final Parser parser;

    private YdbProperty(String name, String description, String defaultValue, Class clazz, Parser parser) {
        this.name = Objects.requireNonNull(name);
        this.description = Objects.requireNonNull(description);
        this.defaultValue = defaultValue;
        this.clazz = clazz;
        this.parser = Objects.requireNonNull(parser);
    }

    public String getName() {
        return name;
    }

    public YdbValue readValue(Properties props) throws SQLException {
        Object value = props.get(name);
        if (value == null) {
            if (defaultValue == null || defaultValue.isEmpty()) {
                return new YdbValue<>(false, "", null);
            } else {
                return new YdbValue<>(false, defaultValue, parser.parse(defaultValue));
            }
        }

        if (value instanceof String) {
            try {
                String stringValue = (String) value;
                return new YdbValue<>(true, stringValue, parser.parse(stringValue));
            } catch (RuntimeException e) {
                throw new SQLException("Unable to convert property " + name + ": " + e.getMessage(), e);
            }
        } else {
            if (clazz.isAssignableFrom(value.getClass())) {
                T typed = clazz.cast(value);
                return new YdbValue<>(true, typed.toString(), typed);
            } else {
                throw new SQLException("Invalid object property " + name + ", must be " + clazz +
                        ", got " + value.getClass());
            }
        }
    }

    DriverPropertyInfo toInfo(Properties values) throws SQLException {
        YdbValue value = readValue(values);
        DriverPropertyInfo info = new DriverPropertyInfo(name, value.rawValue());
        info.description = description;
        info.required = false;
        return info;
    }

    public static YdbProperty string(String name, String description) {
        return string(name, description, null);
    }

    public static YdbProperty string(String name, String description, String defaultValue) {
        return new YdbProperty<>(name, description, defaultValue, String.class, v -> v);
    }

    public static YdbProperty bool(String name, String description, boolean defaultValue) {
        return bool(name, description, String.valueOf(defaultValue));
    }

    public static YdbProperty bool(String name, String description) {
        return bool(name, description, null);
    }

    private static YdbProperty bool(String name, String description, String defaultValue) {
        return new YdbProperty<>(name, description, defaultValue, Boolean.class, Boolean::valueOf);
    }

    public static YdbProperty integer(String name, String description) {
        return integer(name, description, null);
    }

    public static YdbProperty integer(String name, String description, int defaultValue) {
        return integer(name, description, String.valueOf(defaultValue));
    }

    private static YdbProperty integer(String name, String description, String defaultValue) {
        return new YdbProperty<>(name, description, defaultValue, Integer.class, value -> {
            try {
                return Integer.valueOf(value);
            } catch (NumberFormatException e) {
                throw new RuntimeException("Unable to parse value [" + value + "] as Integer: " +
                        e.getMessage(), e);
            }
        });
    }

    public static > YdbProperty enums(String name, Class clazz, String description) {
        return enums(name, description, clazz, null);
    }

    public static > YdbProperty enums(String name, Class clazz, String description, E def) {
        return enums(name, description, clazz, def.toString());
    }

    private static > YdbProperty enums(String name, String desc, Class clazz, String def) {
        return new YdbProperty<>(name, desc, def, clazz, value -> {
            for (E v: clazz.getEnumConstants()) {
                if (v.name().equalsIgnoreCase(value)) {
                    return v;
                }
            }
            return null;
        });
    }

    public static YdbProperty duration(String name, String description) {
        return duration(name, description, null);
    }

    public static YdbProperty duration(String name, String description, String defaultValue) {
        return new YdbProperty<>(name, description, defaultValue, Duration.class, value -> {
            String targetValue = "PT" + value.replace(" ", "").toUpperCase(Locale.ROOT);
            try {
                return Duration.parse(targetValue);
            } catch (DateTimeParseException e) {
                throw new RuntimeException("Unable to parse value [" + value + "] -> [" +
                        targetValue + "] as Duration: " + e.getMessage(), e);
            }
        });
    }

    @Deprecated
    public static YdbProperty bytes(String name, String description) {
        return new YdbProperty<>(name, description, null, byte[].class, YdbLookup::byteFileReference);
    }

    public static YdbProperty content(String name, String description) {
        return new YdbProperty<>(name, description, null, String.class, YdbLookup::stringFileReference);
    }

    public static YdbProperty file(String name, String description) {
        return new YdbProperty<>(name, description, null, String.class, v -> YdbLookup.readFileAsString(name, v));
    }

    public static YdbProperty fileBytes(String name, String description) {
        return new YdbProperty<>(name, description, null, byte[].class, v -> YdbLookup.readFileAsBytes(name, v));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy