001/**
002 * Portions Copyright 2004 Sun Microsystems, Inc.
003 * Portions Copyright 1999-2004 Language Technologies Institute, 
004 * Carnegie Mellon University.
005 * All Rights Reserved.  Use is subject to license terms.
006 * 
007 * See the file "license.terms" for information on usage and
008 * redistribution of this file, and for a DISCLAIMER OF ALL 
009 * WARRANTIES.
010 */
011package com.sun.speech.freetts;
012
013import java.util.LinkedList;
014import java.util.List;
015
016/**
017 * Manages a process queue for utterances. Utterances that are
018 * queued to a processor can be written via the post method.
019 * A processing thread can wait for an utterance to arrive via the
020 * pend method.
021 */
022public class OutputQueue {
023    private List<Utterance> list = new LinkedList<Utterance>();
024    private int size;
025    private final static int DEFAULT_SIZE = 5;
026    private volatile boolean closed = false;
027
028    /**
029     * Creates an OutputQueue with the given size.
030     * 
031     * @param size the size of the queue
032     */
033    public OutputQueue(int size) {
034        this.size = size;
035    }
036
037    /**
038     * Creates a queue with the default size.
039     */
040    public OutputQueue() {
041        this(DEFAULT_SIZE);
042    }
043
044    /**
045     * Posts the given utterance to the queue. This call will block if
046     * the queue is full.
047     * 
048     * @param utterance the utterance to post
049     *
050     * @throws IllegalStateException if the queue is closed
051     */
052    public synchronized void post(Utterance utterance) {
053        if (closed) {
054            throw new IllegalStateException("output queue closed");
055        }
056
057        while (list.size() >= size) {
058            try {
059                wait();
060            } catch (InterruptedException ie) {
061            }
062        }
063
064        list.add(utterance);
065        notify();
066    }
067
068
069    /**
070     * Closes the queue.
071     */
072    public synchronized void close() {
073        closed = true;
074        list.add(null);
075        notify();
076    }
077
078
079    /**
080     * Determines if the queue is closed.
081     *
082     * @return  true the queue is closed; otherwise false
083     */
084    public boolean isClosed() {
085        return closed;
086    }
087
088    /**
089     * Blocks until there is an utterance in the queue.
090     *
091     * @return the next utterance. On a close or interrupt, a null is
092     * returned.
093     */
094    public synchronized Utterance pend() {
095        Utterance utterance = null;
096        while (list.size() == 0) {
097            try {
098                wait();
099            } catch (InterruptedException ie) {
100                return null;
101            }
102        }
103        utterance = list.remove(0);
104        notify();
105        return utterance;
106    }
107
108    /**
109     * Removes all items from this OutputQueue.
110     */
111    public synchronized void removeAll() {
112        list.clear();
113    }
114}
115
116