[wip] initial commit
extracted useful snippets from other projects
This commit is contained in:
commit
b950c9ebdb
26 changed files with 1362 additions and 0 deletions
70
src/de/pzzz/vertx/worker/QueueProcessingStatus.java
Executable file
70
src/de/pzzz/vertx/worker/QueueProcessingStatus.java
Executable file
|
|
@ -0,0 +1,70 @@
|
|||
package de.pzzz.vertx.worker;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
public class QueueProcessingStatus<T> implements Serializable {
|
||||
private static final long serialVersionUID = 7560765077464782742L;
|
||||
|
||||
private transient Queue<T> requestsToProcess = new LinkedList<>();
|
||||
private boolean calculate = false;
|
||||
private int runningCalculations = 0;
|
||||
|
||||
public T startProcessing() {
|
||||
if (requestsToProcess.isEmpty()) {
|
||||
throw new IllegalStateException("No requests to process!");
|
||||
}
|
||||
calculate = true;
|
||||
runningCalculations = 1;
|
||||
return requestsToProcess.poll();
|
||||
}
|
||||
|
||||
public void stopProcessing() {
|
||||
calculate = false;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return !requestsToProcess.isEmpty();
|
||||
}
|
||||
|
||||
public T processNext() {
|
||||
runningCalculations += 1;
|
||||
return requestsToProcess.poll();
|
||||
}
|
||||
|
||||
public void complete() {
|
||||
runningCalculations -= 1;
|
||||
if (runningCalculations == 0 && requestsToProcess.isEmpty()) {
|
||||
calculate = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void enqueue(final T request) {
|
||||
requestsToProcess.add(request);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
requestsToProcess.clear();
|
||||
}
|
||||
|
||||
public boolean isCalculate() {
|
||||
return calculate;
|
||||
}
|
||||
|
||||
public void setCalculate(final boolean calculate) {
|
||||
this.calculate = calculate;
|
||||
}
|
||||
|
||||
public int getRunningCalculations() {
|
||||
return runningCalculations;
|
||||
}
|
||||
|
||||
public void setRunningCalculations(final int running) {
|
||||
this.runningCalculations = running;
|
||||
}
|
||||
|
||||
public int getQueueLength() {
|
||||
return requestsToProcess.size();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue