1 2 12 package com.versant.core.jdo; 13 14 import com.versant.core.common.Debug; 15 16 import java.util.*; 17 18 import com.versant.core.common.BindingSupportImpl; 19 import com.versant.core.jdo.query.mem.BCodeQuery; 20 import com.versant.core.jdo.query.mem.BCodeSorter; 21 import com.versant.core.jdo.VersantPersistenceManagerImp; 22 import com.versant.core.jdo.query.mem.BCodeSorter; 23 import com.versant.core.jdo.query.mem.BCodeQuery; 24 25 28 public class MemoryQueryResult extends QueryResultBase { 29 30 private final List internalList = new ArrayList(); 31 private boolean closed; 32 private final List operIters = new ArrayList(); 33 boolean toSort = false; 34 private BCodeQuery bCodeQuery = null; 35 private final Class candidateClass; 36 private final boolean checkExtentSubClasses; 37 38 public MemoryQueryResult(PMProxy sm, QueryDetails queryDetails, 39 Collection col, Object [] params) { 40 checkExtentSubClasses = queryDetails.includeSubClasses(); 41 this.candidateClass = queryDetails.getCandidateClass(); 42 createDynamicQuery(sm.getRealPM(), queryDetails, params); 43 44 filter(col, internalList, params, sm); 45 int n = internalList.size(); 46 for (int i = n - 1; i >= 0; i--) { 47 internalList.set(i, sm.getObjectById(internalList.get(i), false)); 48 } 49 Collections.reverse(internalList); 50 } 51 52 public MemoryQueryResult() { 53 candidateClass = null; 54 checkExtentSubClasses = false; 55 } 56 57 public void filter(Collection toFilter, List results, Object [] params, 58 PMProxy pm) { 59 results.clear(); 61 PCStateMan pcStateObject = null; 62 for (Iterator iterator = toFilter.iterator(); iterator.hasNext();) { 63 pcStateObject = (PCStateMan)iterator.next(); 64 if (checkType(pcStateObject) && checkFilter(pcStateObject, params)) { 65 results.add(pcStateObject.oid); 66 } 67 } 68 if (toSort) { 69 sort(results, pm); 70 } 71 } 75 76 private void sort(List list, PMProxy pm) { 77 if (list == null || list.isEmpty()) { 78 return; 79 } 80 BCodeSorter bCodeSorter = new BCodeSorter(); 81 bCodeSorter.sort(list, pm, bCodeQuery); 82 } 83 84 90 public final boolean checkType(PCStateMan pcStateObject) { 91 if (checkExtentSubClasses) { 92 return candidateClass.isAssignableFrom(pcStateObject.pc.getClass()); 93 } else { 94 return pcStateObject.pc.getClass() == candidateClass; 95 } 96 } 97 98 103 public final boolean checkFilter(PCStateMan pcStateObject, Object [] params) { 104 if (pcStateObject.isDeleted(null)) return false; 105 try { 106 boolean result = bCodeQuery.exec(pcStateObject.queryStateWrapper, 107 params); 108 return result; 109 } catch (Exception e) { 110 if (Debug.DEBUG) { 111 e.printStackTrace(Debug.OUT); 112 } 113 return false; 114 } 115 } 116 117 private void createDynamicQuery(VersantPersistenceManagerImp sm, 118 QueryDetails queryParams, Object [] params) { 119 120 bCodeQuery = sm.getMemQueryCompiler().compile(queryParams, 121 params); 122 123 toSort = isOrdered(queryParams); 124 } 125 126 private final boolean isOrdered(QueryDetails queryParams) { 127 return queryParams.getOrdering() != null; 128 } 129 130 public int size() { 131 return internalList.size(); 132 } 133 134 public boolean isEmpty() { 135 return internalList.isEmpty(); 136 } 137 138 public boolean contains(Object o) { 139 return internalList.contains(o); 140 } 141 142 public Object [] toArray() { 143 return internalList.toArray(); 144 } 145 146 public Object [] toArray(Object a[]) { 147 return internalList.toArray(a); 148 } 149 150 public boolean containsAll(Collection c) { 151 return internalList.containsAll(c); 152 } 153 154 public Object get(int index) { 155 return internalList.get(index); 156 } 157 158 public int indexOf(Object o) { 159 return internalList.indexOf(o); 160 } 161 162 public int lastIndexOf(Object o) { 163 return internalList.lastIndexOf(o); 164 } 165 166 public Iterator iterator() { 167 Iterator iter = new InternalIter(internalList.listIterator()); 168 operIters.add(iter); 169 return iter; 170 } 171 172 public Iterator createInternalIter() { 173 return iterator(); 174 } 175 176 public Iterator createInternalIterNoFlush() { 177 return iterator(); 178 } 179 180 public ListIterator listIterator() { 181 ListIterator iter = new InternalIter(internalList.listIterator()); 182 operIters.add(iter); 183 return iter; 184 } 185 186 public ListIterator listIterator(int index) { 187 ListIterator iter = new InternalIter(internalList.listIterator(index)); 188 operIters.add(iter); 189 return iter; 190 } 191 192 public List subList(int fromIndex, int toIndex) { 193 throw BindingSupportImpl.getInstance().unsupported(""); 194 } 195 196 public void close() { 197 closed = true; 198 for (int i = 0; i < operIters.size(); i++) { 199 ((InternalIter)operIters.get(i)).wrapperIter = null; 200 } 201 internalList.clear(); 202 operIters.clear(); 203 } 204 205 public void setParams(Object [] params) { 206 } 207 208 public class InternalIter implements ListIterator { 209 210 private ListIterator wrapperIter; 211 212 public InternalIter(ListIterator wrapperIter) { 213 this.wrapperIter = wrapperIter; 214 } 215 216 public boolean hasNext() { 217 if (closed) { 218 return false; 219 } 220 return wrapperIter.hasNext(); 221 } 222 223 public Object next() { 224 if (closed) { 225 throw BindingSupportImpl.getInstance().noSuchElement(""); 226 } 227 return wrapperIter.next(); 228 } 229 230 public boolean hasPrevious() { 231 if (closed) { 232 return false; 233 } 234 return wrapperIter.hasPrevious(); 235 } 236 237 public Object previous() { 238 if (closed) { 239 throw BindingSupportImpl.getInstance().noSuchElement(""); 240 } 241 return wrapperIter.previous(); 242 } 243 244 public int nextIndex() { 245 if (closed) { 246 return -1; 247 } 248 return wrapperIter.nextIndex(); 249 } 250 251 public int previousIndex() { 252 if (closed) { 253 return -1; 254 } 255 return wrapperIter.previousIndex(); 256 } 257 258 public void remove() { 259 throw BindingSupportImpl.getInstance().invalidOperation("Modification is not allowed"); 260 } 261 262 public void set(Object o) { 263 throw BindingSupportImpl.getInstance().invalidOperation("Modification is not allowed"); 264 } 265 266 public void add(Object o) { 267 throw BindingSupportImpl.getInstance().invalidOperation("Modification is not allowed"); 268 } 269 } 270 } 271 | Popular Tags |