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

tech.ydb.jdbc.settings.AbstractYdbProperty 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.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiConsumer;

import javax.annotation.Nullable;

public abstract class AbstractYdbProperty implements ToDriverPropertyInfo {

    private final String name;
    private final String description;
    @Nullable
    private final String defaultValue;
    private final Class type;
    private final PropertyConverter converter;
    private final BiConsumer setter;

    protected AbstractYdbProperty(String name,
                                  String description,
                                  @Nullable String defaultValue,
                                  Class type,
                                  PropertyConverter converter,
                                  BiConsumer setter) {
        this.name = Objects.requireNonNull(name);
        this.description = Objects.requireNonNull(description);
        this.defaultValue = defaultValue;
        this.type = Objects.requireNonNull(type);
        this.converter = Objects.requireNonNull(converter);
        this.setter = Objects.requireNonNull(setter);
    }

    public String getName() {
        return name;
    }

    @Nullable
    public String getDefaultValue() {
        return defaultValue;
    }

    public PropertyConverter getConverter() {
        return converter;
    }

    public Class getType() {
        return type;
    }

    public BiConsumer getSetter() {
        return setter;
    }

    @Override
    public DriverPropertyInfo toDriverPropertyInfo(@Nullable String value) {
        DriverPropertyInfo info = new DriverPropertyInfo(name,
                value != null ? value : defaultValue != null ? defaultValue : "");
        info.description = description;
        info.required = false;
        return info;
    }

    static class PropertiesCollector> {
        private final Map properties = new LinkedHashMap<>();

        protected void register(T property) {
            if (properties.put(property.getName(), property) != null) {
                throw new IllegalStateException("Internal error. Unable to register property with name " +
                        property.getName() + " twice");
            }
        }

        protected Collection properties() {
            return Collections.unmodifiableCollection(properties.values());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy