KickJava   Java API By Example, From Geeks To Geeks.

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


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.AbstractCollection JavaDoc;
25
26 /**
27  * An abstract implementation of a Queue. Sub-classes must provide methods
28  * for <code>addLast(Object)</code> and <code>removeFirst()</code>.
29  *
30  * @version <tt>$Revision: 1958 $</tt>
31  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
32  */

33 public abstract class AbstractQueue
34    extends AbstractCollection JavaDoc
35    implements Queue
36 {
37    /** Default maximum queue size */
38    public static int DEFAULT_MAXIMUM_SIZE = UNLIMITED_MAXIMUM_SIZE;
39
40    /** Maximum queue size */
41    protected int maximumSize = DEFAULT_MAXIMUM_SIZE;
42
43    /**
44     * Initializes the AbstractQueue.
45     */

46    protected AbstractQueue() {}
47
48    /**
49     * Initializes the AbstractQueue.
50     *
51     * @param maxSize Maximum queue size.
52     *
53     * @exception IllegalArgumentException Illegal size.
54     */

55    protected AbstractQueue(final int maxSize) {
56       setMaximumSize(maxSize);
57    }
58
59    /**
60     * Get the maximum size of the queue.
61     *
62     * @return Maximum queue size or {@link #UNLIMITED_MAXIMUM_SIZE}.
63     */

64    public int getMaximumSize() {
65       return maximumSize;
66    }
67
68    /**
69     * Set the maximum size of the queue
70     *
71     * @param size New maximim queue size or {@link #UNLIMITED_MAXIMUM_SIZE}.
72     *
73     * @exception IllegalArgumentException Illegal size.
74     */

75    public void setMaximumSize(final int size) {
76       if (size < 0 && size != UNLIMITED_MAXIMUM_SIZE)
77          throw new IllegalArgumentException JavaDoc("illegal size: " + size);
78
79       maximumSize = size;
80    }
81
82    /**
83     * Check if the queue is full.
84     *
85     * @return True if the queue is full.
86     */

87    public boolean isFull() {
88       if (maximumSize != UNLIMITED_MAXIMUM_SIZE && size() >= maximumSize)
89          return true;
90
91       return false;
92    }
93
94    /**
95     * Check if the queue is empty.
96     *
97     * @return True if the queue is empty.
98     */

99    public boolean isEmpty() {
100       if (size() <= 0)
101          return true;
102
103       return false;
104    }
105
106    /**
107     * Append and object to the underling list.
108     *
109     * @param obj Object to enqueue.
110     * @return True if collection was modified.
111     *
112     * @exception FullCollectionException The queue is full.
113     */

114    public boolean add(Object JavaDoc obj) throws FullCollectionException {
115       if (isFull())
116          throw new FullCollectionException();
117
118       return addLast(obj);
119    }
120
121    /**
122     * Remove and return the first object in the queue.
123     *
124     * @return Dequeued object.
125     *
126     * @exception EmptyCollectionException The queue is empty.
127     */

128    public Object JavaDoc remove() throws EmptyCollectionException {
129       if (isEmpty())
130          throw new EmptyCollectionException();
131
132       return removeFirst();
133    }
134
135    /**
136     * Removes all of the elements from this queue
137     */

138    public void clear() {
139       while (!isEmpty()) {
140          remove();
141       }
142    }
143
144    /**
145     * Appends the given element to the end of the queue
146     *
147     * @param obj Object to append
148     * @return Per Collection.add(), we return a boolean to indicate if
149     * the object modified the collection.
150     */

151    protected abstract boolean addLast(Object JavaDoc obj);
152
153    /**
154     * Remove the first object in the queue
155     *
156     * @return First object in the queue
157     */

158    protected abstract Object JavaDoc removeFirst();
159 }
160
Popular Tags