package info.izumin.android.bletia;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import info.izumin.android.bletia.action.Action;
import info.izumin.android.bletia.wrapper.BluetoothGattWrapper;

/**
 * Created by izumin on 10/3/15.
 */
public class ActionQueue<A extends Action<?, I>, I> {

    private List<A> mWaitingActionList;
    private Map<I, A> mRunningActionMap;

    public ActionQueue() {
        mWaitingActionList = new ArrayList<>();
        mRunningActionMap = new HashMap<>();
    }

    public synchronized boolean enqueue(A action) {
        return mWaitingActionList.add(action);
    }

    public synchronized boolean execute(I identity, BluetoothGattWrapper gattWrapper) {
        if (isRunning(identity)) {
            return false;
        } else {
            for (A action : mWaitingActionList) {
                if ((identity == null) ? (action.getIdentity() == null) : (identity.equals(action.getIdentity()))) {
                    action.execute(gattWrapper);
                    mWaitingActionList.remove(action);
                    mRunningActionMap.put(action.getIdentity(), action);
                    return true;
                }
            }
            throw new IllegalArgumentException();
        }
    }

    public synchronized A dequeue(I identity) {
        return mRunningActionMap.remove(identity);
    }

    public boolean isRunning(I identity) {
        return mRunningActionMap.containsKey(identity);
    }
}

@@ -1012,92 +1012,102 @@
     
-action.execute(gattWrapper);%0A                    mWaitingActionList.remove(action);%0A
+mWaitingActionList.remove(action);%0A                    if (action.execute(gattWrapper)) %7B%0A    
     
@@ -1191,24 +1191,28 @@
             
+    
 return true;
@@ -1212,16 +1212,105 @@
 n true;%0A
+                    %7D else %7B%0A                        return false;%0A                    %7D%0A
         

Fix ActionQueue implementation