package com.atlassian.bitbucket.mesh.git;

import io.grpc.stub.ServerCallStreamObserver;
import io.grpc.stub.StreamObserver;

import javax.annotation.Nonnull;
import java.util.function.BooleanSupplier;

import static java.util.Objects.requireNonNull;

abstract class AbstractObserverCallback<T> {

    static final int FRAGMENT_SIZE = 50;

    protected final StreamObserver<T> observer;

    private final BooleanSupplier cancelState;

    AbstractObserverCallback(@Nonnull StreamObserver<T> observer) {
        this.observer = requireNonNull(observer, "observer");

        cancelState = (observer instanceof ServerCallStreamObserver) ?
                ((ServerCallStreamObserver<?>) observer)::isCancelled :
                () -> Boolean.FALSE;
    }

    boolean isCanceled() {
        return cancelState.getAsBoolean();
    }
}
