1 29 30 package com.caucho.quercus.env; 31 32 import com.caucho.quercus.program.JavaClassDef; 33 34 import com.caucho.quercus.QuercusRuntimeException; 35 import com.caucho.quercus.UnimplementedException; 36 37 import java.util.*; 38 import java.util.logging.*; 39 40 43 public class JavaCollectionAdapter extends JavaAdapter 44 { 45 private static final Logger log 46 = Logger.getLogger(JavaCollectionAdapter.class.getName()); 47 48 private Collection _collection; 50 51 public JavaCollectionAdapter(Env env, Collection coll, JavaClassDef def) 52 { 53 super(env, coll, def); 54 55 _collection = coll; 56 } 57 58 61 public void clear() 62 { 63 _collection.clear(); 64 } 65 66 public int size() 67 { 68 return _collection.size(); 69 } 70 71 75 78 public Value copy() 79 { 80 try { 81 Class cl = _collection.getClass(); 82 83 Collection coll = (Collection)cl.newInstance(); 84 85 coll.addAll(_collection); 86 87 return new JavaCollectionAdapter(getEnv(), coll, getClassDef()); 88 } 89 catch (Exception e) { 90 throw new QuercusRuntimeException(e); 91 } 92 } 93 94 97 public int getSize() 98 { 99 return size(); 100 } 101 102 105 public Value createTailKey() 106 { 107 return LongValue.create(size()); 108 } 109 110 public Value putImpl(Value key, Value value) 111 { 112 if (key.toInt() != size()) 113 throw new UnsupportedOperationException ("random assignment into Collection"); 114 115 _collection.add(value.toJavaObject()); 116 117 return value; 118 } 119 120 123 public Value get(Value key) 124 { 125 int pos = key.toInt(); 126 127 if (pos < 0) 128 return UnsetValue.UNSET; 129 130 for (Object obj : _collection) { 131 if (pos-- > 0) 132 continue; 133 134 return wrapJava(obj); 135 } 136 137 return UnsetValue.UNSET; 138 } 139 140 143 public Value remove(Value key) 144 { 145 int i = 0; 146 int pos = key.toInt(); 147 148 if (pos < 0) 149 return UnsetValue.UNSET; 150 151 for (Object obj : _collection) { 152 if (pos-- > 0) 153 continue; 154 155 Value val = wrapJava(obj); 156 157 _collection.remove(obj); 158 return val; 159 } 160 161 return UnsetValue.UNSET; 162 } 163 164 167 public Set<Map.Entry<Value,Value>> entrySet() 168 { 169 return new CollectionValueSet(); 170 } 171 172 175 public Set<Map.Entry> objectEntrySet() 176 { 177 return new CollectionSet(); 178 } 179 180 183 public Collection<Value> values() 184 { 185 return new ValueCollection(); 186 } 187 188 public Value []getValueArray(Env env) 189 { 190 Value[] values = new Value[getSize()]; 191 192 int i = 0; 193 for (Object ob: _collection) { 194 values[i++] = env.wrapJava(ob); 195 } 196 197 return values; 198 } 199 200 public class CollectionSet 201 extends AbstractSet<Map.Entry> 202 { 203 CollectionSet() 204 { 205 } 206 207 public int size() 208 { 209 return getSize(); 210 } 211 212 public Iterator<Map.Entry> iterator() 213 { 214 return new CollectionIterator(_collection); 215 } 216 } 217 218 public class CollectionIterator 219 implements Iterator 220 { 221 private int _index; 222 private Iterator _iterator; 223 224 public CollectionIterator(Collection collection) 225 { 226 _index = 0; 227 _iterator = collection.iterator(); 228 } 229 230 public boolean hasNext() 231 { 232 return _iterator.hasNext(); 233 } 234 235 public Map.Entry next() 236 { 237 return new CollectionEntry(_index++, _iterator.next()); 238 } 239 240 public void remove() 241 { 242 throw new UnsupportedOperationException (); 243 } 244 } 245 246 public static class CollectionEntry 247 implements Map.Entry 248 { 249 private final int _key; 250 private Object _value; 251 252 public CollectionEntry(int key, Object value) 253 { 254 _key = key; 255 _value = value; 256 } 257 258 public Object getKey() 259 { 260 return Long.valueOf(_key); 261 } 262 263 public Object getValue() 264 { 265 return _value; 266 } 267 268 public Object setValue(Object value) 269 { 270 Object oldValue = _value; 271 272 _value = value; 273 274 return oldValue; 275 } 276 } 277 278 public class CollectionValueSet 279 extends AbstractSet<Map.Entry<Value,Value>> 280 { 281 CollectionValueSet() 282 { 283 } 284 285 public int size() 286 { 287 return getSize(); 288 } 289 290 public Iterator<Map.Entry<Value,Value>> iterator() 291 { 292 return new CollectionValueIterator(_collection); 293 } 294 } 295 296 public class CollectionValueIterator 297 implements Iterator<Map.Entry<Value,Value>> 298 { 299 private int _index; 300 private Iterator _iterator; 301 302 public CollectionValueIterator(Collection collection) 303 { 304 _index = 0; 305 _iterator = collection.iterator(); 306 } 307 308 public boolean hasNext() 309 { 310 return _iterator.hasNext(); 311 } 312 313 public Map.Entry<Value,Value> next() 314 { 315 Value val = wrapJava(_iterator.next()); 316 317 return new CollectionValueEntry(LongValue.create(_index++), val); 318 } 319 320 public void remove() 321 { 322 throw new UnsupportedOperationException (); 323 } 324 } 325 326 public static class CollectionValueEntry 327 implements Map.Entry<Value,Value> 328 { 329 private final Value _key; 330 private Value _value; 331 332 public CollectionValueEntry(Value key, Value value) 333 { 334 _key = key; 335 _value = value; 336 } 337 338 public Value getKey() 339 { 340 return _key; 341 } 342 343 public Value getValue() 344 { 345 return _value; 346 } 347 348 public Value setValue(Value value) 349 { 350 Value oldValue = _value; 351 352 _value = value; 353 354 return oldValue; 355 } 356 } 357 358 public class ValueCollection 359 extends AbstractCollection<Value> 360 { 361 ValueCollection() 362 { 363 } 364 365 public int size() 366 { 367 return getSize(); 368 } 369 370 public Iterator<Value> iterator() 371 { 372 return new ValueIterator(_collection); 373 } 374 } 375 376 public class ValueIterator 377 implements Iterator<Value> 378 { 379 private Iterator _iterator; 380 381 public ValueIterator(Collection collection) 382 { 383 _iterator = collection.iterator(); 384 } 385 386 public boolean hasNext() 387 { 388 return _iterator.hasNext(); 389 } 390 391 public Value next() 392 { 393 return wrapJava(_iterator.next()); 394 } 395 396 public void remove() 397 { 398 throw new UnsupportedOperationException (); 399 } 400 } 401 402 } 403 | Popular Tags |