upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / OutputQueue.java
1 /**
2  * Portions Copyright 2004 Sun Microsystems, Inc.
3  * Portions Copyright 1999-2004 Language Technologies Institute, 
4  * Carnegie Mellon University.
5  * All Rights Reserved.  Use is subject to license terms.
6  * 
7  * See the file "license.terms" for information on usage and
8  * redistribution of this file, and for a DISCLAIMER OF ALL 
9  * WARRANTIES.
10  */
11 package com.sun.speech.freetts;
12
13 import java.util.LinkedList;
14
15 /**
16  * Manages a process queue for utterances. Utterances that are
17  * queued to a processor can be written via the post method.
18  * A processing thread can wait for an utterance to arrive via the
19  * pend method.
20  */
21 public class OutputQueue {
22     private LinkedList list = new LinkedList();
23     private int size;
24     private final static int DEFAULT_SIZE = 5;
25     private volatile boolean closed = false;
26
27     /**
28      * Creates an OutputQueue with the given size.
29      * 
30      * @param size the size of the queue
31      */
32     public OutputQueue(int size) {
33         this.size = size;
34     }
35
36     /**
37      * Creates a queue with the default size.
38      */
39     public OutputQueue() {
40         this(DEFAULT_SIZE);
41     }
42
43     /**
44      * Posts the given utterance to the queue. This call will block if
45      * the queue is full.
46      * 
47      * @param utterance the utterance to post
48      *
49      * @throws IllegalStateException if the queue is closed
50      */
51     public synchronized void post(Utterance utterance) {
52         if (closed) {
53             throw new IllegalStateException("output queue closed");
54         }
55
56         while (list.size() >= size) {
57             try {
58                 wait();
59             } catch (InterruptedException ie) {
60             }
61         }
62
63         list.add(utterance);
64         notify();
65     }
66
67
68     /**
69      * Closes the queue.
70      */
71     public synchronized void close() {
72         closed = true;
73         list.add(null);
74         notify();
75     }
76
77
78     /**
79      * Determines if the queue is closed.
80      *
81      * @return  true the queue is closed; otherwise false
82      */
83     public boolean isClosed() {
84         return closed;
85     }
86
87     /**
88      * Blocks until there is an utterance in the queue.
89      *
90      * @return the next utterance. On a close or interrupt, a null is
91      * returned.
92      */
93     public synchronized Utterance pend() {
94         Utterance utterance = null;
95         while (list.size() == 0) {
96             try {
97                 wait();
98             } catch (InterruptedException ie) {
99                 return null;
100             }
101         }
102         utterance = (Utterance) list.removeFirst();
103         notify();
104         return utterance;
105     }
106
107     /**
108      * Removes all items from this OutputQueue.
109      */
110     public synchronized void removeAll() {
111         list.clear();
112     }
113 }
114
115