| 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 | |
|---|
| 32 | package org.logicprobe.LogicMail.util; |
|---|
| 33 | |
|---|
| 34 | import java.util.NoSuchElementException; |
|---|
| 35 | |
|---|
| 36 | public 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 | * Retrieves the element at the head of the queue. |
|---|
| 75 | * @return The element, or null if the queue is empty |
|---|
| 76 | */ |
|---|
| 77 | public Object element() { |
|---|
| 78 | if(head == null) { |
|---|
| 79 | return null; |
|---|
| 80 | } |
|---|
| 81 | else { |
|---|
| 82 | return head.item; |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | /** |
|---|
| 87 | * Retrieves and removes the element at the head of the queue. |
|---|
| 88 | * @return The element |
|---|
| 89 | * @throws NoSuchElementException if the queue is empty |
|---|
| 90 | */ |
|---|
| 91 | public Object remove() { |
|---|
| 92 | if(head == null) { |
|---|
| 93 | throw new NoSuchElementException(); |
|---|
| 94 | } |
|---|
| 95 | Object item = head.item; |
|---|
| 96 | head.item = null; |
|---|
| 97 | head = head.next; |
|---|
| 98 | return item; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | /** |
|---|
| 102 | * Removes all elements from the queue. |
|---|
| 103 | */ |
|---|
| 104 | public void clear() { |
|---|
| 105 | while(head != null) { |
|---|
| 106 | Node temp = head.next; |
|---|
| 107 | head.item = null; |
|---|
| 108 | head.next = null; |
|---|
| 109 | head = temp; |
|---|
| 110 | } |
|---|
| 111 | tail = null; |
|---|
| 112 | } |
|---|
| 113 | } |
|---|