1 19 package org.netbeans.mdr.handlers; 20 21 import java.util.*; 22 import java.lang.reflect.Array ; 23 import javax.jmi.reflect.RefBaseObject; 24 25 import org.netbeans.mdr.storagemodel.*; 26 import org.netbeans.mdr.util.DebugException; 27 28 33 public class IndexSetWrapper implements Collection { 34 protected final Collection inner; 35 protected final MdrStorage storage; 36 37 38 public IndexSetWrapper(MdrStorage mdrStorage, Collection inner) { 39 this.inner = inner; 40 this.storage = mdrStorage; 41 } 42 43 protected void lock(boolean writeAccess) { 44 storage.getRepository().beginTrans(writeAccess); 45 } 46 47 protected void unlock() { 48 storage.getRepository().endTrans(); 49 } 50 51 protected void unlock(boolean fail) { 52 storage.getRepository().endTrans(fail); 53 } 54 55 protected static Object wrap(Object obj) { 56 if (obj == null) { 57 return null; 58 } else if (obj instanceof StorableBaseObject) { 59 return ((StorableBaseObject) obj).getMdrStorage().getRepository().getHandler((StorableBaseObject) obj); 60 } else if (obj instanceof AssociationLink) { 61 return AssociationLinkWrapper.wrapLink((AssociationLink) obj); 62 } else { 63 throw new DebugException("Invalid object in collection: " + obj.getClass().getName()); 64 } 65 } 66 67 protected static Object unwrap(Object obj) { 68 if (obj == null) { 69 return null; 70 } else if (obj instanceof RefBaseObject) { 71 return ((BaseObjectHandler) obj)._getDelegate().getMofId(); 72 } else if (obj instanceof AssociationLinkWrapper) { 73 return ((AssociationLinkWrapper) obj).getInnerLink(); 74 } else { 75 return obj; 76 } 77 } 78 79 protected static Object [] wrapArray(Object [] array) { 80 for (int i = 0; i < array.length; i++) { 81 array[i] = wrap(array[i]); 82 } 83 84 return array; 85 } 86 87 public boolean contains(Object obj) { 88 lock(false); 89 try { 90 return inner.contains(unwrap(obj)); 91 } finally { 92 unlock(); 93 } 94 } 95 96 public Iterator iterator() { 97 lock(false); 98 try { 99 return new IndexIteratorWrapper(inner.iterator()); 100 } finally { 101 unlock(); 102 } 103 } 104 105 public int size() { 106 try { 107 lock(false); 108 return inner.size(); 109 } finally { 110 unlock(); 111 } 112 } 113 114 public boolean isEmpty() { 115 lock(false); 116 try { 117 return inner.isEmpty(); 118 } finally { 119 unlock(); 120 } 121 } 122 123 public boolean containsAll(Collection collection) { 124 lock(false); 125 try { 126 return inner.containsAll(new CollectionUnwrapper(collection)); 127 } finally { 128 unlock(); 129 } 130 } 131 132 public Object [] toArray(Object [] obj) { 133 lock(false); 134 try { 135 Object [] value = wrapArray(inner.toArray()); 136 Object [] result = obj; 137 if (value.length > result.length) { 138 if (value.getClass() == result.getClass()) { 139 return value; 140 } else { 141 result = (Object []) Array.newInstance(obj.getClass().getComponentType(), 142 value.length); 143 } 144 } else if (value.length < result.length) { 145 result[value.length] = null; 146 } 147 System.arraycopy(value, 0, result, 0, value.length); 148 return result; 149 } finally { 150 unlock(); 151 } 152 } 153 154 public boolean equals(Object object) { 155 if (object == this) return true; 156 if (!(object instanceof Collection)) return false; 157 lock(false); 158 try { 159 Iterator it1 = iterator(); 160 Iterator it2 = ((Collection) object).iterator(); 161 while(it1.hasNext() && it2.hasNext()) { 162 Object o1 = it1.next(); 163 Object o2 = it2.next(); 164 if (!(o1==null ? o2==null : o1.equals(o2))) 165 return false; 166 } 167 return !(it1.hasNext() || it2.hasNext()); 168 } finally { 169 unlock(); 170 } 171 } 172 173 public int hashCode() { 174 lock(false); 175 try { 176 int hashCode = 1; 177 for (Iterator it = iterator(); it.hasNext();) { 178 Object obj = it.next(); 179 hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode()); 180 } 181 return hashCode; 182 } finally { 183 unlock(); 184 } 185 } 186 187 public Object [] toArray() { 188 return toArray(new Object [size()]); 189 } 190 191 public void clear() { 192 throw new UnsupportedOperationException (); 193 } 194 195 public boolean addAll(Collection collection) { 196 throw new UnsupportedOperationException (); 197 } 198 199 public boolean remove(Object obj) { 200 throw new UnsupportedOperationException (); 201 } 202 203 public boolean add(Object obj) { 204 throw new UnsupportedOperationException (); 205 } 206 207 public boolean retainAll(Collection collection) { 208 throw new UnsupportedOperationException (); 209 } 210 211 public boolean removeAll(Collection collection) { 212 throw new UnsupportedOperationException (); 213 } 214 215 protected class IndexIteratorWrapper implements Iterator { 216 protected Iterator innerIterator; 217 218 public IndexIteratorWrapper(Iterator innerIterator) { 219 this.innerIterator = innerIterator; 220 } 221 222 public boolean hasNext() { 223 lock(false); 224 try { 225 return innerIterator.hasNext(); 226 } finally { 227 unlock(); 228 } 229 } 230 231 public Object next() { 232 lock(false); 233 try { 234 return wrap(innerIterator.next()); 235 } finally { 236 unlock(); 237 } 238 } 239 240 public void remove() { 241 throw new UnsupportedOperationException (); 242 } 243 } 244 } 245 | Popular Tags |