001: /*
002: * $Id: IVVector.java,v 1.2 2002/02/15 23:44:28 skavish Exp $
003: *
004: * ===========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.util;
052:
053: import java.util.Enumeration;
054: import java.io.*;
055: import org.openlaszlo.iv.flash.api.FlashItem;
056:
057: /**
058: * Simple unsynchronized vector.
059: *
060: * @author Dmitry Skavish
061: */
062: public class IVVector implements Cloneable {
063:
064: protected Object[] objects;
065: protected int top;
066: protected static final int INIT_CAPACITY = 20;
067:
068: /**
069: * Creates emtpy vector with default capacity.
070: */
071: public IVVector() {
072: this (INIT_CAPACITY);
073: }
074:
075: /**
076: * Creates empty vector with specified capacity.
077: *
078: * @param capacity initial capacity
079: */
080: public IVVector(int capacity) {
081: init(capacity);
082: }
083:
084: /**
085: * Creates vector from existing one.
086: * <P>
087: * Creates vector with capacity equal to the size of given vector
088: * and copies all data from given vector to this one
089: *
090: * @param data vector to copy from
091: */
092: public IVVector(IVVector data) {
093: init(data.size() + 1, data.size());
094: System.arraycopy(data.objects, 0, objects, 0, top);
095: }
096:
097: /**
098: * Ensure capacity of this vector.<p>
099: * Increases the capacity of this vector, if necessary, to ensure
100: * that it can hold at least the number of objects specified by
101: * the argument.
102: *
103: * @param cap new capacity
104: */
105: public final void ensureCapacity(int cap) {
106: if (cap >= objects.length) {
107: Object[] newObjs = new Object[cap * 2];
108: System.arraycopy(objects, 0, newObjs, 0, objects.length);
109: objects = newObjs;
110: }
111: }
112:
113: /**
114: * Adds the specified object to the end of this vector,
115: * increasing its size by one. The capacity of this vector is
116: * increased if its size becomes greater than its capacity.
117: *
118: * @param o object to be added
119: */
120: public final void addElement(Object o) {
121: ensureCapacity(top);
122: objects[top++] = o;
123: }
124:
125: /**
126: * Sets the object at the specified <code>index</code> of this
127: * vector to be the specified object. The previous object at that
128: * position is discarded.
129: *
130: * @param o new object to be set at the index
131: * @param index the specified index
132: */
133: public final void setElementAt(Object o, int index) {
134: ensureCapacity(index);
135: objects[index] = o;
136: if (index >= top)
137: top = index + 1;
138: }
139:
140: /**
141: * Returns the object at the specified index.
142: *
143: * @param index the specified index
144: * @return object at specified index
145: */
146: public final Object elementAt(int index) {
147: if (index >= top) {
148: throw new ArrayIndexOutOfBoundsException(index + " >= "
149: + top);
150: }
151: return objects[index];
152: }
153:
154: /**
155: * Removes the object at the specified index.
156: *
157: * @param index the specified index
158: * @return removed object at specified index
159: */
160: public final Object removeElementAt(int index) {
161: if (index >= top) {
162: throw new ArrayIndexOutOfBoundsException(index + " >= "
163: + top);
164: }
165: Object o = objects[index];
166: --top;
167: if (index < top) {
168: System.arraycopy(objects, index + 1, objects, index, top
169: - index);
170: }
171: objects[top] = null;
172: return o;
173: }
174:
175: /**
176: * Removes the object from the vector.
177: * <p>
178: * Finds the specified object in the vector and removes it.
179: *
180: * @param o the specified object
181: */
182: public final void removeElement(Object o) {
183: removeElementAt(find(o));
184: }
185:
186: /**
187: * Removes a number of objects.
188: *
189: * @param from first object to be removed from the vector
190: * @param to last object to be removed from the vector
191: */
192: public final void removeRange(int from, int to) {
193: for (int i = from; i < to; i++) {
194: removeElementAt(i);
195: }
196: }
197:
198: /**
199: * Inserts specified number of null objects beginning from specified index.
200: *
201: * @param from inserts nulls beginning from this index (including)
202: * @param num number of null objects to be inserted
203: */
204: public final void insertObjects(int from, int num) {
205: if (from > top) { // has to be '>'
206: throw new ArrayIndexOutOfBoundsException(from + " > " + top);
207: }
208: ensureCapacity(top + num);
209: if (from < top) {
210: System.arraycopy(objects, from, objects, from + num, top
211: - from);
212: }
213: top += num;
214: }
215:
216: /**
217: * Returns size of the vector.
218: *
219: * @return size of the vector (number of objects in it)
220: */
221: public final int size() {
222: return top;
223: }
224:
225: /**
226: * Resets vector.
227: * <P>
228: * Removes all objects from the vector, but does not change the capacity
229: */
230: public final void reset() {
231: top = 0;
232: }
233:
234: /**
235: * Clears vector.
236: * <P>
237: * Removes all objects from the vector and fills it with nulls,
238: * but does not change the capacity
239: */
240: public final void clear() {
241: for (int i = 0; i < objects.length; i++) {
242: objects[i] = null;
243: }
244: top = 0;
245: }
246:
247: /**
248: * Copies objects of the vector into specified array.
249: *
250: * @param cobjects array of objects to be copied to
251: */
252: public final void copyInto(Object[] cobjects) {
253: System.arraycopy(objects, 0, cobjects, 0, top);
254: }
255:
256: /**
257: * Returns enumeration of all the objects of the vector.
258: *
259: * @return enumeration of all the objects
260: */
261: public final Enumeration elements() {
262: return new Enumeration() {
263: private int cur = 0;
264:
265: public boolean hasMoreElements() {
266: return cur < top;
267: }
268:
269: public Object nextElement() {
270: return objects[cur++];
271: }
272: };
273: }
274:
275: protected final int find(Object o) {
276: for (int i = top; --i >= 0;) {
277: if (objects[i] == o)
278: return i;
279: }
280: return -1;
281: }
282:
283: protected final void init(int capacity) {
284: init(capacity, 0);
285: }
286:
287: protected final void init(int capacity, int top) {
288: this .top = top;
289: if (capacity <= 0)
290: capacity = 1;
291: objects = new Object[capacity];
292: }
293:
294: /**
295: * Clones this vector.
296: *
297: * @return new vector - clone of this one
298: */
299: public Object clone() {
300: IVVector v = new IVVector(top);
301: for (int i = 0; i < top; i++) {
302: v.setElementAt(objects[i], i);
303: }
304: return v;
305: }
306:
307: /**
308: * Creates copy of this vector in jgenerator sense.
309: * <P>
310: * Assuming that this vector contains only {@link org.openlaszlo.iv.flash.api.FlashItem}s,
311: * creates copy of this vector which contains copies of all the FlashItems
312: * from this vector.
313: *
314: * @param copier copier to be used when copying FlashItems
315: * @return jgenerator copy of this vector
316: * @see org.openlaszlo.iv.flash.api.FlashItem#getCopy
317: */
318: public IVVector getCopy(ScriptCopier copier) {
319: IVVector v = new IVVector(top);
320: for (int i = 0; i < top; i++) {
321: v.setElementAt(((FlashItem) objects[i]).getCopy(copier), i);
322: }
323: return v;
324: }
325:
326: /**
327: * Prints content of this vector.
328: * <P>
329: * For each {@link org.openlaszlo.iv.flash.api.FlashItem} in this vector call it's
330: * {@link org.openlaszlo.iv.flash.api.FlashItem#printContent} method
331: *
332: * @param out printstream to print to
333: * @param indent indentation
334: * @see org.openlaszlo.iv.flash.api.FlashItem#printContent
335: */
336: public void printContent(PrintStream out, String indent) {
337: for (int i = 0; i < top; i++) {
338: ((FlashItem) objects[i]).printContent(out, indent);
339: }
340: }
341:
342: /**
343: * Writes content of this vector to flash buffer.
344: * <P>
345: * For each {@link org.openlaszlo.iv.flash.api.FlashItem} in this vector call it's
346: * {@link org.openlaszlo.iv.flash.api.FlashItem#write} method
347: *
348: * @param fob flash buffer to write
349: * @see org.openlaszlo.iv.flash.api.FlashItem#write
350: */
351: public void write(FlashOutput fob) {
352: for (int i = 0; i < top; i++) {
353: ((FlashItem) objects[i]).write(fob);
354: }
355: }
356: }
|