package com.atlassian.bitbucket.permission;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;

import javax.annotation.Nonnull;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Base class for simple implementations of {@link EffectivePermission effective permissions}
 *
 * @since 4.1
 */
public abstract class SimpleEffectivePermissionBase {

    protected final Permission permission;

    public SimpleEffectivePermissionBase(@Nonnull Permission permission) {
        this.permission = checkNotNull(permission, "permission");
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj != null && getClass().equals(obj.getClass())) {
            return Objects.equal(permission, ((SimpleEffectivePermissionBase) obj).permission);
        }

        return false;
    }

    @Nonnull
    public Permission getPermission() {
        return permission;
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .addValue(permission)
                .toString();
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(permission);
    }
}
