summaryrefslogtreecommitdiff
path: root/same/src/main/java/com/orbekk/util/WorkQueue.java
blob: 397c4b8d5bba77058345058a40397f41eeeac945 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.orbekk.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A WorkList is a list for pending units of work. 
 */
abstract public class WorkQueue<E> extends Thread implements List<E> {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private volatile List<E> list = null;
    private volatile boolean done = false;

    public WorkQueue() {
        list = new ArrayList<E>();
    }

    public WorkQueue(Collection<? extends E> collection) {
        list = new ArrayList<E>(collection);
    }

    public synchronized List<E> getAndClear() {
        List<E> copy = new ArrayList<E>(list);
        list.clear();
        return copy;
    }

    /**
     * OnChange event.
     * 
     * May be run even if the WorkQueue has not been changed.
     * Called until the queue is empty.
     */
    protected abstract void onChange();

    /**
     * Perform work until the queue is empty.
     *
     * Can be used for testing or for combining several WorkQueues in
     * a single thread.
     */
    public synchronized void performWork() {
        while (!isEmpty()) {
            onChange();
        }
    }

    @Override
    public void run() {
        while (!done) {
            if (!isEmpty()) {
                onChange();
            }
            synchronized (this) {
                try {
                    if (isEmpty()) {
                        wait();
                    }
                } catch (InterruptedException e) {
                    done = true;
                }
                if (Thread.interrupted()) {
                    done = true;
                }
            }
        }
    }


    @Override
    public synchronized boolean add(E e) {
        notifyAll();
        return list.add(e);
    }

    @Override
    public synchronized void add(int index, E element) {
        notifyAll();
        list.add(index, element);
    }

    @Override
    public synchronized boolean addAll(Collection<? extends E> c) {
        notifyAll();
        return list.addAll(c);
    }

    @Override
    public synchronized boolean addAll(int index, Collection<? extends E> c) {
        notifyAll();
        return list.addAll(index, c);
    }

    @Override
    public synchronized void clear() {
        notifyAll();
        list.clear();
    }

    @Override
    public synchronized boolean contains(Object o) {
        notifyAll();
        return list.contains(o);
    }

    @Override
    public synchronized boolean containsAll(Collection<?> c) {
        notifyAll();
        return containsAll(c);
    }

    @Override
    public synchronized E get(int index) {
        notifyAll();
        return list.get(index);
    }

    @Override
    public synchronized int indexOf(Object o) {
        notifyAll();
        return list.indexOf(o);
    }

    @Override
    public synchronized boolean isEmpty() {
        notifyAll();
        return list.isEmpty();
    }

    @Override
    public synchronized Iterator<E> iterator() {
        notifyAll();
        return list.iterator();
    }

    @Override
    public synchronized int lastIndexOf(Object o) {
        notifyAll();
        return list.lastIndexOf(o);
    }

    @Override
    public synchronized ListIterator<E> listIterator() {
        notifyAll();
        return list.listIterator();
    }

    @Override
    public synchronized ListIterator<E> listIterator(int index) {
        notifyAll();
        return list.listIterator(index);
    }

    @Override
    public synchronized boolean remove(Object o) {
        notifyAll();
        return list.remove(o);
    }

    @Override
    public synchronized E remove(int index) {
        notifyAll();
        return list.remove(index);
    }

    @Override
    public synchronized boolean removeAll(Collection<?> c) {
        notifyAll();
        return list.removeAll(c);
    }

    @Override
    public synchronized boolean retainAll(Collection<?> c) {
        notifyAll();
        return list.retainAll(c);
    }

    @Override
    public synchronized E set(int index, E element) {
        notifyAll();
        return list.set(index, element);
    }

    @Override
    public synchronized int size() {
        notifyAll();
        return list.size();
    }

    @Override
    public synchronized List<E> subList(int fromIndex, int toIndex) {
        notifyAll();
        return list.subList(fromIndex, toIndex);
    }

    @Override
    public synchronized Object[] toArray() {
        notifyAll();
        return list.toArray();
    }

    @Override
    public synchronized <T> T[] toArray(T[] a) {
        notifyAll();
        return list.toArray(a);
    }

}