package com.datdo.mobilib.util;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 * TBD
 * </pre>
 */
public class MblSerializer {

    /**
     * <pre>
     * TBD
     * </pre>
     */
    public static interface Task {
        public void run(Runnable finishCallback);
    }

    private static final List<Task> sTasks              = new ArrayList<Task>();
    private static boolean          sIsRunning          = false;
    private static final Runnable   sFinishCallback     = new Runnable() {
        @Override
        public void run() {
            sIsRunning = false;
            runNextTask();
        }
    };

    private static void runNextTask() {
        synchronized (MblSerializer.class) {
            if (!sIsRunning && !sTasks.isEmpty()) {
                sIsRunning = true;
                sTasks.remove(0).run(sFinishCallback);
            }
        }
    }

    /**
     * <pre>
     * TBD
     * </pre>
     * @param task
     */
    public static void run(Task task) {
        synchronized (MblSerializer.class) {
            sTasks.add(task);
            runNextTask();
        }
    }
}

@@ -296,31 +296,24 @@
     private 
-static 
 final List%3CT
@@ -317,17 +317,20 @@
 t%3CTask%3E 
-s
+   m
 Tasks   
@@ -377,23 +377,16 @@
 private 
-static 
 boolean 
@@ -386,33 +386,36 @@
 oolean          
-s
+   m
 IsRunning       
@@ -438,23 +438,16 @@
 private 
-static 
 final Ru
@@ -455,17 +455,20 @@
 nable   
-s
+   m
 FinishCa
@@ -547,33 +547,33 @@
 ) %7B%0A            
-s
+m
 IsRunning = fals
@@ -632,23 +632,16 @@
 private 
-static 
 void run
@@ -675,34 +675,19 @@
 onized (
-MblSerializer.clas
+thi
 s) %7B%0A   
@@ -700,17 +700,17 @@
    if (!
-s
+m
 IsRunnin
@@ -715,17 +715,17 @@
 ing && !
-s
+m
 Tasks.is
@@ -751,17 +751,17 @@
         
-s
+m
 IsRunnin
@@ -782,25 +782,237 @@
             
-s
+// post to main thread to prevent StackOverFlow%0A                MblUtils.getMainThreadHandler().post(new Runnable() %7B%0A                    @Override%0A                    public void run() %7B%0A                        m
 Tasks.remove
@@ -1019,17 +1019,17 @@
 (0).run(
-s
+m
 FinishCa
@@ -1033,24 +1033,66 @@
 hCallback);%0A
+                    %7D%0A                %7D);%0A
             
@@ -1194,23 +1194,16 @@
  public 
-static 
 void run
@@ -1242,84 +1242,426 @@
 ed (
-MblSerializer.class) %7B%0A            sTasks.add(task);%0A            runNextTask
+this) %7B%0A            mTasks.add(task);%0A            runNextTask();%0A        %7D%0A    %7D%0A%0A    /**%0A     * %3Cpre%3E%0A     * TBD%0A     * %3C/pre%3E%0A     * @param task%0A     * @return%0A     */%0A    public boolean cancel(Task task) %7B%0A        synchronized (this) %7B%0A            return mTasks.remove(task);%0A        %7D%0A    %7D%0A%0A    /**%0A     * %3Cpre%3ETBD%3C/pre%3E%0A     */%0A    public void cancelAll() %7B%0A        synchronized (this) %7B%0A            mTasks.clear
 ();%0A

Improve Serializer