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

com.jn.sqlhelper.common.connection.NamedConnectionConfiguration Maven / Gradle / Ivy

package com.jn.sqlhelper.common.connection;

import com.jn.easyjson.core.util.JSONs;
import com.jn.langx.configuration.Configuration;
import com.jn.langx.util.Objs;
import com.jn.langx.util.collection.Collects;
import com.jn.langx.util.collection.diff.MapDiffResult;
import com.jn.langx.util.function.Consumer2;

import java.util.Map;
import java.util.Properties;

public class NamedConnectionConfiguration extends ConnectionConfiguration implements Configuration, Cloneable {
    private String name;

    public NamedConnectionConfiguration() {

    }

    public NamedConnectionConfiguration(ConnectionConfiguration configuration) {
        setDriver(configuration.getDriver());
        setUser(configuration.getUser());
        setPassword(configuration.getPassword());
        setUrl(configuration.getUrl());
        final Properties props = new Properties();
        Collects.forEach(configuration.getDriverProps(), new Consumer2() {
            @Override
            public void accept(Object key, Object value) {
                props.setProperty(key.toString(), value.toString());
            }
        });

        setDriverProps(props);
    }

    @Override
    public void setId(String id) {
        name = id;
    }

    @Override
    public String getId() {
        return name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    @Override
    public String toString() {
        return JSONs.toJson(this);
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof NamedConnectionConfiguration)) {
            return false;
        }
        NamedConnectionConfiguration o2 = (NamedConnectionConfiguration) obj;
        if (!name.equals(o2.getName()) || !getDriver().equals(o2.getDriver()) || !getId().equals(o2.getId()) || !getUrl().equals(o2.getUrl()) || !getUser().equals(o2.getUser())) {
            return false;
        }

        if (getPassword() != null) {
            if (!getPassword().equals(o2.getPassword())) {
                return false;
            }
        } else {
            if (o2.getPassword() != null) {
                return false;
            }
        }

        Map map1 = Collects.propertiesToStringMap(getDriverProps(), true);
        Map map2 = Collects.propertiesToStringMap(o2.getDriverProps(), true);
        MapDiffResult diffResult = Collects.diff(map1, map2);
        return diffResult.getAdds().isEmpty() && diffResult.getRemoves().isEmpty() && diffResult.getUpdates().isEmpty();
    }

    @Override
    public int hashCode() {
        return Objs.hash(this.name, getUrl(), getUrl(), getPassword(), getDriver(), getDriverProps());
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        NamedConnectionConfiguration conn = new NamedConnectionConfiguration(this);
        conn.setName(name);
        conn.setId(name);
        return conn;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy