1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import org.netbeans.api.mdr.MDRObject; 22 import org.netbeans.jmi.javamodel.Feature; 23 import org.netbeans.jmi.javamodel.JavaModelPackage; 24 import org.netbeans.mdr.handlers.ClassProxyHandler; 25 import org.netbeans.mdr.storagemodel.StorableClass; 26 import javax.jmi.model.MofClass; 27 import javax.jmi.reflect.RefObject; 28 import java.lang.ref.WeakReference ; 29 import java.util.*; 30 31 32 37 public abstract class MetadataElementClass extends ClassProxyHandler { 38 private WeakReference allOfClass = null; 39 private WeakReference allOfType = null; 40 41 42 public MetadataElementClass(StorableClass s) { 43 super(s); 44 } 45 46 protected boolean _pushIterators(Collection iterators, Object obj, boolean includeSupertypes) { 47 boolean pushed = false; 48 if (obj == null) 49 return pushed; 50 51 if (obj instanceof MetadataElement) { 71 Collection children = ((MetadataElement) obj).getChildren(); 72 if (children != null && !children.isEmpty()) { 73 iterators.add(children.iterator()); 74 pushed = true; 75 } 76 } 77 else if (obj instanceof List) { 78 iterators.add(((List) obj).iterator()); 79 pushed = true; 80 } 81 82 return pushed; 83 } 84 85 protected boolean _accept(Object obj, boolean includeSupertypes) { 86 if (obj == null || !(obj instanceof RefObject)) 87 return false; 88 return ((RefObject) obj).refIsInstanceOf(refMetaObject(), includeSupertypes); 89 } 90 91 protected boolean _isAbstract() { 92 return ((MofClass) refMetaObject()).isAbstract(); 93 } 94 95 protected Collection _allOfClass(boolean includeSubtypes) { 96 Collection result; 99 if (includeSubtypes) { 100 if (allOfType == null || (result = (Collection) allOfType.get()) == null) { 101 result = new AllOfClassCollection(true, (JavaModelPackage) refImmediatePackage()); 102 allOfType = new WeakReference (result); 103 } 104 } else { 105 if (_isAbstract()) { 106 result = Collections.EMPTY_LIST; 107 } else { 108 if (allOfClass == null || (result = (Collection) allOfClass.get()) == null) { 109 result = new AllOfClassCollection(false, (JavaModelPackage) refImmediatePackage()); 110 allOfClass = new WeakReference (result); 111 } 112 } 113 } 114 return result; 115 } 116 117 private class AllOfClassCollection extends AbstractCollection { 118 private final boolean includeSubtypes; 119 private final JavaModelPackage extent; 120 121 public AllOfClassCollection(boolean includeSubtypes, JavaModelPackage extent) { 122 this.includeSubtypes = includeSubtypes; 123 this.extent = extent; 124 } 125 126 public Iterator iterator() { 127 return new It(); 128 } 129 130 public int size() { 131 lock(); 132 try { 133 int size = 0; 134 for (Iterator it = iterator(); it.hasNext(); it.next(), size++); 135 return size; 136 } finally { 137 unlock(); 138 } 139 } 140 141 private void lock() { 142 ((MDRObject) extent).repository().beginTrans(false); 143 } 144 145 private void unlock() { 146 ((MDRObject) extent).repository().endTrans(); 147 } 148 149 private class It implements Iterator { 150 private final List iterators = new ArrayList(); 151 private Iterator currentIterator; 152 private Object lastItem = null; 153 154 public It() { 155 currentIterator = extent.getResource().refAllOfClass().iterator(); 156 } 157 158 public boolean hasNext() { 159 while (lastItem == null && (!iterators.isEmpty() || currentIterator.hasNext())) { 160 while (lastItem == null && currentIterator.hasNext()) { 161 Object temp = currentIterator.next(); 162 if (_pushIterators(iterators, temp, includeSubtypes)) { 163 iterators.add(currentIterator); 164 currentIterator = (Iterator) iterators.remove(0); 165 } 166 if (_accept(temp, includeSubtypes)) { 167 lastItem = temp; 168 } 169 else if (temp instanceof Feature && _accept(((Feature)temp).getJavadoc(), includeSubtypes)) { 170 lastItem = ((Feature)temp).getJavadoc(); 171 } 172 } 173 if (lastItem == null) { 174 currentIterator = (Iterator) iterators.remove(0); 175 } 176 } 177 return lastItem != null; 178 } 179 180 public Object next() { 181 lock(); 182 try { 183 if (hasNext()) { 184 Object result = lastItem; 185 lastItem = null; 186 return result; 187 } else { 188 throw new NoSuchElementException(); 189 } 190 } finally { 191 unlock(); 192 } 193 } 194 195 public void remove() { 196 throw new UnsupportedOperationException (); 197 } 198 } 199 } 200 } 201 | Popular Tags |