package com.atlassian.bitbucket.mesh.credentials;

import javax.annotation.Nonnull;
import java.util.Objects;

import static java.util.Objects.requireNonNull;

public class UsernamePasswordCredentials implements Credentials {

    private final String password;
    private final String username;

    public UsernamePasswordCredentials(@Nonnull String username, @Nonnull String password) {
        this.password = requireNonNull(password, "password");
        this.username = requireNonNull(username, "username");
    }

    @Override
    public <T> T accept(@Nonnull CredentialsVisitor<T> visitor) {
        return requireNonNull(visitor, "visitor").visit(this);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        UsernamePasswordCredentials that = (UsernamePasswordCredentials) o;
        return password.equals(that.password) &&
               username.equals(that.username);
    }

    @Nonnull
    public String getPassword() {
        return password;
    }

    @Nonnull
    public String getUsername() {
        return username;
    }

    @Override
    public int hashCode() {
        return Objects.hash(password, username);
    }

    @Override
    public String toString() {
        return "UsernamePasswordCredentials{" +
               "username='" + username + '\'' +
               '}';
    }
}
