source: trunk/LogicMail/src/org/logicprobe/LogicMail/util/Queue.java @ 964

Revision 964, 3.6 KB checked in by octorian, 8 months ago (diff)

Added experimental WiFi connection handover support (refs #79)

Line 
1/*-
2 * Copyright (c) 2008, Derek Konigsberg
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the project nor the names of its
15 *    contributors may be used to endorse or promote products derived
16 *    from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32package org.logicprobe.LogicMail.util;
33
34import java.util.NoSuchElementException;
35
36public class Queue {
37        private static class Node {
38                public Object item;
39                public Node next;
40        }
41       
42        private Node head;
43        private Node tail;
44       
45        public Queue() {
46                head = null;
47                tail = null;
48        }
49       
50        /**
51         * Adds an element to the queue.
52         * @param element The element
53         * @throws NullPointerException if the item is null
54         */
55        public void add(Object element) {
56                if(element == null) {
57                        throw new NullPointerException();
58                }
59                Node node = new Node();
60                node.item = element;
61                node.next = null;
62               
63                if(head == null) {
64                        head = node;
65                        tail = head;
66                }
67                else {
68                        tail.next = node;
69                        tail = tail.next;
70                }
71        }
72
73    /**
74     * Pushes an element onto the head of the queue.
75     * @param element The element
76     * @throws NullPointerException if the item is null
77     */
78        public void push(Object element) {
79        if(element == null) {
80            throw new NullPointerException();
81        }
82        Node node = new Node();
83        node.item = element;
84       
85        if(head == null) {
86            node.next = null;
87            head = node;
88            tail = head;
89        }
90        else {
91            node.next = head;
92            head = node;
93        }
94        }
95       
96        /**
97         * Retrieves the element at the head of the queue.
98         * @return The element, or null if the queue is empty
99         */
100        public Object element() {
101                if(head == null) {
102                        return null;
103                }
104                else {
105                        return head.item;
106                }
107        }
108       
109        /**
110         * Retrieves and removes the element at the head of the queue.
111         * @return The element
112         * @throws NoSuchElementException if the queue is empty
113         */
114        public Object remove() {
115                if(head == null) {
116                        throw new NoSuchElementException();
117                }
118                Object item = head.item;
119                head.item = null;
120                head = head.next;
121                return item;
122        }
123       
124        /**
125         * Removes all elements from the queue.
126         */
127        public void clear() {
128                while(head != null) {
129                        Node temp = head.next;
130                        head.item = null;
131                        head.next = null;
132                        head = temp;
133                }
134                tail = null;
135        }
136}
Note: See TracBrowser for help on using the repository browser.