1 9 package org.ozoneDB.DxLib; 10 11 import org.ozoneDB.io.stream.ResolvingObjectInputStream; 12 13 import java.io.*; 14 15 16 19 public abstract class DxAbstractCollection extends DxObject implements DxCollection, Externalizable { 20 21 final static long serialVersionUID = 2L; 22 23 24 public DxAbstractCollection() { 25 } 26 27 30 35 36 public Object clone( DxCollection newInstance ) { 37 try { 38 DxIterator it = iterator(); 39 Object obj; 40 while ((obj = it.next()) != null) { 41 newInstance.add( obj ); 42 } 43 return newInstance; 44 } catch (Exception e) { 45 throw new RuntimeException ( e.toString() ); 46 } 47 } 48 49 50 54 public DxCollection valueClone() { 55 try { 56 DxCollection coll = (DxCollection)getClass().newInstance(); 57 DxIterator it = iterator(); 58 while (it.next() != null) { 59 Object obj = it.object(); 60 61 if (obj instanceof DxCompatible) { 62 coll.add( ((DxCompatible)obj).clone() ); 63 } else if (obj instanceof Serializable) { 64 65 ByteArrayOutputStream bout = new ByteArrayOutputStream( 4096 ); 66 ObjectOutputStream out = new ObjectOutputStream( bout ); 67 out.writeObject( obj ); 68 out.close(); 69 70 ObjectInputStream in = new ResolvingObjectInputStream( new ByteArrayInputStream( bout.toByteArray() ) ); 71 coll.add( in.readObject() ); 72 } else { 73 74 throw new CloneNotSupportedException (); 75 } 76 } 77 return coll; 78 } catch (Exception e) { 79 throw new RuntimeException ( e.toString() ); 80 } 81 } 82 83 84 public Object [] toArray() { 85 Object [] result = new Object [count()]; 86 DxIterator it = iterator(); 87 Object obj; 88 for (int i = 0; (obj = it.next()) != null; i++) { 89 result[i] = obj; 90 } 91 return result; 92 } 93 94 95 101 public boolean equals( Object obj ) { 102 return super.equals( obj ); 103 104 } 131 132 133 public synchronized boolean addAll( DxCollection coll ) { 134 boolean answer = false; 135 DxIterator it = coll.iterator(); 136 Object obj; 137 while ((obj = it.next()) != null) { 138 if (add( obj )) { 139 answer = true; 140 } 141 } 142 return answer; 143 } 144 145 146 public synchronized boolean addAll( Object [] objs ) { 147 boolean answer = false; 148 for (int i = 0; i < objs.length; i++) { 149 if (add( objs[i] )) { 150 answer = true; 151 } 152 } 153 return answer; 154 } 155 156 157 161 public synchronized boolean remove( Object obj ) { 162 boolean answer = false; 163 DxIterator it = iterator(); 164 Object cursor; 165 while ((cursor = it.next()) != null) { 166 if (obj.equals( cursor )) { 167 it.removeObject(); 168 answer = true; 169 } 170 } 171 return answer; 172 } 173 174 175 public synchronized boolean removeAll( DxCollection coll ) { 176 boolean answer = false; 177 if (!coll.isEmpty() && !isEmpty()) { 178 DxIterator it = coll.iterator(); 179 Object obj; 180 while ((obj = it.next()) != null) { 181 if (remove( obj )) { 182 answer = true; 183 } 184 } 185 } 186 return answer; 187 } 188 189 190 194 public boolean contains( Object obj ) { 195 DxIterator it = iterator(); 196 Object cursor; 197 while ((cursor = it.next()) != null) { 198 if (obj.equals( cursor )) { 199 return true; 200 } 201 } 202 return false; 203 } 204 205 206 public boolean containsAll( DxCollection coll ) { 207 DxIterator it = coll.iterator(); 208 Object cursor; 209 while ((cursor = it.next()) != null) { 210 if (!contains( cursor )) { 211 return false; 212 } 213 } 214 return true; 215 } 216 217 218 public void writeExternal( ObjectOutput out ) throws IOException { 219 out.writeInt( count() ); 221 DxIterator it = iterator(); 222 Object obj; 223 while ((obj = it.next()) != null) { 224 out.writeObject( obj ); 225 } 226 } 227 228 229 public synchronized void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException { 230 int count = in.readInt(); 231 for (; count > 0; count--) { 232 add( in.readObject() ); 233 } 234 } 235 236 } 237 | Popular Tags |