1 18 package org.objectweb.speedo.query.lib; 19 20 import org.objectweb.speedo.query.api.QueryDefinition; 21 22 import javax.jdo.Extent; 23 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Collection ; 27 28 33 public class QueryDefinitionImpl implements QueryDefinition { 34 42 protected boolean ignoreCache = true; 43 44 47 protected Collection candidateInstances = null; 48 protected Extent extentClass = null; 49 50 53 protected Class candidateClass = null; 54 55 58 protected String filter = "true"; 59 60 63 protected String parameters = null; 64 protected String variables = null; 65 66 69 protected List importStatements = null; 70 protected List order = null; 71 72 transient protected long indexFirst = 0; 73 74 transient protected long indexLast = Long.MAX_VALUE; 75 76 protected boolean unique; 77 78 protected String result; 79 80 protected Class resultClass; 81 82 protected boolean withPrefetch; 83 84 boolean includeSubClasses = true; 85 86 protected String grouping; 87 88 public QueryDefinitionImpl() { 89 } 90 91 public QueryDefinitionImpl(QueryDefinition qd) { 92 defineWith(qd); 93 } 94 95 public void defineWith(QueryDefinition qd) { 96 if (qd == null) { 97 this.candidateClass = null; 98 this.candidateInstances = null; 99 this.extentClass = null; 100 this.filter = null; 101 this.order = null; 102 this.variables = null; 103 this.parameters = null; 104 this.importStatements = null; 105 this.ignoreCache = true; 106 this.indexFirst = 0; 107 this.indexLast = Long.MAX_VALUE; 108 this.withPrefetch = true; 109 this.grouping = null; 110 this.result = null; 111 this.resultClass = null; 112 this.includeSubClasses = true; 113 this.unique = false; 114 } else { 115 this.candidateClass = qd.getCandidateClass(); 116 this.candidateInstances = qd.getCollection(); 117 this.extentClass = qd.getExtent(); 118 this.filter = qd.getFilter(); 119 this.order = qd.getOrder(); 120 this.variables = qd.getVariables(); 121 this.parameters = qd.getParameters(); 122 this.importStatements = qd.getImportStatements(); 123 this.ignoreCache = qd.isIgnoreCache(); 124 this.indexFirst = qd.getIndexFirst(); 125 this.indexLast = qd.getIndexLast(); 126 this.withPrefetch = qd.withPrefetch(); 127 this.grouping = qd.getGrouping(); 128 this.result = qd.getResult(); 129 this.resultClass = qd.getResultClass(); 130 this.includeSubClasses = qd.getIncludeSubClasses(); 131 this.unique = qd.getUnique(); 132 } 133 } 134 135 136 public Class getCandidateClass() { 137 return candidateClass; 138 } 139 140 public boolean isCollection() { 141 return candidateInstances != null; 142 } 143 144 public Collection getCollection() { 145 return candidateInstances; 146 } 147 148 public String getParameters() { 149 return parameters; 150 } 151 152 public String getVariables() { 153 return variables; 154 } 155 156 public String getFilter() { 157 return filter; 158 } 159 160 public Extent getExtent() { 161 return extentClass; 162 } 163 164 public List getImportStatements() { 165 return importStatements; 166 } 167 168 public boolean isIgnoreCache() { 169 return ignoreCache; 170 } 171 172 public List getOrder() { 173 return order; 174 } 175 176 public long getIndexFirst() { 177 return indexFirst; 178 } 179 180 public long getIndexLast() { 181 return indexLast; 182 } 183 184 public boolean getUnique() { 185 return unique; 186 } 187 188 public String getResult() { 189 return result; 190 } 191 192 public Class getResultClass() { 193 return resultClass; 194 } 195 196 public boolean getIncludeSubClasses() { 197 return includeSubClasses; 198 } 199 200 public String getGrouping() { 201 return grouping; 202 } 203 public boolean withPrefetch() { 204 return withPrefetch; 205 } 206 public void withPrefetch(boolean withPrefetch) { 207 this.withPrefetch = withPrefetch; 208 } 209 public String toString() { 210 return qdToString(true); 211 } 212 public String qdToString(final boolean oneLine) { 213 final StringBuffer sb = new StringBuffer (); 214 String sep = ""; 215 final String sepNext = (oneLine ? ", " : "\n\t-"); 216 if (candidateClass!=null) { 217 sb.append(sep).append("class=\"").append(candidateClass.getName()).append("\""); 218 sep = sepNext; 219 } 220 if (parameters!=null && parameters.length() >0) { 221 sb.append(sep).append("parameters=\"").append(parameters).append("\""); 222 sep = sepNext; 223 } 224 if (variables!=null && variables.length() >0) { 225 sb.append(sep).append("variables=\"").append(variables).append("\""); 226 sep = sepNext; 227 } 228 if (filter!=null && filter.length() >0 && !filter.equals("true")) { 229 sb.append(sep).append("filter=\"").append(filter).append("\""); 230 sep = sepNext; 231 } 232 if (order!=null && order.size() >0) { 233 sb.append(sep).append("order=\""); 234 for (Iterator iter = order.iterator(); iter.hasNext();) { 235 sb.append(iter.next()); 236 } 237 sb.append("\""); 238 sep = sepNext; 239 } 240 if (result != null) { 241 sb.append(sep).append("result=\"").append(result).append("\""); 242 sep = sepNext; 243 } 244 if (resultClass != null) { 245 sb.append(sep).append("resultClass=\"").append(resultClass).append("\""); 246 sep = sepNext; 247 } 248 if (grouping!=null && grouping.length() >0) { 249 sb.append(sep).append("grouping=\"").append(grouping).append("\""); 250 sep = sepNext; 251 } 252 if (unique) { 253 sb.append(sep).append("unique=\"").append(unique).append("\""); 254 sep = sepNext; 255 } 256 if (ignoreCache) { 257 sb.append(sep).append("ignoreCache=\"").append(ignoreCache).append("\""); 258 sep = sepNext; 259 } 260 if (!withPrefetch) { 261 sb.append(sep).append("withPrefetch=\"").append(withPrefetch).append("\""); 262 sep = sepNext; 263 } 264 return sb.toString(); 265 } 266 267 public int hashCode() { 268 return candidateClass.hashCode(); 269 } 270 271 public boolean equals(Object obj) { 272 if (obj == null || !(obj instanceof QueryDefinition)) { 273 return false; 274 } 275 QueryDefinition qd = (QueryDefinition) obj; 276 return (candidateClass==null 277 ? qd.getCandidateClass()==null 278 : candidateClass.equals(qd.getCandidateClass())) 279 && (parameters == null 280 ? qd.getParameters() == null 281 : parameters.equals(qd.getParameters())) 282 && (variables == null 283 ? qd.getVariables() == null 284 : variables.equals(qd.getVariables())) 285 && (filter == null 286 ? qd.getFilter() == null 287 : filter.equals(qd.getFilter())) 288 && (order == null 289 ? qd.getOrder() == null 290 : order.equals(qd.getOrder())) 291 && (result == null 292 ? qd.getResult() == null 293 : result.equals(qd.getResult())) 294 && (resultClass == null 295 ? qd.getResultClass() == null 296 : resultClass.equals(qd.getResultClass())) 297 && (grouping == null 298 ? qd.getGrouping() == null 299 : grouping.equals(qd.getGrouping())) 300 && (qd.isCollection() 301 ? qd.getCollection().equals(candidateInstances) : true) 302 && (qd.getUnique() == unique) 303 && (qd.withPrefetch() == withPrefetch) 304 && sameRangeCondition(qd) 305 ; 306 } 307 308 private boolean sameRangeCondition(QueryDefinition qd) { 309 if ((indexFirst == 0 && qd.getIndexFirst() != 0) 311 || (indexFirst != 0 && qd.getIndexFirst() == 0)) { 312 return false; 313 } 314 if ((indexLast == Long.MAX_VALUE && qd.getIndexLast() != Long.MAX_VALUE) 316 || (indexLast != Long.MAX_VALUE && qd.getIndexLast() == Long.MAX_VALUE)) { 317 return false; 318 } 319 return true; 320 } 321 322 } 323 | Popular Tags |