package com.atlassian.bitbucket.mesh.transaction;

import com.atlassian.bitbucket.mesh.rpc.v1.RpcReplicaVersion;

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

import static java.util.Objects.requireNonNull;

public class ReplicaVersion {

    private final String contentHash;
    private final long version;

    public ReplicaVersion(@Nonnull String contentHash, long version) {
        this.contentHash = requireNonNull(contentHash, "contentHash");
        this.version = version;
    }

    @Nonnull
    public static ReplicaVersion fromReplicaVersion(@Nonnull RpcReplicaVersion rpc) {
        requireNonNull(rpc, "rpc");

        return new ReplicaVersion(rpc.getContentHash(), rpc.getVersion());
    }

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

    @Nonnull
    public String getContentHash() {
        return contentHash;
    }

    public long getVersion() {
        return version;
    }

    @Override
    public int hashCode() {
        return Objects.hash(contentHash, version);
    }

    @Override
    public String toString() {
        return "{content-hash=" + contentHash + ", version=" + version + "}";
    }
}
