KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sosnoski > util > queue > IntQueue


1 /*
2  * Copyright (c) 2000-2001 Sosnoski Software Solutions, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */

22
23 package com.sosnoski.util.queue;
24
25 /**
26  * Growable circular queue of <code>int</code>s. This implementation is
27  * unsynchronized in order to provide the best possible performance for typical
28  * usage scenarios, so explicit synchronization must be implemented by a
29  * wrapper class or directly by the application in cases where instances are
30  * modified in a multithreaded environment. See the base classes for other
31  * details of the implementation.
32  *
33  * @author Dennis M. Sosnoski
34  * @version 1.0
35  */

36
37 public class IntQueue extends QueueBase
38 {
39     /** The underlying array used for storing the data. */
40     protected int[] m_baseArray;
41
42     /**
43      * Constructor with full specification.
44      *
45      * @param size number of elements initially allowed in queue
46      * @param growth maximum size increment for growing queue
47      */

48     
49     public IntQueue(int size, int growth) {
50         super(size, growth, int.class);
51     }
52
53     /**
54      * Constructor with partial specification.
55      *
56      * @param size number of elements initially allowed in queue
57      */

58     
59     public IntQueue(int size) {
60         super(size, int.class);
61     }
62
63     /**
64      * Default constructor.
65      */

66     
67     public IntQueue() {
68         this(DEFAULT_SIZE);
69     }
70
71     /**
72      * Copy (clone) constructor.
73      *
74      * @param base instance being copied
75      */

76     
77     public IntQueue(IntQueue base) {
78         super(base);
79     }
80
81     /**
82      * Get the backing array. This method is used by the type-agnostic base
83      * class code to access the array used for type-specific storage.
84      *
85      * @return backing array object
86      */

87
88     protected Object JavaDoc getArray() {
89         return m_baseArray;
90     }
91
92     /**
93      * Set the backing array. This method is used by the type-agnostic base
94      * class code to set the array used for type-specific storage.
95      *
96      * @param backing array object
97      */

98
99     protected void setArray(Object JavaDoc array) {
100         m_baseArray = (int[]) array;
101     }
102
103     /**
104      * Add an item at the end of the queue. If necessary, the array used for
105      * storing the queue data is grown before storing the added item.
106      *
107      * @param item item to be added
108      */

109     
110     public void add(int item) {
111         int index = getAddIndex();
112         m_baseArray[index] = item;
113     }
114
115     /**
116      * Remove an item from the start of the queue.
117      *
118      * @return item removed from queue
119      * @exception IllegalStateException on attempt to remove an item from an
120      * empty queue
121      */

122     
123     public int remove() {
124         return m_baseArray[getRemoveIndex()];
125     }
126
127     /**
128      * Constructs and returns a simple array containing the same data as held
129      * in this growable array.
130      *
131      * @return array containing a copy of the data
132      */

133     
134     public int[] toArray() {
135         return (int[]) buildArray(int.class);
136     }
137
138     /**
139      * Duplicates the object with the generic call.
140      *
141      * @return a copy of the object
142      */

143     
144     public Object JavaDoc clone() {
145         return new IntQueue(this);
146     }
147 }
148
Popular Tags