package com.atlassian.stash.internal.user;

import com.atlassian.bitbucket.permission.Permission;

import javax.annotation.Nonnull;

/**
 * A base class to simplify constructing permitted entities which automatically translates from a weight, provided to
 * the constructor, to a {@link Permission}.
 */
abstract class InternalPermittedEntity {

    protected final Permission permission;

    protected InternalPermittedEntity(int permissionWeight) {
        this.permission = Permission.fromWeight(permissionWeight);
        if (this.permission == null) {
            throw new IllegalArgumentException("No permission has weight [" + permissionWeight +
                    "]. This is an indication the database and enumeration have mismatched weights.");
        }
    }

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