package com.atlassian.stash.internal.plugin;

import com.atlassian.bitbucket.util.CustomPreconditions;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "plugin_state")
public class PluginState {

    @Column(name = "enabled", nullable = false)
    private final boolean enabled;

    @Id
    @Column(name = "name", nullable = false)
    private final String name;

    // for hibernate
    protected PluginState() {
        name = null;
        enabled = false;
    }

    public PluginState(String name, boolean enabled) {
        CustomPreconditions.checkRequiredString(name, 255);
        this.name = name;
        this.enabled = enabled;
    }

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

        PluginState that = (PluginState) o;

        return name != null ? name.equals(that.name) : that.name == null;
    }

    public String getName() {
        return name;
    }

    @Override
    public int hashCode() {
        return name != null ? name.hashCode() : 0;
    }

    public boolean isEnabled() {
        return enabled;
    }

}
