01: /*
02: * Copyright 1999,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.jasper.util;
18:
19: import java.util.Vector;
20:
21: /**
22: * A simple FIFO queue class which causes the calling thread to wait
23: * if the queue is empty and notifies threads that are waiting when it
24: * is not empty.
25: *
26: * @author Anil V (akv@eng.sun.com)
27: */
28: public class Queue {
29: private Vector vector = new Vector();
30:
31: /**
32: * Put the object into the queue.
33: *
34: * @param object the object to be appended to the
35: * queue.
36: */
37: public synchronized void put(Object object) {
38: vector.addElement(object);
39: notify();
40: }
41:
42: /**
43: * Pull the first object out of the queue. Wait if the queue is
44: * empty.
45: */
46: public synchronized Object pull() {
47: while (isEmpty())
48: try {
49: wait();
50: } catch (InterruptedException ex) {
51: }
52: return get();
53: }
54:
55: /**
56: * Get the first object out of the queue. Return null if the queue
57: * is empty.
58: */
59: public synchronized Object get() {
60: Object object = peek();
61: if (object != null)
62: vector.removeElementAt(0);
63: return object;
64: }
65:
66: /**
67: * Peek to see if something is available.
68: */
69: public Object peek() {
70: if (isEmpty())
71: return null;
72: return vector.elementAt(0);
73: }
74:
75: /**
76: * Is the queue empty?
77: */
78: public boolean isEmpty() {
79: return vector.isEmpty();
80: }
81:
82: /**
83: * How many elements are there in this queue?
84: */
85: public int size() {
86: return vector.size();
87: }
88: }
|