package com.atlassian.stash.internal.idx;

import com.atlassian.bitbucket.validation.annotation.RequiredString;

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

/**
 * Describes an attribute associated with an {@link InternalIndexedCommit indexed commit}.
 */
@Embeddable
public class InternalCommitAttribute {

    @Column(name="att_name", nullable = false) @RequiredString(size=32)
    private final String name;
    
    @Column(name="att_value", nullable = false) @RequiredString(size=1024)
    private final String value;

    // for Hibernate
    protected InternalCommitAttribute() {
        name = null;
        value = null;
    }

    public InternalCommitAttribute(@Nonnull String property, @Nonnull String value) {
        this.name = property;
        this.value = value;
    }

    @Nonnull
    public String getName() {
        return name;
    }

    @Nonnull
    public String getValue() {
        return value;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o instanceof InternalCommitAttribute) {
            InternalCommitAttribute that = (InternalCommitAttribute) o;

            return Objects.equals(getName(), that.getName()) && Objects.equals(getValue(), that.getValue());
        }

        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(getName(), getValue());
    }
}
