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