00001 /* 00002 * Copyright (c) 2003, KNOPFLERFISH project 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following 00007 * conditions are met: 00008 * 00009 * - Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 00012 * - Redistributions in binary form must reproduce the above 00013 * copyright notice, this list of conditions and the following 00014 * disclaimer in the documentation and/or other materials 00015 * provided with the distribution. 00016 * 00017 * - Neither the name of the KNOPFLERFISH project nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00027 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00028 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00029 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00030 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00031 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 00032 * OF THE POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 00035 package org.knopflerfish.framework; 00036 00037 import java.util.Vector; 00038 00044 public class Queue extends Vector { 00045 private int m_nMaxSize = -1; 00046 private boolean queueClosed = false; 00047 00048 // ==================== Queue ==================== 00054 public Queue(int size) 00055 { 00056 m_nMaxSize = size; 00057 } 00058 00059 // ==================== insert ==================== 00067 public synchronized void insert(Object item) throws IndexOutOfBoundsException 00068 { 00069 // Check if queue is full 00070 if (m_nMaxSize > 0 && size() >= m_nMaxSize) 00071 throw new IndexOutOfBoundsException("Queue full"); 00072 00073 addElement(item); 00074 notify(); 00075 } 00076 00077 // ==================== insertFirst ==================== 00084 public synchronized void insertFirst(Object item) 00085 { 00086 insertElementAt(item, 0); 00087 notify(); 00088 } 00089 00090 // ==================== remove ==================== 00100 public synchronized Object removeWait(float timeout) 00101 { 00102 Object obj = null; 00103 00104 // If queue is empty wait for object to be inserted 00105 if (isEmpty() && !queueClosed) { 00106 try { 00107 if (timeout > 0) { 00108 wait(Math.round(timeout * 1000.0f)); 00109 } else 00110 wait(); 00111 } catch (InterruptedException e) {} 00112 } 00113 00114 if (queueClosed) { 00115 return null; 00116 } 00117 00118 try { 00119 obj = firstElement(); 00120 removeElementAt(0); 00121 } catch (Exception e) {} 00122 00123 return obj; 00124 } 00125 00126 // ==================== remove ==================== 00134 public Object remove() 00135 { 00136 return removeWait(0); 00137 } 00138 00139 00140 // ==================== close ==================== 00146 public synchronized void close() { 00147 queueClosed = true; 00148 notifyAll(); 00149 } 00150 00151 } 00152