KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > SimpleQueue


1 package jodd.util;
2
3 import java.util.LinkedList;
4 import java.util.NoSuchElementException;
5
6 /**
7  * Simple Queue (FIFO).
8  */

9 public class SimpleQueue {
10
11     private LinkedList list = new LinkedList();
12
13     /**
14      * Puts object in queue.
15      *
16      * @param o object to be stored in queue
17      */

18     public void put(Object o) {
19         list.addLast(o);
20     }
21
22     /**
23      * Returns an element (object) from queue.
24      *
25      * @return element from queue or null if queue is empty
26      */

27     public Object get() {
28         Object result = null;
29         try {
30             result = list.removeFirst();
31         } catch (NoSuchElementException nsee) {
32         }
33         return result;
34     }
35
36     /**
37      * Returns all elements from the queue and clears it.
38      *
39      * @return array of objects from queue
40      */

41     public Object[] getAll() {
42         Object[] res = list.toArray();
43         list.clear();
44         return res;
45     }
46
47
48     /**
49      * Peek element in queue. Returned elements is not removed from the queue.
50      *
51      * @return peeked object
52      */

53     public Object peek() {
54         return list.getFirst();
55     }
56
57     /**
58      * Is queue empty?
59      *
60      * @return true if queue is empty, otherwise false
61      */

62     public boolean isEmpty() {
63         return list.isEmpty();
64     }
65 }
66
Popular Tags