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

io.github.mfvanek.pg.connection.ConnectionCredentials Maven / Gradle / Ivy

Go to download

pg-index-health-jdbc-connection is an abstraction of a connection to a high availability PostgreSQL cluster.

The newest version!
/*
 * Copyright (c) 2019-2024. Ivan Vakhrushev and others.
 * https://github.com/mfvanek/pg-index-health
 *
 * This file is a part of "pg-index-health" - a Java library for
 * analyzing and maintaining indexes health in PostgreSQL databases.
 *
 * Licensed under the Apache License 2.0
 */

package io.github.mfvanek.pg.connection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;

/**
 * Parameters for connecting to the database.
 *
 * @author Ivan Vakhrushev
 */
@Immutable
public class ConnectionCredentials {

    private final SortedSet connectionUrls;
    private final String userName;
    private final String password;

    private ConnectionCredentials(@Nonnull final Collection connectionUrls,
                                  @Nonnull final String userName,
                                  @Nonnull final String password) {
        final List defensiveCopy = new ArrayList<>(Objects.requireNonNull(connectionUrls, "connectionUrls cannot be null"));
        PgConnectionValidators.connectionUrlsNotEmptyAndValid(defensiveCopy);
        this.connectionUrls = Collections.unmodifiableSortedSet(new TreeSet<>(defensiveCopy));
        this.userName = PgConnectionValidators.userNameNotBlank(userName);
        this.password = PgConnectionValidators.passwordNotBlank(password);
    }

    /**
     * Gets a set of connection strings for accessing all hosts in the database cluster.
     *
     * @return connection urls
     */
    @Nonnull
    public Collection getConnectionUrls() {
        return connectionUrls;
    }

    /**
     * Gets the name of the user to connect to the database.
     *
     * @return the name of the user
     */
    @Nonnull
    public String getUserName() {
        return userName;
    }

    /**
     * Gets the user's password for connecting to the database.
     *
     * @return the user's password
     */
    @Nonnull
    public String getPassword() {
        return password;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean equals(final Object other) {
        if (this == other) {
            return true;
        }

        if (!(other instanceof ConnectionCredentials)) {
            return false;
        }

        final ConnectionCredentials that = (ConnectionCredentials) other;
        return Objects.equals(connectionUrls, that.connectionUrls) &&
            Objects.equals(userName, that.userName) &&
            Objects.equals(password, that.password);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final int hashCode() {
        return Objects.hash(connectionUrls, userName, password);
    }

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    public String toString() {
        return ConnectionCredentials.class.getSimpleName() + '{' +
            "connectionUrls=" + connectionUrls +
            ", userName='" + userName + '\'' +
            ", password='" + password + '\'' +
            '}';
    }

    @Nonnull
    public static ConnectionCredentials of(@Nonnull final Collection connectionUrls,
                                           @Nonnull final String userName,
                                           @Nonnull final String password) {
        return new ConnectionCredentials(connectionUrls, userName, password);
    }

    @Nonnull
    public static ConnectionCredentials ofUrl(@Nonnull final String writeUrl,
                                              @Nonnull final String userName,
                                              @Nonnull final String password) {
        final Set connectionUrls = Set.of(PgConnectionValidators.pgUrlNotBlankAndValid(writeUrl, "writeUrl"));
        return new ConnectionCredentials(connectionUrls, userName, password);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy