View Javadoc

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