Browse files

first version of OrderedRunnableQueue

  • Loading branch information...
1 parent 075fd6d commit 25e8be184b69fbbb410bf51040a09ee464c79c91 @alexvetter committed Aug 13, 2012
View
91 src/org/kaffeezusatz/commodity/collections/OrderedRunnableQueue.java
@@ -0,0 +1,91 @@
+package org.kaffeezusatz.commodity.collections;
+
+import java.util.Map.Entry;
+import java.util.TreeMap;
+
+public class OrderedRunnableQueue {
+
+ private final int runEveryEntries;
+
+ private final int runEveryMillis;
+
+ private Integer maxKey;
+
+ private TreeMap<Integer, Runnable> map;
+
+ /**
+ * Ausführen wenn kein Leak oder Ausführen nach X neuen Elementen.
+ */
+ public OrderedRunnableQueue(final int runEveryEntries, final int runEveryMillis) {
+ this.runEveryEntries = runEveryEntries;
+ this.runEveryMillis = runEveryMillis;
+
+ this.maxKey = -1;
+
+ this.map = new TreeMap<Integer, Runnable>();
+ }
+
+ public synchronized void addRunnable(final Integer num, final Runnable r) {
+ if (num == null) {
+ throw new IllegalArgumentException("Integer num can't be null!");
+ }
+ if (r == null) {
+ throw new IllegalArgumentException("Runnable r can't be null!");
+ }
+
+ this.map.put(num, r);
+
+ System.out.print(num + "... ");
+
+ runQueue();
+
+ System.out.println("");
+ }
+
+ public synchronized void addRunnable(final OrderedRunnable or) {
+ if (or == null) {
+ throw new IllegalArgumentException("OrderedRunnable or can't be null!");
+ }
+
+ addRunnable(or.getNum(), or);
+ }
+
+ protected void runQueue() {
+ int last = maxKey.intValue() + 1;
+
+ TreeMap<Integer, Runnable> mapCopy = new TreeMap<Integer, Runnable>(map);
+
+ for (Entry<Integer, Runnable> runnable : mapCopy.entrySet()) {
+ if (last == runnable.getKey().intValue()) {
+ last += 1;
+ maxKey = runnable.getKey();
+
+ System.out.print(" " + runnable.getKey());
+
+ map.remove(runnable.getKey()).run();
+ } else if (runEveryEntries == map.size()) {
+ System.out.print(" Run every " + runEveryEntries);
+ runCurrentQueue();
+ last = maxKey.intValue() + 1;
+ }
+ }
+ }
+
+ protected void runCurrentQueue() {
+ synchronized (map) {
+ for (Entry<Integer, Runnable> entry : map.entrySet()) {
+ if (entry.getValue() != null) {
+ entry.getValue().run();
+ System.out.print(" " + entry.getKey());
+ }
+ maxKey = entry.getKey();
+ }
+ }
+
+ map.clear();
+ }
+
+ public static interface OrderedRunnable extends Runnable {
+ public int getNum();
+ }
+}
View
62 src/org/kaffeezusatz/commodity/collections/OrderedRunnableQueueTest.java
@@ -0,0 +1,62 @@
+package org.kaffeezusatz.commodity.collections;
+
+import org.junit.Test;
+
+public class OrderedRunnableQueueTest {
+ @Test
+ public final void test() {
+ final Runnable r = new Runnable() {
+ @Override
+ public void run() {
+ //System.out.println("Peter");
+ }
+ };
+
+ final OrderedRunnableQueue orq = new OrderedRunnableQueue(5, 4000);
+
+ orq.addRunnable(0, r); // 0
+ orq.addRunnable(3, r); // 1
+ orq.addRunnable(2, r); // 2
+ orq.addRunnable(1, r); // 3
+ orq.addRunnable(4, r); // 4
+ orq.addRunnable(6, r); // 5
+ orq.addRunnable(5, r); // 6
+ orq.addRunnable(7, r); // 7
+ orq.addRunnable(9, r); // 8
+ orq.addRunnable(8, r); // 9
+ orq.addRunnable(10, r); // 10
+ orq.addRunnable(11, r); // 11
+ orq.addRunnable(16, r); // 12
+ orq.addRunnable(13, r); // 13
+ orq.addRunnable(12, r); // 14
+ orq.addRunnable(15, r); // 15
+ orq.addRunnable(18, r); // 16
+ orq.addRunnable(14, r); // 17
+ orq.addRunnable(19, r); // 18
+ orq.addRunnable(17, r); // 19
+ orq.addRunnable(20, r); // 20
+ orq.addRunnable(26, r); // 21
+ orq.addRunnable(24, r); // 22
+ orq.addRunnable(23, r); // 23
+ orq.addRunnable(22, r); // 24
+ orq.addRunnable(25, r); // 25
+ orq.addRunnable(21, r); // 26
+ }
+
+ @Test
+ public final void addNull() {
+ final OrderedRunnableQueue orq = new OrderedRunnableQueue(5, 4000);
+ try {
+ orq.addRunnable(null);
+ } catch (Exception e) {}
+
+ try {
+ orq.addRunnable(1, null);
+ } catch (Exception e) {}
+
+ try {
+ orq.addRunnable(null, null);
+ } catch (Exception e) {
+ }
+ }
+}

0 comments on commit 25e8be1

Please sign in to comment.