1 19 package org.netbeans.mdr.storagemodel; 20 21 import java.util.*; 22 import java.lang.reflect.Array ; 23 24 import org.netbeans.mdr.util.DebugException; 25 import org.netbeans.mdr.persistence.StorageException; 26 import org.netbeans.mdr.util.Logger; 27 28 35 class CachedCollection implements Collection { 36 private final Collection innerCollection; 37 private final MdrStorage storage; 38 39 44 public CachedCollection(MdrStorage storage, Collection innerCollection) { 45 this.innerCollection = innerCollection; 46 this.storage = storage; 47 } 48 49 public boolean retainAll(Collection collection) { 50 throw new UnsupportedOperationException (); 51 } 52 53 public boolean contains(Object obj) { 54 return innerCollection.contains(obj); 55 } 56 57 public Object [] toArray(Object [] obj) { 58 Object [] value = toArray(); 59 Object [] result = obj; 60 if (value.length > result.length) { 61 if (value.getClass() == result.getClass()) { 62 return value; 63 } else { 64 result = (Object []) Array.newInstance(obj.getClass(), value.length); 65 } 66 } 67 for (int i = 0; i < result.length; i++) { 68 result[i] = (i < value.length) ? value[i] : null; 69 } 70 return result; 71 } 72 73 public Iterator iterator() { 74 return new CachedIterator(innerCollection.iterator()); 75 } 76 77 public boolean removeAll(Collection collection) { 78 throw new UnsupportedOperationException (); 79 } 80 81 public Object [] toArray() { 82 Object [] array = innerCollection.toArray(); 83 84 try { 85 for (int i = 0; i < array.length; i++) { 86 array[i] = storage.getObject((org.netbeans.mdr.persistence.MOFID) array[i]); 87 } 88 } catch (Exception e) { 89 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 90 } 91 92 return array; 93 } 94 95 public boolean remove(Object obj) { 96 throw new UnsupportedOperationException (); 97 } 98 99 public void clear() { 100 throw new UnsupportedOperationException (); 101 } 102 103 public boolean addAll(Collection collection) { 104 throw new UnsupportedOperationException (); 105 } 106 107 public int size() { 108 return innerCollection.size(); 109 } 110 111 public boolean containsAll(Collection collection) { 112 return innerCollection.containsAll(collection); 113 } 114 115 public boolean add(Object obj) { 116 throw new UnsupportedOperationException (); 117 } 118 119 public boolean isEmpty() { 120 return innerCollection.isEmpty(); 121 } 122 123 private class CachedIterator implements Iterator { 124 private final Iterator innerIterator; 125 126 private CachedIterator(Iterator innerIterator) { 127 this.innerIterator = innerIterator; 128 } 129 130 public boolean hasNext() { 131 return innerIterator.hasNext(); 132 } 133 134 public Object next() { 135 try { 136 return storage.getObject((org.netbeans.mdr.persistence.MOFID) innerIterator.next()); 137 } catch (StorageException e) { 138 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 139 } 140 } 141 142 public void remove() { 143 throw new UnsupportedOperationException (); 144 } 145 } 146 } 147 | Popular Tags |