Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jgift/command/CommandQueue.java


1   /*
2    * This file is part of jgiFT.
3    * Copyright (c) 2003, Jason Shobe
4    *
5    * jgiFT is free software; you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation; either version 2 of the License, or
8    * (at your option) any later version.
9    *
10   * jgiFT is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with jgiFT; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package jgift.command;
20  
21  import java.util.ArrayList;
22  
23  /**
24   * Data structure for holding Command objects in a FIFO fashion.
25   *
26   * @author  Jason Shobe
27   * @version $Revision: 1.1 $
28   */
29  public class CommandQueue extends ArrayList {
30     /**
31      * Adds a command to the end of this queue.
32      *
33      * @param command the command to add.
34      */
35     public void enqueue(Command command) {
36        synchronized(this) {
37           add(command);
38           notify();
39        }
40     }
41     
42     /**
43      * Gets the command at the head of this queue. This method will block until
44      * there is a command available in the queue.
45      *
46      * @return the first command in the queue.
47      */
48     public Command dequeue() {
49        // block until a command is enqueued
50        synchronized(this) {
51           while(!isCommandAvailable()) {
52              try {
53                 wait();
54              }
55              catch(InterruptedException ie) {
56                 // ignore this
57              }
58           }
59        }
60        
61        return (Command) remove(0);
62     }
63     
64     /**
65      * Determines if there are any commands in this queue.
66      *
67      * @return <code>true</code> if a command is available.
68      */
69     public final boolean isCommandAvailable() {
70        return size() > 0;
71     }
72  }
73