summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/TodoDeque.java
blob: 4cb1d3820fe30a39119f3ab5ac3e31e15abfb784 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package org.madore.damlengine;

import java.util.Collection;
import java.util.LinkedList;

public final class TodoDeque {

    private LinkedList<TodoItem> deque;

    public TodoDeque() {
	deque = new LinkedList<TodoItem>();
    }

    public void registerAtEnd(TodoItem it) {
	it.ownerDeque = this;
	deque.addLast(it);
    }

    public void registerAtEnd(Collection<? extends TodoItem> them) {
	for ( TodoItem it : them )
	    it.ownerDeque = this;
	deque.addAll(them);
    }

    public void registerAtStart(TodoItem it) {
	it.ownerDeque = this;
	deque.addFirst(it);
    }

    public void registerAtStart(Collection<? extends TodoItem> them) {
	for ( TodoItem it : them )
	    it.ownerDeque = this;
	deque.addAll(0, them);
    }

    public TodoItem removeNext() {
	return deque.poll();
    }

    public boolean dispatchOne() {
	TodoItem it = removeNext();
	if ( it != null ) {
	    assert(it.ownerDeque == this);
	    it.handle();
	    return true;
	} else
	    return false;
    }

    public void dispatchLoop() {
	while ( dispatchOne() )
	    ;
    }

}