KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > collection > SimpleQueue


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.util.collection;
4
5 import java.util.LinkedList JavaDoc;
6
7 /**
8  * Simple Queue (FIFO) based on LinkedList.
9  */

10 public class SimpleQueue {
11
12     private LinkedList JavaDoc list = new LinkedList JavaDoc();
13
14     /**
15      * Puts object in queue.
16      */

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

26     public Object JavaDoc get() {
27         if (list.isEmpty()) {
28             return null;
29         }
30         return list.removeFirst();
31     }
32
33     /**
34      * Returns all elements from the queue and clears it.
35      */

36     public Object JavaDoc[] getAll() {
37         Object JavaDoc[] res = new Object JavaDoc[list.size()];
38         for (int i = 0; i < res.length; i++) {
39             res[i] = list.get(i);
40         }
41         list.clear();
42         return res;
43     }
44
45
46     /**
47      * Peeks an element in the queue. Returned elements is not removed from the queue.
48      */

49     public Object JavaDoc peek() {
50         return list.getFirst();
51     }
52
53     /**
54      * Returns <code>true</code> if queue is empty, otherwise <code>false</code>
55      */

56     public boolean isEmpty() {
57         return list.isEmpty();
58     }
59
60     /**
61      * Returns queue size.
62      */

63     public int size() {
64         return list.size();
65     }
66 }
67
Popular Tags