001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1999-2004 Gerald Brose
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: *
020: */
021: package org.jacorb.collection.util;
022:
023: import java.util.*;
024:
025: public class DynArray {
026: protected Object elementData[];
027: protected int elementCount = 0;
028:
029: public DynArray(int initialCapacity) {
030: this .elementData = ArrayFactory.get_array(initialCapacity);
031: }
032:
033: public DynArray() {
034: this .elementData = new Object[16];
035: }
036:
037: public void copyInto(Object anArray[]) {
038: System.arraycopy(elementData, 0, anArray, 0, elementCount);
039: }
040:
041: public void ensureCapacity(int minCapacity) {
042: if (minCapacity > elementData.length) {
043: ensureCapacityHelper(minCapacity);
044: }
045: }
046:
047: private void ensureCapacityHelper(int minCapacity) {
048: Object[] oldData = elementData;
049: elementData = ArrayFactory.get_array(minCapacity);
050: System.arraycopy(oldData, 0, elementData, 0, elementCount);
051: ArrayFactory.free_array(oldData);
052: }
053:
054: public void setSize(int newSize) {
055: if (newSize > elementCount) {
056: if (newSize > elementData.length) {
057: ensureCapacityHelper(newSize);
058: }
059: for (int i = newSize; i >= elementCount;) {
060: elementData[--i] = null;
061: }
062: } else {
063: for (int i = newSize; i < elementCount; i++) {
064: elementData[i] = null;
065: }
066: }
067: elementCount = newSize;
068: }
069:
070: public int capacity() {
071: return elementData.length;
072: }
073:
074: public int size() {
075: return elementCount;
076: }
077:
078: public boolean isEmpty() {
079: return elementCount == 0;
080: }
081:
082: public Enumeration elements() {
083: return new DynArrayEnumerator(this );
084: }
085:
086: public boolean contains(Object elem) {
087: return indexOf(elem, 0) >= 0;
088: }
089:
090: public int indexOf(Object elem) {
091: return indexOf(elem, 0);
092: }
093:
094: public int indexOf(Object elem, int index) {
095: if (index >= elementCount) {
096: throw new ArrayIndexOutOfBoundsException(index);
097: }
098: for (int i = index; i < elementCount; i++) {
099: if (elem.equals(elementData[i])) {
100: return i;
101: }
102: }
103: return -1;
104: }
105:
106: public Object elementAt(int index) {
107: return elementData[index];
108: }
109:
110: public void setElementAt(Object obj, int index) {
111: if (index >= elementCount) {
112: throw new ArrayIndexOutOfBoundsException(index + " >= "
113: + elementCount);
114: }
115: elementData[index] = obj;
116: }
117:
118: public void removeElementAt(int index) {
119: if (index >= elementCount || index < 0) {
120: throw new ArrayIndexOutOfBoundsException(index);
121: }
122: int j = elementCount - index - 1;
123: if (j > 0) {
124: System.arraycopy(elementData, index + 1, elementData,
125: index, j);
126: }
127: elementCount--;
128: elementData[elementCount] = null;
129: }
130:
131: public void insertElementAt(Object obj, int index) {
132: int newcount = elementCount + 1;
133: if (index >= newcount) {
134: throw new ArrayIndexOutOfBoundsException(index);
135: }
136: if (newcount > elementData.length) {
137: ensureCapacityHelper(newcount);
138: }
139: System.arraycopy(elementData, index, elementData, index + 1,
140: elementCount - index);
141: elementData[index] = obj;
142: elementCount++;
143: }
144:
145: public void addElement(Object obj) {
146: int newcount = elementCount + 1;
147: if (newcount > elementData.length) {
148: ensureCapacityHelper(newcount);
149: }
150: elementData[elementCount++] = obj;
151: }
152:
153: public boolean removeElement(Object obj) {
154: int i = indexOf(obj);
155: if (i >= 0) {
156: removeElementAt(i);
157: return true;
158: }
159: return false;
160: }
161:
162: public void removeAllElements() {
163: for (int i = 0; i < elementCount; i++) {
164: elementData[i] = null;
165: }
166: elementCount = 0;
167: }
168:
169: public Object firstElement() {
170: if (elementCount == 0) {
171: throw new NoSuchElementException();
172: }
173: return elementData[0];
174: }
175:
176: public Object lastElement() {
177: if (elementCount == 0) {
178: throw new NoSuchElementException();
179: }
180: return elementData[elementCount - 1];
181: }
182:
183: protected void finalize() {
184: if (elementData != null) {
185: ArrayFactory.free_array(elementData);
186: }
187: }
188: }
189:
190: final class DynArrayEnumerator implements java.util.Enumeration {
191: Object[] data;
192: int count;
193: int elementCount;
194:
195: DynArrayEnumerator(DynArray da) {
196: data = ArrayFactory.get_array(da.elementCount);
197: elementCount = da.elementCount;
198: System.arraycopy(da.elementData, 0, data, 0, da.elementCount);
199: count = 0;
200: }
201:
202: public boolean hasMoreElements() {
203: return count < elementCount;
204: }
205:
206: public Object nextElement() {
207: if (count < elementCount) {
208: return data[count++];
209: }
210: if (data != null) {
211: ArrayFactory.free_array(data);
212: }
213: throw new NoSuchElementException("DynArrayEnumerator");
214: }
215:
216: protected void finalize() {
217: if (data != null) {
218: ArrayFactory.free_array(data);
219: }
220: }
221: }
|