package com.stolets.rxdiffutil.diffrequest;

import android.support.annotation.NonNull;

import com.stolets.rxdiffutil.di.FragmentScope;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static com.stolets.rxdiffutil.internal.Preconditions.checkNotNull;

/**
 * Manages the diff requests lifecycle.
 */
@FragmentScope
public final class DiffRequestManager {
    @NonNull
    private Map<String, DiffRequest> mPendingRequests = new HashMap<>();
    @NonNull
    private Map<String, Disposable> mCurrentSubscriptions = new HashMap<>();
    @NonNull
    private CompositeDisposable mCompositeDisposable = new CompositeDisposable();

    /**
     * Returns a new {@link Single} that emits the {@link RxDiffResult}.
     *
     * @param diffRequest {@link DiffRequest} holding all parameters to start the difference calculation.
     * @return {@link Single}.
     */
    private static Single<RxDiffResult> single(@NonNull final DiffRequest diffRequest) {
        return Single.fromCallable(new Callable<RxDiffResult>() {
            @Override
            public RxDiffResult call() throws Exception {
                final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffRequest.getDiffCallback(), diffRequest.isDetectingMoves());
                return new RxDiffResult(diffRequest.getTag(), diffResult);
            }
        });
    }

    /**
     * Adds a new {@link DiffRequest} to the pending list.
     *
     * @param diffRequest A new {@link DiffRequest}.
     *                    Note: if a request with the same tag is already exists it will be replaced.
     */
    void addPendingRequest(@NonNull final DiffRequest diffRequest) {
        checkNotNull(diffRequest, "diffRequest must not be null!");
        mPendingRequests.put(diffRequest.getTag(), diffRequest);
    }

    /**
    /**
     * Stores {@link Disposable} reference so that it can be retrieved later using the given tag.
     *
     * @param disposable {@link Disposable}
     * @param tag        A {@link String} representing the tag the given disposable is associated with.
     */
    private void registerDisposable(@NonNull final Disposable disposable, @NonNull final String tag) {
        mCompositeDisposable.add(disposable);
        mCurrentSubscriptions.put(tag, disposable);
    }

    /**
     * Disposes the {@link Disposable} according to the given tag if the subscription is still in progress.
     *
     * @param tag A {@link String} identifying the diff request.
     */
    private void dispose(@NonNull final String tag) {
        final Disposable disposableForTag = mCurrentSubscriptions.get(tag);

        // Check if the subscription is still in progress, if so then dispose the disposable
        if (disposableForTag != null) {
            mCompositeDisposable.remove(disposableForTag);
            mCurrentSubscriptions.remove(tag);
        }
    }

    /**
     * @return A map holding all pending requests. Note: the returned map is unmodifiable.
     */
    @NonNull
    Map<String, DiffRequest> getPendingRequests() {
        return Collections.unmodifiableMap(mPendingRequests);
    }
}

@@ -3131,11 +3131,1356 @@
 );%0A    %7D
+%0A%0A    /**%0A     * The class used to handle %7B@link RxDiffResult%7D obtained from %7B@link Single%7D.%0A     */%0A    private static final class DiffResultSubscriber implements BiConsumer%3CRxDiffResult, Throwable%3E %7B%0A        @NonNull%0A        private final DefaultDiffCallback mDefaultDiffCallback;%0A%0A        /**%0A         * @param defaultDiffCallback %7B@link DefaultDiffCallback%7D instance or its subclass the %7B@link android.support.v7.util.DiffUtil.DiffResult%7D will be passed to.%0A         */%0A        DiffResultSubscriber(@NonNull final DefaultDiffCallback defaultDiffCallback) %7B%0A            checkNotNull(defaultDiffCallback, %22defaultDiffCallback must not be null!%22);%0A            this.mDefaultDiffCallback = defaultDiffCallback;%0A        %7D%0A%0A        @Override%0A        @UiThread%0A        public void accept(@io.reactivex.annotations.NonNull RxDiffResult rxDiffResult, @io.reactivex.annotations.NonNull Throwable throwable) throws Exception %7B%0A            if (throwable != null && throwable.getMessage() != null) %7B%0A                throw new IllegalStateException(%22Failed to calculate diff%22, throwable);%0A            %7D%0A%0A            if (rxDiffResult == null) %7B%0A                throw new IllegalStateException(%22rxDiffResult must not be null if the throwable is null too%22);%0A            %7D%0A%0A            mDefaultDiffCallback.update(rxDiffResult.getDiffResult());%0A        %7D%0A    %7D
 %0A%7D%0A

Implement DiffResultSubscriber as static nested class