KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > util > IVVector


1 /*
2  * $Id: IVVector.java,v 1.2 2002/02/15 23:44:28 skavish Exp $
3  *
4  * ===========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash.util;
52
53 import java.util.Enumeration JavaDoc;
54 import java.io.*;
55 import org.openlaszlo.iv.flash.api.FlashItem;
56
57 /**
58  * Simple unsynchronized vector.
59  *
60  * @author Dmitry Skavish
61  */

62 public class IVVector implements Cloneable JavaDoc {
63
64     protected Object JavaDoc[] objects;
65     protected int top;
66     protected static final int INIT_CAPACITY = 20;
67
68     /**
69      * Creates emtpy vector with default capacity.
70      */

71     public IVVector() {
72         this( INIT_CAPACITY );
73     }
74
75     /**
76      * Creates empty vector with specified capacity.
77      *
78      * @param capacity initial capacity
79      */

80     public IVVector( int capacity ) {
81         init( capacity );
82     }
83
84     /**
85      * Creates vector from existing one.
86      * <P>
87      * Creates vector with capacity equal to the size of given vector
88      * and copies all data from given vector to this one
89      *
90      * @param data vector to copy from
91      */

92     public IVVector( IVVector data ) {
93         init( data.size()+1, data.size() );
94         System.arraycopy(data.objects, 0, objects, 0, top);
95     }
96
97     /**
98      * Ensure capacity of this vector.<p>
99      * 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 JavaDoc[] newObjs = new Object JavaDoc[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 JavaDoc 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 JavaDoc o, int index ) {
134         ensureCapacity( index );
135         objects[index] = o;
136         if( index >= top ) top = index+1;
137     }
138
139     /**
140      * Returns the object at the specified index.
141      *
142      * @param index the specified index
143      * @return object at specified index
144      */

145     public final Object JavaDoc elementAt( int index ) {
146         if( index >= top ) {
147             throw new ArrayIndexOutOfBoundsException JavaDoc( index + " >= " + top );
148         }
149         return objects[index];
150     }
151
152     /**
153      * Removes the object at the specified index.
154      *
155      * @param index the specified index
156      * @return removed object at specified index
157      */

158     public final Object JavaDoc removeElementAt( int index ) {
159         if( index >= top ) {
160             throw new ArrayIndexOutOfBoundsException JavaDoc( index + " >= " + top );
161         }
162         Object JavaDoc o = objects[index];
163         --top;
164         if( index < top ) {
165             System.arraycopy( objects, index+1, objects, index, top-index );
166         }
167         objects[top] = null;
168         return o;
169     }
170
171     /**
172      * Removes the object from the vector.
173      * <p>
174      * Finds the specified object in the vector and removes it.
175      *
176      * @param o the specified object
177      */

178     public final void removeElement( Object JavaDoc o ) {
179         removeElementAt( find(o) );
180     }
181
182     /**
183      * Removes a number of objects.
184      *
185      * @param from first object to be removed from the vector
186      * @param to last object to be removed from the vector
187      */

188     public final void removeRange( int from, int to ) {
189         for( int i=from; i<to; i++ ) {
190             removeElementAt( i );
191         }
192     }
193
194     /**
195      * Inserts specified number of null objects beginning from specified index.
196      *
197      * @param from inserts nulls beginning from this index (including)
198      * @param num number of null objects to be inserted
199      */

200     public final void insertObjects( int from, int num ) {
201         if( from > top ) { // has to be '>'
202
throw new ArrayIndexOutOfBoundsException JavaDoc( from + " > " + top );
203         }
204         ensureCapacity( top+num );
205         if( from < top ) {
206             System.arraycopy( objects, from, objects, from+num, top-from );
207         }
208         top += num;
209     }
210
211     /**
212      * Returns size of the vector.
213      *
214      * @return size of the vector (number of objects in it)
215      */

216     public final int size() {
217         return top;
218     }
219
220     /**
221      * Resets vector.
222      * <P>
223      * Removes all objects from the vector, but does not change the capacity
224      */

225     public final void reset() {
226         top = 0;
227     }
228
229     /**
230      * Clears vector.
231      * <P>
232      * Removes all objects from the vector and fills it with nulls,
233      * but does not change the capacity
234      */

235     public final void clear() {
236         for( int i=0; i<objects.length; i++ ) {
237             objects[i] = null;
238         }
239         top = 0;
240     }
241
242     /**
243      * Copies objects of the vector into specified array.
244      *
245      * @param cobjects array of objects to be copied to
246      */

247     public final void copyInto( Object JavaDoc[] cobjects ) {
248         System.arraycopy(objects, 0, cobjects, 0, top);
249     }
250
251     /**
252      * Returns enumeration of all the objects of the vector.
253      *
254      * @return enumeration of all the objects
255      */

256     public final Enumeration JavaDoc elements() {
257         return new Enumeration JavaDoc() {
258             private int cur = 0;
259             public boolean hasMoreElements() {
260                 return cur < top;
261             }
262             public Object JavaDoc nextElement() {
263                 return objects[cur++];
264             }
265         };
266     }
267
268     protected final int find( Object JavaDoc o ) {
269         for( int i=top; --i>=0; ) {
270             if( objects[i] == o ) return i;
271         }
272         return -1;
273     }
274
275     protected final void init( int capacity ) {
276         init( capacity, 0 );
277     }
278
279     protected final void init( int capacity, int top ) {
280         this.top = top;
281         if( capacity <= 0 ) capacity = 1;
282         objects = new Object JavaDoc[capacity];
283     }
284
285     /**
286      * Clones this vector.
287      *
288      * @return new vector - clone of this one
289      */

290     public Object JavaDoc clone() {
291         IVVector v = new IVVector( top );
292         for( int i=0; i<top; i++ ) {
293             v.setElementAt( objects[i], i );
294         }
295         return v;
296     }
297
298     /**
299      * Creates copy of this vector in jgenerator sense.
300      * <P>
301      * Assuming that this vector contains only {@link org.openlaszlo.iv.flash.api.FlashItem}s,
302      * creates copy of this vector which contains copies of all the FlashItems
303      * from this vector.
304      *
305      * @param copier copier to be used when copying FlashItems
306      * @return jgenerator copy of this vector
307      * @see org.openlaszlo.iv.flash.api.FlashItem#getCopy
308      */

309     public IVVector getCopy( ScriptCopier copier ) {
310         IVVector v = new IVVector( top );
311         for( int i=0; i<top; i++ ) {
312             v.setElementAt( ((FlashItem)objects[i]).getCopy(copier), i );
313         }
314         return v;
315     }
316
317     /**
318      * Prints content of this vector.
319      * <P>
320      * For each {@link org.openlaszlo.iv.flash.api.FlashItem} in this vector call it's
321      * {@link org.openlaszlo.iv.flash.api.FlashItem#printContent} method
322      *
323      * @param out printstream to print to
324      * @param indent indentation
325      * @see org.openlaszlo.iv.flash.api.FlashItem#printContent
326      */

327     public void printContent( PrintStream out, String JavaDoc indent ) {
328         for( int i=0; i<top; i++ ) {
329             ((FlashItem)objects[i]).printContent( out, indent );
330         }
331     }
332
333     /**
334      * Writes content of this vector to flash buffer.
335      * <P>
336      * For each {@link org.openlaszlo.iv.flash.api.FlashItem} in this vector call it's
337      * {@link org.openlaszlo.iv.flash.api.FlashItem#write} method
338      *
339      * @param fob flash buffer to write
340      * @see org.openlaszlo.iv.flash.api.FlashItem#write
341      */

342     public void write( FlashOutput fob ) {
343         for( int i=0; i<top; i++ ) {
344             ((FlashItem)objects[i]).write( fob );
345         }
346     }
347 }
348
349
Popular Tags