1 50 51 package org.openlaszlo.iv.flash.util; 52 53 import java.util.Enumeration ; 54 import java.io.*; 55 import org.openlaszlo.iv.flash.api.FlashItem; 56 57 62 public class IVVector implements Cloneable { 63 64 protected Object [] objects; 65 protected int top; 66 protected static final int INIT_CAPACITY = 20; 67 68 71 public IVVector() { 72 this( INIT_CAPACITY ); 73 } 74 75 80 public IVVector( int capacity ) { 81 init( capacity ); 82 } 83 84 92 public IVVector( IVVector data ) { 93 init( data.size()+1, data.size() ); 94 System.arraycopy(data.objects, 0, objects, 0, top); 95 } 96 97 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 120 public final void addElement( Object o ) { 121 ensureCapacity( top ); 122 objects[top++] = o; 123 } 124 125 133 public final void setElementAt( Object o, int index ) { 134 ensureCapacity( index ); 135 objects[index] = o; 136 if( index >= top ) top = index+1; 137 } 138 139 145 public final Object elementAt( int index ) { 146 if( index >= top ) { 147 throw new ArrayIndexOutOfBoundsException ( index + " >= " + top ); 148 } 149 return objects[index]; 150 } 151 152 158 public final Object removeElementAt( int index ) { 159 if( index >= top ) { 160 throw new ArrayIndexOutOfBoundsException ( index + " >= " + top ); 161 } 162 Object 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 178 public final void removeElement( Object o ) { 179 removeElementAt( find(o) ); 180 } 181 182 188 public final void removeRange( int from, int to ) { 189 for( int i=from; i<to; i++ ) { 190 removeElementAt( i ); 191 } 192 } 193 194 200 public final void insertObjects( int from, int num ) { 201 if( from > top ) { throw new ArrayIndexOutOfBoundsException ( 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 216 public final int size() { 217 return top; 218 } 219 220 225 public final void reset() { 226 top = 0; 227 } 228 229 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 247 public final void copyInto( Object [] cobjects ) { 248 System.arraycopy(objects, 0, cobjects, 0, top); 249 } 250 251 256 public final Enumeration elements() { 257 return new Enumeration () { 258 private int cur = 0; 259 public boolean hasMoreElements() { 260 return cur < top; 261 } 262 public Object nextElement() { 263 return objects[cur++]; 264 } 265 }; 266 } 267 268 protected final int find( Object 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 [capacity]; 283 } 284 285 290 public Object 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 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 327 public void printContent( PrintStream out, String indent ) { 328 for( int i=0; i<top; i++ ) { 329 ((FlashItem)objects[i]).printContent( out, indent ); 330 } 331 } 332 333 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 |