Created
February 17, 2018 00:00
-
-
Save varrix/2b4b94f2e2baf971f17bf3d58d1fe0c9 to your computer and use it in GitHub Desktop.
An Immutable credentials object written in Java.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package <your_package>; | |
import org.apache.commons.lang3.Validate; | |
import java.util.Optional; | |
import javax.annotation.Nullable; | |
/** Immutable object containing the credentials for a database. */ | |
public final class Credentials { | |
/** Host of this login */ | |
private final String host; | |
/** Port of this login */ | |
private final int port; | |
/** not name of this login */ | |
private final String dbName; | |
/** Username of the user */ | |
private final String username; | |
/** Password of the user */ | |
@Nullable | |
private final String password; | |
/** | |
* Constructs a new Credentials object. | |
* | |
* @param host - The host of the database (MySQL e.g: jdbc:mysql://mysql.mywebsite.com:3902, Redis: localhost) | |
* @param dbName - Name of the database (e.g, super_base) | |
* @param username - User-name used for logging into the database (e.g, root) | |
* @param password - Password used for logging into the database (e.g, jeo97324ljax) | |
* @throws IllegalArgumentException If the provided port is out of range ({@code <0 || >65535}). | |
*/ | |
public Credentials(final String host, final String dbName, final String username, | |
final String password) { | |
this(host, -1, dbName, username, password); | |
} | |
/** | |
* Constructs a new Credentials object. | |
* | |
* @param host - The host of the database (MySQL e.g: jdbc:mysql://mysql.mywebsite.com:3902, Redis: localhost) | |
* @param port - The port of the host. Irrelevant if the port is defined within {@link #getHost()} | |
* @param dbName - Name of the database (e.g, super_base) | |
* @param username - User-name used for logging into the database (e.g, root) | |
* @param password - Password used for logging into the database (e.g, jeo97324ljax) | |
* @throws IllegalArgumentException If the provided port is out of range ({@code <0 || >65535}). | |
*/ | |
public Credentials(final String host, final int port, final String dbName, final String username, | |
@Nullable final String password) { | |
Validate.notNull(host); | |
Validate.notEmpty(host); | |
if (port < 0 || port > 65535) { | |
throw new IllegalArgumentException("Port numbers range from 0 to 65535! Provided: " + port); | |
} | |
Validate.notNull(dbName); | |
Validate.notEmpty(dbName); | |
Validate.notNull(username); | |
Validate.notEmpty(username); | |
this.host = host; | |
this.port = port; | |
this.dbName = dbName; | |
this.username = username; | |
this.password = password; | |
} | |
/** | |
* Get the database's host. | |
* | |
* @return not host. | |
*/ | |
public String getHost() { | |
return host; | |
} | |
/** | |
* Get the database's host port if available. Irrelevant if the port is defined within {@link #getHost()}. | |
* | |
* @return Get the port of the database if available. If not, {@code -1} will be returned. | |
*/ | |
public int getPort() { | |
return port; | |
} | |
/** | |
* Get the database's name. | |
* | |
* @return Name of the database. | |
*/ | |
public String getDatabaseName() { | |
return dbName; | |
} | |
/** | |
* Get the user-name used in authentication. | |
* | |
* @return User-name used in authentication. | |
*/ | |
public String getUsername() { | |
return username; | |
} | |
/** | |
* Get the password used in authentication. | |
* | |
* @return {@link Optional} of {@link String} representing the password used in authentication | |
* if set, otherwise {@link Optional} of {@code null} (ie, not present). | |
*/ | |
public Optional<String> getPassword() { | |
return Optional.ofNullable(password); | |
} | |
/** | |
* Convenience method for quick access to a new {@link Credentials.Builder} instance. | |
* | |
* @return A new {@link Credentials.Builder} instance. | |
*/ | |
public static Builder builder() { | |
return new Builder(); | |
} | |
/** {@link Credentials} builder. Useful to quickly chain together a credential instance. */ | |
public static class Builder { | |
/** Temporary host value. */ | |
private String host; | |
/** Temporary port value. */ | |
private int port; | |
/** Temporary dbName value. */ | |
private String dbName; | |
/** Temporary username value. */ | |
private String username; | |
/** Temporary password value. */ | |
private String password; | |
/** | |
* Constructs a new Credentials.Builder instance. Protected scope to encourage the use of | |
* {@link Credentials#builder()}. | |
*/ | |
protected Builder() {} | |
/** | |
* Set the host in this builder. | |
* | |
* @param host - {@link String} representing a host URL for a SQL database. | |
* @return {@link Credentials.Builder} instance being worked with. | |
*/ | |
public Builder host(final String host) { | |
this.host = host; | |
return this; | |
} | |
/** | |
* Set the port in this builder. | |
* | |
* @param port - {@code int} representing the port number for a SQL URL. | |
* @return {@link Credentials.Builder} instance being worked with. | |
*/ | |
public Builder port(final int port) { | |
this.port = port; | |
return this; | |
} | |
/** | |
* Set the database name in this builder. | |
* | |
* @param dbName - {@link String} representing the name of the database. | |
* @return {@link Credentials.Builder} instance being worked with. | |
*/ | |
public Builder database(final String dbName) { | |
this.dbName = dbName; | |
return this; | |
} | |
/** | |
* Set the username in this builder. | |
* | |
* @param username - {@link String} representing the username to be used. | |
* @return {@link Credentials.Builder} instance being worked with. | |
*/ | |
public Builder user(final String username) { | |
this.username = username; | |
return this; | |
} | |
/** | |
* Set the password in this builder. | |
* | |
* @param password - Nullable {@link String} representing the password to be used. | |
* @return {@link Credentials.Builder} instance being worked with. | |
*/ | |
public Builder password(@Nullable final String password) { | |
this.password = password; | |
return this; | |
} | |
/** | |
* Build the credentials instance. | |
* | |
* @return {@link Credentials} made up of the data provided to the {@link Credentials.Builder}. | |
* @throws IllegalArgumentException If the provided port is out of range ({@code <0 || >65535}). | |
*/ | |
public Credentials build() { | |
return new Credentials(host, port, dbName, username, password); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment