EMMA Coverage Report (generated Tue May 01 18:46:53 CEST 2007)
[all classes][dk.deepthought.sidious.util]

COVERAGE SUMMARY FOR SOURCE FILE [SidiousQueue.java]

nameclass, %method, %block, %line, %
SidiousQueue.java67%  (2/3)100% (11/11)66%  (157/237)75%  (41,9/56)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SidiousQueue$InternalThread100% (1/1)100% (3/3)65%  (68/105)65%  (16,4/25)
run (): void 100% (1/1)61%  (58/95)64%  (15,4/24)
SidiousQueue$InternalThread (SidiousQueue): void 100% (1/1)100% (6/6)100% (1/1)
SidiousQueue$InternalThread (SidiousQueue, SidiousQueue$1): void 100% (1/1)100% (4/4)100% (1/1)
     
class SidiousQueue100% (1/1)100% (8/8)67%  (89/132)82%  (25,5/31)
enqueue (Object): void 100% (1/1)44%  (23/52)75%  (6,8/9)
interrupt (): void 100% (1/1)66%  (21/32)75%  (6,7/9)
startThread (String): void 100% (1/1)86%  (19/22)83%  (5/6)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
SidiousQueue (String): void 100% (1/1)100% (14/14)100% (5/5)
access$000 (): Log 100% (1/1)100% (2/2)100% (1/1)
access$100 (SidiousQueue): List 100% (1/1)100% (3/3)100% (1/1)
access$200 (SidiousQueue): boolean 100% (1/1)100% (3/3)100% (1/1)
     
class SidiousQueue$10%   (0/1)100% (0/0)100% (0/0)100% (0/0)

1package dk.deepthought.sidious.util;
2 
3import java.util.LinkedList;
4import java.util.List;
5 
6import net.jcip.annotations.ThreadSafe;
7 
8import org.apache.commons.logging.Log;
9import org.apache.commons.logging.LogFactory;
10 
11/**
12 * Abstract representation of a queue which uses a thread to dequeue and process
13 * elements.
14 * <p>
15 * Implements a work queue; which allows for asynchronous processing of enqueued
16 * items.
17 * 
18 * @author Deepthought
19 * 
20 */
21@ThreadSafe
22public abstract class SidiousQueue<T> {
23        /**
24         * Logger for this class
25         */
26        private static final Log logger = LogFactory.getLog(SidiousQueue.class);
27 
28        /**
29         * Private thread implementation.
30         */
31        private class InternalThread implements Runnable {
32                public void run() {
33                        if (logger.isDebugEnabled()) {
34                                logger.debug("run() - start");
35                        }
36 
37                        while (true) {
38                                T item = null;
39                                synchronized (queue) {
40                                        while (queue.isEmpty() && !interrupted) {
41                                                try {
42                                                        if (logger.isDebugEnabled()) {
43                                                                logger.debug("run() - waiting - start");
44                                                        }
45                                                        queue.wait();
46                                                        if (logger.isDebugEnabled()) {
47                                                                logger.debug("run() - waiting - end");
48                                                        }
49                                                } catch (InterruptedException e) {
50                                                        logger.error("run()", e);
51                                                        return;
52                                                }
53                                        }
54                                        if (interrupted) {
55                                                if (logger.isDebugEnabled()) {
56                                                        logger.debug("run() - interrupted");
57                                                }
58 
59                                                return;
60                                        }
61                                        item = queue.remove(0);
62                                        if (logger.isDebugEnabled()) {
63                                                logger.debug("run() - " + item + " dequeued");
64                                        }
65                                }
66                                process(item);
67                        }
68                }
69        }
70 
71        /**
72         * Boolean to indicate whether the thread has been interrupted.
73         */
74        private boolean interrupted = false;
75 
76        /**
77         * Internal queue.
78         */
79        private final List<T> queue = new LinkedList<T>();
80 
81        /**
82         * Constructor that starts a new thread.
83         */
84        protected SidiousQueue(String name) {
85                startThread(name);
86        }
87 
88        /**
89         * Enqueues an item.
90         * 
91         * @param item
92         *            the item to be enqueued
93         */
94        public final void enqueue(T item) {
95                if (logger.isDebugEnabled()) {
96                        logger.debug("enqueue(item=" + item + ") - start");
97                }
98                synchronized (queue) {
99                        queue.add(item);
100                        queue.notify();
101                }
102 
103                if (logger.isDebugEnabled()) {
104                        logger.debug("enqueue(item=" + item + ") - end");
105                }
106        }
107 
108        /**
109         * Interrupts the thread.
110         */
111        public final void interrupt() {
112                if (logger.isDebugEnabled()) {
113                        logger.debug("interrupt() - start");
114                }
115                synchronized (queue) {
116                        interrupted = true;
117                        queue.notify();
118                }
119                if (logger.isDebugEnabled()) {
120                        logger.debug("interrupt() - end");
121                }
122        }
123 
124        /**
125         * The processing of an item.
126         * 
127         * @param item
128         *            the item to be processed.
129         */
130        protected abstract void process(T item);
131 
132        /**
133         * Method to start the thread.
134         * 
135         * @param name
136         *            the name of the thread
137         */
138        private void startThread(String name) {
139                Thread thread = new Thread(new InternalThread(), name);
140                thread.setDaemon(false);
141                thread.start();
142                if (logger.isDebugEnabled()) {
143                        logger.debug("startThread() - Thread started");
144                }
145        }
146 
147}

[all classes][dk.deepthought.sidious.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov