1 19 package org.netbeans.mdr.storagemodel; 20 21 import org.netbeans.mdr.util.Logger; 22 23 import java.util.*; 24 25 import javax.jmi.reflect.*; 26 27 import org.netbeans.mdr.persistence.*; 28 import org.netbeans.mdr.util.DebugException; 29 30 34 public class AssocEndIndexSet extends IndexImmutSet implements TypedCollection { 35 protected final MOFID metaMofId; 36 protected final MOFID metaMofIdOther; 37 protected final Class type; 38 protected final int maxSize; 39 protected final StorableAssociation storable; 40 protected final Index secondIndex; 41 protected final boolean mutable; 42 protected final boolean isAggregate; 43 protected final boolean isAggregateOther; 44 protected final boolean isIndexed; 45 protected final boolean isIndexedOther; 46 protected final StorableObject keyObject; 47 48 protected AssocEndIndexSet(StorableAssociation storable, MOFID metaMofId, MOFID metaMofIdOther, MultivaluedIndex index, Object indexKey, Index secondIndex, Class type, int max, boolean mutable, boolean isAggregate, boolean isAggregateOther, boolean isIndexed, boolean isIndexedOther) { 49 super(storable.getMdrStorage(), index, indexKey); 50 this.metaMofId = metaMofId; 51 this.metaMofIdOther = metaMofIdOther; 52 this.type = type; 53 this.maxSize = max; 54 this.storable = storable; 55 this.secondIndex = secondIndex; 56 this.mutable = mutable; 57 this.isAggregate = isAggregate; 58 this.isAggregateOther = isAggregateOther; 59 this.isIndexed = isIndexed; 60 this.isIndexedOther = isIndexedOther; 61 62 if (isAggregate || isAggregateOther || isIndexed) { 63 try { 64 keyObject = (StorableObject) storage.getObject((MOFID) indexKey); 65 } catch (StorageException e) { 66 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 67 } 68 } else { 69 keyObject = null; 70 } 71 } 72 73 public void checkType(Object obj) { 74 if (!mutable) throw new UnsupportedOperationException (); 75 76 if (obj == null) { 77 throw new NullPointerException (); 78 } 79 80 if (!type.isInstance(obj)) { 81 throw new TypeMismatchException(type, obj, getMetaElement()); 82 } 83 } 84 85 protected void checkMaxSize(int size) { 86 if (type == null) { 87 throw new UnsupportedOperationException (); 88 } else if (maxSize != -1) { 89 if (maxSize < size + size()) throw new WrongSizeException(getMetaElement()); 90 } 91 } 92 93 protected RefObject getMetaElement() { 94 try { 95 return (RefObject) storage.getRepository().getHandler(storage.getObject(metaMofId)); 96 } catch (Exception e) { 97 return null; 98 } 99 } 100 101 public Iterator iterator() { 102 return new IndexIterator(getObjects().iterator()); 103 } 104 105 public boolean remove(Object o) { 106 try { 107 StorableObject so = null; 108 109 if (isAggregate) { 110 keyObject.clearComposite(); 111 } else if (isAggregateOther) { 112 if (so == null) 113 so = (StorableObject) storage.getObject((MOFID) o); 114 so.clearComposite(); 115 } 116 117 if (isIndexed) { 118 keyObject.removeFromIndex (metaMofId); 119 } 120 if (isIndexedOther) { 121 if (so == null) 122 so = (StorableObject) storage.getObject((MOFID) o); 123 so.removeFromIndex (metaMofIdOther); 124 } 125 126 boolean result = this.index.remove(indexKey, o); 127 if (result) { 128 if (secondIndex instanceof SinglevaluedIndex) { 129 secondIndex.remove(o); 130 } else { 131 ((MultivaluedIndex) secondIndex).remove(o, indexKey); 132 } 133 } 134 if (isIndexed) { 135 keyObject.addToIndex (metaMofId); 136 } 137 if (isIndexedOther) { 138 so.addToIndex (metaMofIdOther); 139 } 140 141 return result; 142 } catch (StorageException e) { 143 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 144 } 145 } 146 147 public boolean add(Object obj) { 148 checkMaxSize(1); 149 try { 150 StorableObject so = null; 151 if (isAggregate) { 152 keyObject.setComposite((MOFID) obj, (MOFID) obj, metaMofId); 153 } else if (isAggregateOther) { 154 if (so == null) 155 so = (StorableObject) storage.getObject((MOFID) obj); 156 so.setComposite(keyObject, (MOFID) obj, metaMofId); 157 } 158 if (isIndexed) { 159 keyObject.removeFromIndex (metaMofId); 160 } 161 if (isIndexedOther) { 162 if (so == null) 163 so = (StorableObject) storage.getObject((MOFID) obj); 164 so.removeFromIndex (metaMofIdOther); 165 } 166 index.add(indexKey, obj); 167 secondIndex.add(obj, indexKey); 168 if (isIndexed) { 169 keyObject.addToIndex (metaMofId); 170 } 171 if (isIndexedOther) { 172 so.addToIndex (metaMofIdOther); 173 } 174 return true; 175 } catch (StorageBadRequestException e) { 176 return false; 178 } catch (StorageException e) { 179 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 180 } 181 } 182 183 public boolean removeAll(Collection collection) { 184 throw new DebugException(); 186 } 187 188 public boolean addAll(Collection collection) { 189 throw new DebugException(); 191 } 192 193 public boolean retainAll(Collection collection) { 194 throw new DebugException(); 196 } 197 198 public void clear() { 199 throw new DebugException(); 201 } 202 203 protected class IndexIterator extends IndexImmutIterator { 204 protected Object lastRead = null; 205 206 protected IndexIterator(Iterator innerIterator) { 207 super(innerIterator); 208 } 209 210 public Object next() { 211 return (lastRead = super.next()); 212 } 213 214 public void remove() { 215 innerIterator.remove(); 216 try { 217 if (isAggregate) { 218 keyObject.clearComposite(); 219 } else if (isAggregateOther) { 220 ((StorableObject) lastRead).clearComposite(); 221 } 222 223 if (isIndexed) { 224 keyObject.removeFromIndex (metaMofId); 225 } 226 if (isIndexedOther) { 227 ((StorableObject) lastRead).removeFromIndex (metaMofIdOther); 228 } 229 230 MOFID key = ((StorableBaseObject) lastRead).getMofId (); 231 if (secondIndex instanceof MultivaluedIndex) { 232 ((MultivaluedIndex) secondIndex).remove(key, indexKey); 233 } else { 234 secondIndex.remove(key); 235 } 236 237 if (isIndexed) { 238 keyObject.addToIndex (metaMofId); 239 } 240 if (isIndexedOther) { 241 ((StorableObject) lastRead).addToIndex (metaMofIdOther); 242 } 243 } catch (StorageException e) { 244 throw new DebugException(); 245 } 246 } 247 } 248 } 249 | Popular Tags |