KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > lib > HsqlArrayList


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.lib;
33
34 import java.lang.reflect.Array JavaDoc;
35
36 // fredt@users - 1.8.0 - enhancements
37

38 /**
39  * Intended as an asynchronous alternative to Vector. Use HsqlLinkedList
40  * instead if its better suited.
41  *
42  * @author dnordahl@users
43  * @version 1.8.0
44  * @since 1.7.0
45  */

46 public class HsqlArrayList extends BaseList implements HsqlList {
47
48 //fredt@users
49
/*
50     private static Reporter reporter = new Reporter();
51
52     private static class Reporter {
53
54         private static int initCounter = 0;
55         private static int updateCounter = 0;
56
57         Reporter() {
58
59             try {
60                 System.runFinalizersOnExit(true);
61             } catch (SecurityException e) {}
62         }
63
64         protected void finalize() {
65
66             System.out.println("HsqlArrayList init count: " + initCounter);
67             System.out.println("HsqlArrayList update count: "
68                                + updateCounter);
69         }
70     }
71 */

72     private static final int DEFAULT_INITIAL_CAPACITY = 10;
73     private static final float DEFAULT_RESIZE_FACTOR = 2.0f;
74     private Object JavaDoc[] elementData;
75     private boolean minimizeOnClear;
76
77     /** Creates a new instance of HsqlArrayList */
78     public HsqlArrayList() {
79
80 // reporter.initCounter++;
81
elementData = new Object JavaDoc[DEFAULT_INITIAL_CAPACITY];
82     }
83
84     /**
85      * Creates a new instance of HsqlArrayList that minimizes the size when
86      * empty
87      */

88     public HsqlArrayList(boolean minimize) {
89
90 // reporter.initCounter++;
91
elementData = new Object JavaDoc[DEFAULT_INITIAL_CAPACITY];
92         minimizeOnClear = minimize;
93     }
94
95     /** Creates a new instance with the given initial capacity */
96     public HsqlArrayList(int initialCapacity) {
97
98 // reporter.initCounter++;
99
if (initialCapacity < 0) {
100             throw new NegativeArraySizeException JavaDoc(
101                 "Invalid initial capacity given");
102         }
103
104         if (initialCapacity == 0) {
105             elementData = new Object JavaDoc[1];
106         } else {
107             elementData = new Object JavaDoc[initialCapacity];
108         }
109     }
110
111     /** Inserts an element at the given index */
112     public void add(int index, Object JavaDoc element) {
113
114 // reporter.updateCounter++;
115
if (index > elementCount) {
116             throw new IndexOutOfBoundsException JavaDoc("Index out of bounds: "
117                                                 + index + ">" + elementCount);
118         }
119
120         if (index < 0) {
121             throw new IndexOutOfBoundsException JavaDoc("Index out of bounds: "
122                                                 + index + " < 0");
123         }
124
125         if (elementCount >= elementData.length) {
126             increaseCapacity();
127         }
128
129         for (int i = elementCount; i > index; i--) {
130             elementData[i] = elementData[i - 1];
131         }
132
133         elementData[index] = element;
134
135         elementCount++;
136     }
137
138     /** Appends an element to the end of the list */
139     public boolean add(Object JavaDoc element) {
140
141 // reporter.updateCounter++;
142
if (elementCount >= elementData.length) {
143             increaseCapacity();
144         }
145
146         elementData[elementCount] = element;
147
148         elementCount++;
149
150         return true;
151     }
152
153     /** Gets the element at given position */
154     public Object JavaDoc get(int index) {
155
156         if (index >= elementCount) {
157             throw new IndexOutOfBoundsException JavaDoc("Index out of bounds: "
158                                                 + index + " >= "
159                                                 + elementCount);
160         }
161
162         if (index < 0) {
163             throw new IndexOutOfBoundsException JavaDoc("Index out of bounds: "
164                                                 + index + " < 0");
165         }
166
167         return elementData[index];
168     }
169
170     /** returns the index of given object or -1 if nt found */
171     public int indexOf(Object JavaDoc o) {
172
173         for (int i = 0; i < elementCount; i++) {
174             if (elementData[i].equals(o)) {
175                 return i;
176             }
177         }
178
179         return -1;
180     }
181
182     /** Removes and returns the element at given position */
183     public Object JavaDoc remove(int index) {
184
185         if (index >= elementCount) {
186             throw new IndexOutOfBoundsException JavaDoc("Index out of bounds: "
187                                                 + index + " >= "
188                                                 + elementCount);
189         }
190
191         if (index < 0) {
192             throw new IndexOutOfBoundsException JavaDoc("Index out of bounds: "
193                                                 + index + " < 0");
194         }
195
196         Object JavaDoc removedObj = elementData[index];
197
198         for (int i = index; i < elementCount - 1; i++) {
199             elementData[i] = elementData[i + 1];
200         }
201
202         elementCount--;
203
204         elementData[elementCount] = null;
205
206         if (minimizeOnClear && elementCount == 0) {
207             elementData = new Object JavaDoc[DEFAULT_INITIAL_CAPACITY];
208         }
209
210         return removedObj;
211     }
212
213     /** Replaces the element at given position */
214     public Object JavaDoc set(int index, Object JavaDoc element) {
215
216         if (index >= elementCount) {
217             throw new IndexOutOfBoundsException JavaDoc("Index out of bounds: "
218                                                 + index + " >= "
219                                                 + elementCount);
220         }
221
222         if (index < 0) {
223             throw new IndexOutOfBoundsException JavaDoc("Index out of bounds: "
224                                                 + index + " < 0");
225         }
226
227         Object JavaDoc replacedObj = elementData[index];
228
229         elementData[index] = element;
230
231         return replacedObj;
232     }
233
234     /** Returns the number of elements in the array list */
235     public final int size() {
236         return elementCount;
237     }
238
239     private void increaseCapacity() {
240
241         int baseSize = elementData.length == 0 ? 1
242                                                : elementData.length;
243         Object JavaDoc[] newArray =
244             new Object JavaDoc[(int) (baseSize * DEFAULT_RESIZE_FACTOR)];
245
246         System.arraycopy(elementData, 0, newArray, 0, elementData.length);
247
248         elementData = newArray;
249         newArray = null;
250     }
251
252     /**
253      * Returns an Enumeration of the elements of the list. The Enumerator will
254      * NOT throw a concurrent modification exception if the list is modified
255      * during the enumeration.
256      */

257 /*
258     public Enumeration elements() {
259
260         Enumeration en = new Enumeration() {
261
262             private int pos = 0;
263
264             public Object nextElement() {
265
266                 if (!hasMoreElements()) {
267                     throw new NoSuchElementException("Enumeration complete");
268                 }
269
270                 pos++;
271
272                 return elementData[pos - 1];
273             }
274
275             public boolean hasMoreElements() {
276                 return (pos < elementCount);
277             }
278         };
279
280         return en;
281     }
282 */

283
284     /** Trims the array to be the same size as the number of elements. */
285     public void trim() {
286
287         // 0 size array is possible
288
Object JavaDoc[] newArray = new Object JavaDoc[elementCount];
289
290         System.arraycopy(elementData, 0, newArray, 0, elementCount);
291
292         elementData = newArray;
293         newArray = null;
294     }
295
296     // fredt@users
297
public void clear() {
298
299         if (minimizeOnClear
300                 && elementData.length > DEFAULT_INITIAL_CAPACITY) {
301             elementData = new Object JavaDoc[DEFAULT_INITIAL_CAPACITY];
302             elementCount = 0;
303
304             return;
305         }
306
307         for (int i = 0; i < elementCount; i++) {
308             elementData[i] = null;
309         }
310
311         elementCount = 0;
312     }
313
314     public void setSize(int newSize) {
315
316         if (newSize < elementCount) {
317             if (minimizeOnClear && newSize == 0
318                     && elementData.length > DEFAULT_INITIAL_CAPACITY) {
319                 elementData = new Object JavaDoc[DEFAULT_INITIAL_CAPACITY];
320                 elementCount = 0;
321
322                 return;
323             }
324
325             for (int i = newSize; i < elementCount; i++) {
326                 elementData[i] = null;
327             }
328         }
329
330         elementCount = newSize;
331
332         for (; elementCount > elementData.length; ) {
333             increaseCapacity();
334         }
335     }
336
337 // fredt@users
338
public Object JavaDoc[] toArray() {
339
340         Object JavaDoc[] a = new Object JavaDoc[elementCount];
341
342         System.arraycopy(elementData, 0, a, 0, elementCount);
343
344         return a;
345     }
346
347     public Object JavaDoc[] toArray(int start, int limit) {
348
349         Object JavaDoc[] a = new Object JavaDoc[elementCount - limit];
350
351         System.arraycopy(elementData, start, a, 0, elementCount - limit);
352
353         return a;
354     }
355
356     /**
357      * Copies all elements of the list to a[]. It is assumed a[] is of the
358      * correct type. If a[] is too small, a new array or the same type is
359      * returned. If a[] is larger, only the list elements are copied and no
360      * other change is made to the array.
361      * Differs from the implementation in java.util.ArrayList in the second
362      * aspect.
363      */

364     public Object JavaDoc toArray(Object JavaDoc a) {
365
366         if (Array.getLength(a) < elementCount) {
367             a = Array.newInstance(a.getClass().getComponentType(),
368                                   elementCount);
369         }
370
371         System.arraycopy(elementData, 0, a, 0, elementCount);
372
373         return a;
374     }
375
376     public void sort(ObjectComparator c) {
377
378         if (elementCount < 2) {
379             return;
380         }
381
382         Sort.sort(elementData, c, 0, elementCount - 1);
383     }
384
385     public Object JavaDoc[] getArray() {
386         return elementData;
387     }
388 }
389
Popular Tags