package com.atlassian.bitbucket.scm;

import com.atlassian.bitbucket.RequestCanceledException;
import com.atlassian.bitbucket.i18n.KeyedMessage;
import com.atlassian.bitbucket.repository.Ref;
import com.atlassian.bitbucket.repository.Repository;

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

import static java.util.Objects.requireNonNull;

/**
 * Thrown when {@link ScmExtendedCommandFactory#merge(MergeCommandParameters)} merge operation} is canceled by an
 * event listener.
 * <p>
 * The processing for canceling a merge operation is <i>SCM-specific</i>, including the event that must be canceled
 * to trigger it. However, it shall be considered part of the contract for this exception that <i>no refs</i> in the
 * target repository may be updated prior to throwing it. A canceled merge should not update the target repository.
 */
public class MergeCanceledException extends RequestCanceledException {

    private final Ref from;
    private final Repository repository;
    private final Ref to;

    public MergeCanceledException(@Nonnull KeyedMessage message, @Nonnull List<KeyedMessage> cancelMessages,
                                  @Nonnull Repository repository, @Nonnull Ref from, @Nonnull Ref to) {
        super(message, cancelMessages);
        this.from = requireNonNull(from, "from");
        this.repository = requireNonNull(repository, "repository");
        this.to = requireNonNull(to, "to");
    }

    @Nonnull
    public Ref getFrom() {
        return from;
    }

    @Nonnull
    public Repository getRepository() {
        return repository;
    }

    @Nonnull
    public Ref getTo() {
        return to;
    }
}
