KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > collection > ListQueue


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.collection;
23
24 import java.util.List JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import org.jboss.util.NullArgumentException;
29
30 /**
31  * A ListQueue implements a first-in, first-out container using a List as
32  * a data structure.
33  *
34  * @version <tt>$Revision: 1958 $</tt>
35  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
36  */

37 public class ListQueue
38    extends AbstractQueue
39 {
40    /** List container */
41    protected final List JavaDoc list;
42
43    /**
44     * Construct a new <i>constrained</i> ListQueue.
45     *
46     * @param list The list which will be used to store queued objects.
47     * @param maxSize The maximum size of the queue.
48     *
49     * @exception IllegalArgumentException List is <i>null</i>.
50     */

51    public ListQueue(final List JavaDoc list, final int maxSize) {
52       super(maxSize);
53
54       if (list == null)
55          throw new NullArgumentException("list");
56
57       this.list = list;
58    }
59
60    /**
61     * Construct a new <i>constrained</i> ListQueue using a
62     * <code>LinkedList</code> for a data-structure.
63     *
64     * @param maxSize The maximum size of the queue.
65     */

66    public ListQueue(final int maxSize) {
67       super(maxSize);
68       this.list = new LinkedList JavaDoc();
69    }
70
71    /**
72     * Construct a new <i>unconstrained</i> ListQueue.
73     *
74     * @param list The list which will be used to store queued objects.
75     *
76     * @exception IllegalArgumentException List is <i>null</i>
77     */

78    public ListQueue(final List JavaDoc list) {
79       this(list, UNLIMITED_MAXIMUM_SIZE);
80    }
81
82    /**
83     * Construct a new <i>unconstrained</i> ListQueue using a
84     * <code>LinkedList</code> for a data-structure.
85     */

86    public ListQueue() {
87       this(new LinkedList JavaDoc(), UNLIMITED_MAXIMUM_SIZE);
88    }
89
90    /**
91     * Appends the given element to the end of this list.
92     *
93     * @param obj Object to append.
94     */

95    protected boolean addLast(final Object JavaDoc obj) {
96       return list.add(obj);
97    }
98
99    /**
100     * Remove the first object in the queue.
101     *
102     * @return First object in the queue.
103     */

104    protected Object JavaDoc removeFirst() {
105       return list.remove(0);
106    }
107
108    /**
109     * Get the size of the queue.
110     *
111     * @return The number of elements in the queue.
112     */

113    public int size() {
114       return list.size();
115    }
116
117    /**
118     * Returns an iterator over the elements in this list in proper sequence.
119     *
120     * @return An iterator over the elements in this list in proper sequence.
121     */

122    public Iterator JavaDoc iterator() {
123       return list.iterator();
124    }
125
126    /**
127     * Get the object at the front of the queue.
128     *
129     * @return Object at the front of the queue.
130     *
131     * @exception EmptyCollectionException The queue is empty.
132     */

133    public Object JavaDoc getFront() throws EmptyCollectionException {
134       if (isEmpty())
135          throw new EmptyCollectionException();
136
137       return list.get(0);
138    }
139
140    /**
141     * Get the object at the back of the queue.
142     *
143     * @return Object at the back of the queue.
144     *
145     * @exception EmptyCollectionException The queue is empty.
146     */

147    public Object JavaDoc getBack() throws EmptyCollectionException {
148       if (isEmpty())
149          throw new EmptyCollectionException();
150
151       return list.get(list.size() - 1);
152    }
153
154    /**
155     * Returns an iterator over the elements in this list in reverse sequence.
156     *
157     * @return An iterator over the elements in this list in reverse sequence.
158     */

159    public Iterator JavaDoc reverseIterator() {
160       return new ReverseListIterator(list);
161    }
162
163    /**
164     * Return a String representation of this queue.
165     *
166     * @return String
167     */

168    public String JavaDoc toString() {
169       return list.toString();
170    }
171 }
172
Popular Tags