KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > collection > util > DynArray


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21 package org.jacorb.collection.util;
22
23 import java.util.*;
24
25 public class DynArray
26 {
27     protected Object JavaDoc elementData[];
28     protected int elementCount = 0;
29     public DynArray(int initialCapacity) {
30     this.elementData = ArrayFactory.get_array( initialCapacity );
31     }
32     public DynArray() {
33     this.elementData = new Object JavaDoc[16];
34     }
35     public void copyInto(Object JavaDoc anArray[]) {
36         System.arraycopy( elementData, 0, anArray, 0, elementCount );
37     }
38
39     public void ensureCapacity(int minCapacity) {
40     if (minCapacity > elementData.length) {
41         ensureCapacityHelper(minCapacity);
42     }
43     }
44
45     private void ensureCapacityHelper(int minCapacity) {
46         Object JavaDoc [] oldData = elementData;
47     elementData = ArrayFactory.get_array( minCapacity );
48     System.arraycopy(oldData, 0, elementData, 0, elementCount);
49         ArrayFactory.free_array( oldData );
50     }
51     
52     public void setSize(int newSize) {
53     if (newSize > elementCount) {
54             if (newSize > elementData.length) {
55             ensureCapacityHelper(newSize);
56             }
57         for (int i = newSize ; i >= elementCount ;) {
58         elementData[--i] = null;
59         }
60     } else {
61             for(int i = newSize; i<elementCount; i++ ){
62                 elementData[i] = null;
63             }
64     }
65         elementCount = newSize;
66     }
67
68     public int capacity() {
69     return elementData.length;
70     }
71
72     public int size() {
73     return elementCount;
74     }
75
76     public boolean isEmpty() {
77     return elementCount == 0;
78     }
79
80     public Enumeration elements() {
81     return new DynArrayEnumerator(this);
82     }
83     
84     public boolean contains(Object JavaDoc elem) {
85     return indexOf(elem, 0) >= 0;
86     }
87
88     public int indexOf(Object JavaDoc elem) {
89     return indexOf(elem, 0);
90     }
91
92     public int indexOf(Object JavaDoc elem, int index) {
93     if (index >= elementCount) {
94         throw new ArrayIndexOutOfBoundsException JavaDoc(index);
95     }
96     for (int i = index ; i < elementCount ; i++) {
97         if (elem.equals(elementData[i])) {
98         return i;
99         }
100     }
101     return -1;
102     }
103
104     public Object JavaDoc elementAt(int index) {
105         return elementData[index];
106     }
107
108     public void setElementAt(Object JavaDoc obj, int index) {
109     if (index >= elementCount) {
110         throw new ArrayIndexOutOfBoundsException JavaDoc(index + " >= " +
111                              elementCount);
112     }
113     elementData[index] = obj;
114     }
115
116     public void removeElementAt(int index) {
117     if (index >= elementCount || index < 0) {
118         throw new ArrayIndexOutOfBoundsException JavaDoc(index);
119     }
120     int j = elementCount - index - 1;
121     if (j > 0) {
122         System.arraycopy(elementData, index + 1, elementData, index, j);
123     }
124     elementCount--;
125     elementData[elementCount] = null;
126     }
127
128     public void insertElementAt(Object JavaDoc obj, int index) {
129     int newcount = elementCount + 1;
130     if (index >= newcount) {
131         throw new ArrayIndexOutOfBoundsException JavaDoc(index);
132     }
133     if (newcount > elementData.length) {
134         ensureCapacityHelper(newcount);
135     }
136     System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
137     elementData[index] = obj;
138     elementCount++;
139     }
140
141     public void addElement(Object JavaDoc obj) {
142     int newcount = elementCount + 1;
143     if (newcount > elementData.length) {
144         ensureCapacityHelper(newcount);
145     }
146     elementData[elementCount++] = obj;
147     }
148
149     public boolean removeElement(Object JavaDoc obj) {
150     int i = indexOf(obj);
151     if (i >= 0) {
152         removeElementAt(i);
153         return true;
154     }
155     return false;
156     }
157
158     public void removeAllElements() {
159     for (int i = 0; i < elementCount; i++) {
160         elementData[i] = null;
161     }
162     elementCount = 0;
163     }
164
165     public Object JavaDoc firstElement() {
166     if (elementCount == 0) {
167         throw new NoSuchElementException();
168     }
169     return elementData[0];
170     }
171
172     public Object JavaDoc lastElement() {
173     if (elementCount == 0) {
174         throw new NoSuchElementException();
175     }
176     return elementData[elementCount - 1];
177     }
178
179     protected void finalize(){
180         if( elementData != null ){
181             ArrayFactory.free_array( elementData );
182         }
183     }
184 }
185
186 final
187 class DynArrayEnumerator implements java.util.Enumeration JavaDoc {
188     Object JavaDoc [] data;
189     int count;
190     int elementCount;
191     DynArrayEnumerator(DynArray da) {
192     data = ArrayFactory.get_array( da.elementCount );
193         elementCount = da.elementCount;
194         System.arraycopy(da.elementData, 0, data, 0, da.elementCount );
195     count = 0;
196     }
197
198     public boolean hasMoreElements() {
199     return count < elementCount;
200     }
201
202     public Object JavaDoc nextElement() {
203         if (count < elementCount) {
204             return data[count++];
205     }
206         if( data != null ){
207             ArrayFactory.free_array( data );
208         }
209     throw new NoSuchElementException("DynArrayEnumerator");
210     }
211     protected void finalize(){
212         if( data != null ){
213             ArrayFactory.free_array( data );
214         }
215     }
216 }
217
218
219
220
221
222
223
Popular Tags