io.sidecar.credential.Credential.java Source code

Java tutorial

Introduction

Here is the source code for io.sidecar.credential.Credential.java

Source

/*
 * Copyright 2015 QSense, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sidecar.credential;

import com.google.common.base.CharMatcher;
import com.google.common.base.Preconditions;

import org.apache.commons.lang.StringUtils;

import java.util.Objects;

public final class Credential {

    private final String username;
    private final String password;

    public Credential(String username, String password) {

        Preconditions.checkNotNull(username);
        Preconditions.checkArgument(StringUtils.isNotBlank(username));
        Preconditions.checkArgument(CharMatcher.WHITESPACE.matchesNoneOf(username));
        this.username = username;

        Preconditions.checkNotNull(password);
        Preconditions.checkArgument(StringUtils.isNotBlank(password));
        Preconditions.checkArgument(CharMatcher.WHITESPACE.matchesNoneOf(password));
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    @Override
    public String toString() {
        return "Credential{" + "username=" + username + ", password=" + password + "} " + super.toString();
    }

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

        Credential that = (Credential) o;

        return Objects.equals(this.username, that.username) && Objects.equals(this.password, that.password);
    }

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