Created
September 25, 2013 10:48
-
-
Save TalhaAkkas/6697962 to your computer and use it in GitHub Desktop.
Operation Queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ikitap.library.DelayMaker; | |
import android.os.AsyncTask; | |
public class DelayMaker extends AsyncTask<Integer, Void, Void>{ | |
private OnDelayFinishedListener dlgt; | |
public DelayMaker(OnDelayFinishedListener delegate){ | |
dlgt = delegate; | |
} | |
@Override | |
protected Void doInBackground(Integer... params) { | |
try { | |
Thread.sleep(params[0]); | |
} catch (InterruptedException e) { | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(Void x){ | |
dlgt.onDelayFinished(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ikitap.library.DelayMaker; | |
public interface OnDelayFinishedListener { | |
public void onDelayFinished(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ikitap.library.operation; | |
import com.ikitap.library.DelayMaker.DelayMaker; | |
import com.ikitap.library.DelayMaker.OnDelayFinishedListener; | |
import android.os.AsyncTask; | |
import android.util.Log; | |
public class Operation <T1, T2, T3> extends AsyncTask<T1, T2, T3> implements OnDelayFinishedListener{ | |
public final static int TIMEOUT_NON_DETERMINED = -1; | |
public final static int TIMEOUT_NOTIMEOUT = -2; | |
OperationList master = null; | |
private int timeout = -1; | |
private DelayMaker delayMaker; | |
public void setMaster(OperationList master){ | |
this.master = master; | |
} | |
@Override | |
protected void onPreExecute(){ | |
super.onPreExecute(); | |
if(timeout > -1){ | |
delayMaker = new DelayMaker(this); | |
delayMaker.execute(timeout); | |
} | |
} | |
@Override | |
protected T3 doInBackground(T1 ...arg0) { | |
return null; | |
} | |
@Override | |
protected void onPostExecute(T3 arg1){ | |
if(delayMaker != null){ | |
delayMaker.cancel(true); | |
delayMaker = null; | |
} | |
master.update(this); | |
super.onPostExecute(arg1); | |
} | |
@Override | |
protected void onProgressUpdate(T2 ...arg2){ | |
super.onProgressUpdate(arg2); | |
} | |
@Override | |
public void onDelayFinished() { | |
Log.v("operations", "timeout one"); | |
this.cancel(true); | |
onPostExecute(null); | |
} | |
public void setTimeout(int timeout) { | |
this.timeout = timeout; | |
} | |
public int getTimeout() { | |
return timeout; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ikitap.library.operation; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Queue; | |
import com.ikitap.library.DelayMaker.OnDelayFinishedListener; | |
import com.test.TestLog; | |
public interface OperationList { | |
public boolean addOperation(Operation newbie); | |
public Operation[] operations(); | |
public Operation[] waitingOperations(); | |
public Operation[] activeOperations(); | |
public int operationsCount(); | |
public int activeOperationsCount(); | |
public int waitingOperationsCount(); | |
public void cancelAllOperations(); | |
public int maxConcurrentOperationCount(); | |
public void setMaxConcurrentOperationCount(int i); | |
public void setSuspended(boolean suspention); | |
public boolean isSuspended(); | |
public void update(Operation finished); | |
public void setTimeout(int time); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ikitap.library.operation; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Queue; | |
import com.test.TestLog; | |
import android.util.Log; | |
public class OperationQueue implements OperationList { | |
private int max; | |
private int timeout = -1; | |
private Integer waitingMax; | |
private boolean isActive = true; | |
private Queue<Operation> waitingQueue; | |
private List<Operation> activeList; | |
public OperationQueue(int maxConcurrentOperatinoCount){ | |
waitingQueue = new LinkedList<Operation>(); | |
activeList = new LinkedList<Operation>(); | |
max = maxConcurrentOperatinoCount; | |
waitingMax = null; | |
} | |
public OperationQueue(int maxConcurrentOperatinoCount, Integer maxWaitingOperationCount){ | |
waitingQueue = new LinkedList<Operation>(); | |
activeList = new LinkedList<Operation>(); | |
max = maxConcurrentOperatinoCount; | |
waitingMax = maxWaitingOperationCount; | |
} | |
public boolean addOperation(Operation newbie){ | |
if(waitingMax == null || waitingMax.intValue() > waitingQueue.size()){ | |
newbie.setMaster(this); | |
waitingQueue.add(newbie); | |
tryRunSome(); | |
return true; | |
} | |
else | |
return false; | |
} | |
public Operation[] operations(){ | |
Operation[] activeOperations = activeOperations(); | |
Operation[] waitingOperations = waitingOperations(); | |
Operation[] array = new Operation[operationsCount()]; | |
for(int i = 0; i < array.length; i++){ | |
if(i < activeOperations.length){ | |
array[i] = activeOperations[i]; | |
}else{ | |
array[i] = waitingOperations[i - activeOperations.length]; | |
} | |
} | |
return array; | |
} | |
public void update(Operation finished) { | |
Log.v("operations", activeList.size() + "ended one"); | |
activeList.remove(finished); | |
tryRunSome(); | |
} | |
private void tryRunSome(){ | |
while(activeList.size() < max && isActive && waitingQueue.size() > 0 ){ | |
Operation activate = waitingQueue.poll(); | |
if(activate != null){ | |
if(activate.getTimeout() == Operation.TIMEOUT_NON_DETERMINED) | |
activate.setTimeout(timeout); | |
activeList.add(activate); | |
TestLog.getLog().putMessage("operations", activate.toString()); | |
activate.execute(); | |
} | |
} | |
} | |
public Operation[] waitingOperations(){ | |
return (Operation[]) waitingQueue.toArray(); | |
} | |
public Operation[] activeOperations(){ | |
return (Operation[]) activeList.toArray(); | |
} | |
public int operationsCount(){ | |
return activeOperationsCount() + waitingOperationsCount(); | |
} | |
public int activeOperationsCount(){ | |
return activeList.size(); | |
} | |
public int waitingOperationsCount(){ | |
return waitingQueue.size(); | |
} | |
public void cancelAllOperations(){ | |
waitingQueue = new LinkedList<Operation>(); | |
for(Operation o : activeList){ | |
o.cancel(true); | |
} | |
} | |
public void cancelWaitingOperations(){ | |
waitingQueue = new LinkedList<Operation>(); | |
} | |
public int maxConcurrentOperationCount(){ | |
return max; | |
} | |
public void setMaxConcurrentOperationCount(int i){ | |
max = i; | |
} | |
public void setSuspended(boolean suspention){ | |
isActive = !suspention; | |
} | |
public boolean isSuspended(){ | |
return !isActive; | |
} | |
public void setTimeout(int time){ | |
timeout = time; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment