1 9 package org.ozoneDB.DxLib; 10 11 import java.util.*; 12 import java.io.*; 13 14 20 public class DxArrayBag extends DxAbstractBag implements DxVector, DxVectorCollection { 21 22 final static long serialVersionUID = 1L; 23 24 private transient Vector vector; 25 26 private transient int itemCount = 0; 27 28 29 public DxArrayBag() { 30 this( 32 ); 31 } 32 33 34 public DxArrayBag( int initSpace ) { 35 vector = new Vector( initSpace ); 36 itemCount = 0; 37 } 38 39 40 public synchronized boolean addFront( Object obj ) { 41 insertAtIndex( obj, 0 ); 42 return true; 43 } 44 45 46 public synchronized boolean addBack( Object obj ) { 47 insertAtIndex( obj, size() ); 48 return true; 49 } 50 51 52 58 public synchronized Object addAtIndex( Object obj, int index ) { 59 if (index >= vector.size()) { 60 vector.setSize( index + 1 ); 61 } 62 Object old = vector.elementAt( index ); 63 vector.setElementAt( obj, index ); 64 if (old == null) { 65 itemCount++; 66 } 67 return old; 68 } 69 70 71 public Object elementAtIndex( int index ) { 72 return vector.elementAt( index ); 73 } 74 75 76 81 public synchronized Object removeAtIndex( int index ) { 82 Object old = vector.elementAt( index ); 83 vector.setElementAt( null, index ); 84 if (old != null) { 85 itemCount--; 86 } 87 return old; 88 } 89 90 91 98 public synchronized void insertAtIndex( Object obj, int index ) { 99 if (index >= vector.size()) { 100 vector.setSize( index ); 101 } 102 vector.insertElementAt( obj, index ); 103 itemCount++; 104 } 105 106 107 114 public synchronized Object deleteAtIndex( int index ) { 115 Object old = vector.elementAt( index ); 116 if (old != null) { 117 itemCount--; 118 } 119 vector.removeElementAt( index ); 120 return old; 121 } 122 123 124 public int space() { 125 return vector.capacity(); 126 } 127 128 129 public DxIterator iterator() { 130 return new DxVectorIterator( this ); 131 } 132 133 134 public int count() { 135 return itemCount; 136 } 137 138 139 public int size() { 140 return vector.size(); 141 } 142 143 144 public boolean isEmpty() { 145 return vector.isEmpty(); 146 } 147 148 149 public synchronized void clear() { 150 vector.removeAllElements(); 151 itemCount = 0; 152 } 153 154 155 public Vector internalVector() { 156 return vector; 157 } 158 159 public void decCounter() { 160 itemCount--; 161 } 162 163 164 } 165 | Popular Tags |