package com.stolets.rxdiffutil.diffrequest;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.stolets.rxdiffutil.RxDiffResult;

import java.lang.ref.WeakReference;

import io.reactivex.Single;

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

/**
 * This class is used as a wrapper around {@link DiffRequestManager} to prevent memory leaks.
 * <p>
 * Note: The underlying {@link DiffRequestManager} will be GCd when the activity passed to {@link DiffRequestBuilder} is finished completely (not just due a configuration change).
 * </p>
 */
@SuppressWarnings("WeakerAccess")
public final class DiffRequestManagerWrapper {
    @NonNull
    private final WeakReference<DiffRequestManager> mDiffRequestManagerWeakRef;
    @NonNull
    private final String mTag;

    /**
     * Constructs a wrapper that holds a weak reference to {@link DiffRequestManager}.
     * @param diffRequestManager {@link DiffRequestManager}.
     * @param tag A tag that is passed to the underlying {@link DiffRequestManager} to handle the appropriate request.
     */
    public DiffRequestManagerWrapper(@NonNull final DiffRequestManager diffRequestManager, @NonNull final String tag) {
        checkNotNull(diffRequestManager, "diffRequestManager must not be null!");
        checkNotNull(tag, "tag string must not be null!");
        checkArgument(!tag.isEmpty(), "tag string must not be empty!");

        this.mDiffRequestManagerWeakRef = new WeakReference<>(diffRequestManager);
        this.mTag = tag;
    }

    /**
     * Starts the difference calculation in accordance with parameters specified in {@link DiffRequestBuilder}.
     *
     * @return {@link Single} you can subscribe to or null if the activity has been finished. It also returns null if you have passed {@link com.stolets.rxdiffutil.DefaultDiffCallback} to the builder, in this case the result is handled internally automatically.
     */
    @Nullable
    public Single<RxDiffResult> calculate() {
        final DiffRequestManager diffRequestManager = mDiffRequestManagerWeakRef.get();
        return diffRequestManager != null ? diffRequestManager.execute(mTag) : null;
    }

    /**
     * @return The tag you specified in {@link DiffRequestBuilder}.
     */
    @NonNull
    public String getTag() {
        return mTag;
    }
}

@@ -84,52 +84,8 @@
 ull;
-%0Aimport android.support.annotation.Nullable;
 %0A%0Aim
@@ -131,45 +131,8 @@
 t;%0A%0A
-import java.lang.ref.WeakReference;%0A%0A
 impo
@@ -306,16 +306,17 @@
 tNull;%0A%0A
+%0A
 /**%0A * T
@@ -387,224 +387,50 @@
  to 
-prevent memory leaks.%0A * %3Cp%3E%0A * Note: The underlying %7B@link DiffRequestManager%7D will be GCd when the activity passed to %7B@link DiffRequestBuilder%7D is finished completely (not just due a configuration change).%0A * %3C/p%3E
+handle a request with the appropriate tag.
 %0A */
@@ -542,30 +542,16 @@
 e final 
-WeakReference%3C
 DiffRequ
@@ -560,17 +560,16 @@
 tManager
-%3E
  mDiffRe
@@ -580,23 +580,16 @@
 tManager
-WeakRef
 ;%0A    @N
@@ -667,38 +667,14 @@
 per 
-that holds a weak reference to
+around
  %7B@l
@@ -690,32 +690,39 @@
 equestManager%7D.%0A
+     *%0A
      * @param di
@@ -784,16 +784,31 @@
 ram tag 
+               
 A tag th
@@ -1278,38 +1278,11 @@
 ager
-WeakRef = new WeakReference%3C%3E(
+ = 
 diff
@@ -1295,17 +1295,16 @@
 tManager
-)
 ;%0A      

Refactor weak reference to diff request manager