| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CriteriaBuilder |
|
| 1.1304347826086956;1.13 | ||||
| CriteriaBuilder$AndEntry |
|
| 1.1304347826086956;1.13 | ||||
| CriteriaBuilder$Entry |
|
| 1.1304347826086956;1.13 | ||||
| CriteriaBuilder$OrEntry |
|
| 1.1304347826086956;1.13 | ||||
| CriteriaBuilder$SimpleEntry |
|
| 1.1304347826086956;1.13 |
| 1 | /* | |
| 2 | * Copyright 2011 The Kuali Foundation | |
| 3 | * | |
| 4 | * Licensed under the Educational Community License, Version 1.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.opensource.org/licenses/ecl1.php | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package org.kuali.rice.core.api.criteria; | |
| 17 | ||
| 18 | import java.math.BigDecimal; | |
| 19 | import java.math.BigInteger; | |
| 20 | import java.util.ArrayList; | |
| 21 | import java.util.Calendar; | |
| 22 | import java.util.Date; | |
| 23 | import java.util.List; | |
| 24 | import java.util.concurrent.atomic.AtomicInteger; | |
| 25 | import java.util.concurrent.atomic.AtomicLong; | |
| 26 | ||
| 27 | /** | |
| 28 | * Aids in the construction of {@link Criteria} object for use in | |
| 29 | * criteria-based queries. | |
| 30 | * | |
| 31 | * <p>In order to construct an instance of a {@link CriteriaBuilder}, use the | |
| 32 | * {@link #newCriteriaBuilder(Class)} method. The given class type should | |
| 33 | * indicate the type of object against which the criteria will be used to | |
| 34 | * query. This is used to aid in the validation and construction of the | |
| 35 | * property paths which identify the properties of the object which will be | |
| 36 | * evaluated during execution of the query. | |
| 37 | * | |
| 38 | * <p>A propertyPath represents a relative path from the parameterized type "T" | |
| 39 | * and supports dot-notation to access nested properties of the "T" class. | |
| 40 | * | |
| 41 | * <p>While many of the methods used to construct the criteria on this class | |
| 42 | * take a generic {@link Object} value, depending on the particular expression | |
| 43 | * only certain values might be valid. The criteria framework supports four | |
| 44 | * different "classes" of values, as follows: | |
| 45 | * | |
| 46 | * <ol> | |
| 47 | * <li>character data</li> | |
| 48 | * <li>decimals</li> | |
| 49 | * <li>integers</li> | |
| 50 | * <li>date-time</li> | |
| 51 | * </ol> | |
| 52 | * | |
| 53 | * <p>For each of the above, the following object types are supported: | |
| 54 | * | |
| 55 | * <ol> | |
| 56 | * <li><strong>character data</strong> - {@link CharSequence}</li> | |
| 57 | * <li><strong>decimals</strong> - {@link BigDecimal}, {@link Float}, {@link Double}</li> | |
| 58 | * <li><strong>integers</strong> - {@link BigInteger}, {@link Short}, {@link Integer}, {@link Long}, {@link AtomicInteger}, {@link AtomicLong}</li> | |
| 59 | * <li><strong>date-time</strong> - {@link Date}, {@link Calendar}</li> | |
| 60 | * </ol> | |
| 61 | * | |
| 62 | * <p>Once a new instance is obtained, the criteria is built by appending | |
| 63 | * the various expressions together using the methods provided. The criteria | |
| 64 | * produced by the builder will contain an implicit "and" of any of the | |
| 65 | * expressions which are added at the top-level of the builder. Each | |
| 66 | * invocation of one of the methods on the builder will add the related | |
| 67 | * expression to the end of the entries which are tracked internally on this | |
| 68 | * builder. Once the {@link #build()} method is invoked, a criteria | |
| 69 | * object will be constructed and returned which contains all of the defined | |
| 70 | * expressions. | |
| 71 | * | |
| 72 | * <p>It is possible to construct an empty set of criteria by creating a new | |
| 73 | * builder and then simply invoking {@link #build()} on it without adding any | |
| 74 | * expressions. | |
| 75 | * | |
| 76 | * <p>This class is <strong>not</strong> thread safe. Once a new instance is obtained it | |
| 77 | * should only be used by a single thread. The resulting {@link Criteria} | |
| 78 | * which is created when the {@link #build()} method is invoked is both | |
| 79 | * immutable and thread-safe. | |
| 80 | * | |
| 81 | * @param <T> the type of the class to which property paths are relative | |
| 82 | * | |
| 83 | * @see Criteria | |
| 84 | * @see QueryByCriteria | |
| 85 | * | |
| 86 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
| 87 | * | |
| 88 | */ | |
| 89 | public final class CriteriaBuilder<T> { | |
| 90 | ||
| 91 | private final List<Entry> entries; | |
| 92 | ||
| 93 | 0 | private CriteriaBuilder() { |
| 94 | 0 | entries = new ArrayList<Entry>(); |
| 95 | 0 | } |
| 96 | ||
| 97 | /** | |
| 98 | * Constructs a new {@link CriteriaBuilder} targeting criteria that will be | |
| 99 | * used to query for data of the targetClass's type. The builder | |
| 100 | * that is created will produce a {@link Criteria} object which will contain | |
| 101 | * an implicit "and" of all expressions contained within. | |
| 102 | * | |
| 103 | * @param targetClass the target class which represents the root from which | |
| 104 | * property paths are defined | |
| 105 | * | |
| 106 | * @return the newly created {@link CriteriaBuilder} | |
| 107 | * | |
| 108 | * @throws IllegalArgumentException if the targetClass is null | |
| 109 | */ | |
| 110 | public static <T> CriteriaBuilder<T> newCriteriaBuilder(Class<T> targetClass) { | |
| 111 | 0 | if (targetClass == null) { |
| 112 | 0 | throw new IllegalArgumentException("Target class cannot be null."); |
| 113 | } | |
| 114 | 0 | return new CriteriaBuilder<T>(); |
| 115 | } | |
| 116 | ||
| 117 | /** | |
| 118 | * Appends an {@link EqualExpression} to the builder. Defines that the | |
| 119 | * property represented by the given path should be equal to the specified | |
| 120 | * value. | |
| 121 | * | |
| 122 | * <p>Supports the following types of values: | |
| 123 | * | |
| 124 | * <ul> | |
| 125 | * <li>character data</li> | |
| 126 | * <li>decimals</li> | |
| 127 | * <li>integers</li> | |
| 128 | * <li>date-time</li> | |
| 129 | * </ul> | |
| 130 | * | |
| 131 | * @param propertyPath the path to the property which should be evaluated | |
| 132 | * @param value the value to compare with the property value located at the | |
| 133 | * propertyPath | |
| 134 | * | |
| 135 | * @return a reference to "this" CriteriaBuilder, allows for fluent | |
| 136 | * chaining of method calls | |
| 137 | * | |
| 138 | * @throws IllegalArgumentException if the propertyPath is null | |
| 139 | * @throws IllegalArgumentException if the value is null or of an invalid type | |
| 140 | * | |
| 141 | * @see EqualExpression | |
| 142 | */ | |
| 143 | public CriteriaBuilder<T> equal(String propertyPath, Object value) { | |
| 144 | 0 | EqualExpression expression = new EqualExpression(propertyPath, CriteriaSupportUtils.determineCriteriaValue(value)); |
| 145 | 0 | entries.add(new SimpleEntry(expression)); |
| 146 | 0 | return this; |
| 147 | } | |
| 148 | ||
| 149 | /** | |
| 150 | * Appends a {@link NotEqualExpression} to the builder. Defines that the | |
| 151 | * property represented by the given path should <strong>not</strong> be | |
| 152 | * equal to the specified value. | |
| 153 | * | |
| 154 | * <p>Supports the following types of values: | |
| 155 | * | |
| 156 | * <ul> | |
| 157 | * <li>character data</li> | |
| 158 | * <li>decimals</li> | |
| 159 | * <li>integers</li> | |
| 160 | * <li>date-time</li> | |
| 161 | * </ul> | |
| 162 | * | |
| 163 | * @param propertyPath the path to the property which should be evaluated | |
| 164 | * @param value the value to compare with the property value located at the | |
| 165 | * propertyPath | |
| 166 | * | |
| 167 | * @return a reference to "this" CriteriaBuilder, allows for fluent | |
| 168 | * chaining of method calls | |
| 169 | * | |
| 170 | * @throws IllegalArgumentException if the propertyPath is null | |
| 171 | * @throws IllegalArgumentException if the value is null or of an invalid type | |
| 172 | * | |
| 173 | * @see NotEqualExpression | |
| 174 | */ | |
| 175 | public CriteriaBuilder<T> notEqual(String propertyPath, Object value) { | |
| 176 | 0 | NotEqualExpression expression = new NotEqualExpression(propertyPath, CriteriaSupportUtils.determineCriteriaValue(value)); |
| 177 | 0 | entries.add(new SimpleEntry(expression)); |
| 178 | 0 | return this; |
| 179 | } | |
| 180 | ||
| 181 | /** | |
| 182 | * Appends a {@link LikeExpression} to the builder. Defines that the | |
| 183 | * property represented by the given path should match the specified value, | |
| 184 | * but supports the use of wildcards in the given value. | |
| 185 | * | |
| 186 | * <p>The supported wildcards include: | |
| 187 | * | |
| 188 | * <ul> | |
| 189 | * <li><strong>?</strong> - matches on any single character</li> | |
| 190 | * <li><strong>*</strong> - matches any string of any length (including zero length)</li> | |
| 191 | * </ul> | |
| 192 | * | |
| 193 | * <p>Because of this, the like expression only supports character data | |
| 194 | * for the passed-in value. | |
| 195 | * | |
| 196 | * @param propertyPath the path to the property which should be evaluated | |
| 197 | * @param value the value to compare with the property value located at the | |
| 198 | * propertyPath | |
| 199 | * | |
| 200 | * @return a reference to "this" CriteriaBuilder, allows for fluent | |
| 201 | * chaining of method calls | |
| 202 | * | |
| 203 | * @throws IllegalArgumentException if the propertyPath is null | |
| 204 | * @throws IllegalArgumentException if the value is null | |
| 205 | * | |
| 206 | * @see LikeExpression for more information | |
| 207 | */ | |
| 208 | public CriteriaBuilder<T> like(String propertyPath, CharSequence value) { | |
| 209 | 0 | LikeExpression expression = new LikeExpression(propertyPath, CriteriaSupportUtils.determineCriteriaValue(value)); |
| 210 | 0 | entries.add(new SimpleEntry(expression)); |
| 211 | 0 | return this; |
| 212 | } | |
| 213 | ||
| 214 | /** | |
| 215 | * Appends an {@link InExpression} to the builder. Defines that the | |
| 216 | * property represented by the given path should be contained within the | |
| 217 | * specified list of values. | |
| 218 | * | |
| 219 | * <p>Supports any of the valid types of value in the value list, with the | |
| 220 | * restriction that all items in the list of values must be of the same type. | |
| 221 | * | |
| 222 | * @param propertyPath the path to the property which should be evaluated | |
| 223 | * @param value the value to compare with the property value located at the | |
| 224 | * propertyPath | |
| 225 | * | |
| 226 | * @return a reference to "this" CriteriaBuilder, allows for fluent | |
| 227 | * chaining of method calls | |
| 228 | * | |
| 229 | * @throws IllegalArgumentException if the propertyPath is null | |
| 230 | * @throws IllegalArgumentException if the values list is null, empty, | |
| 231 | * contains object of different types, or includes objects of an invalid type | |
| 232 | * | |
| 233 | * @see InExpression for more information | |
| 234 | */ | |
| 235 | public CriteriaBuilder<T> in(String propertyPath, List<?> values) { | |
| 236 | 0 | InExpression expression = new InExpression(propertyPath, CriteriaSupportUtils.determineCriteriaValueList(values)); |
| 237 | 0 | entries.add(new SimpleEntry(expression)); |
| 238 | 0 | return this; |
| 239 | } | |
| 240 | ||
| 241 | /** | |
| 242 | * Appends a {@link NotInExpression} to the builder. Defines that the | |
| 243 | * property represented by the given path should <strong>not</strong> be | |
| 244 | * contained within the specified list of values. | |
| 245 | * | |
| 246 | * <p>Supports any of the valid types of value in the value list, with the | |
| 247 | * restriction that all items in the list of values must be of the same type. | |
| 248 | * | |
| 249 | * @param propertyPath the path to the property which should be evaluated | |
| 250 | * @param value the value to compare with the property value located at the | |
| 251 | * propertyPath | |
| 252 | * | |
| 253 | * @return a reference to "this" CriteriaBuilder, allows for fluent | |
| 254 | * chaining of method calls | |
| 255 | * | |
| 256 | * @throws IllegalArgumentException if the propertyPath is null | |
| 257 | * @throws IllegalArgumentException if the values list is null, empty, | |
| 258 | * contains object of different types, or includes objects of an invalid type | |
| 259 | * | |
| 260 | * @see InExpression | |
| 261 | */ | |
| 262 | public CriteriaBuilder<T> notIn(String propertyPath, List<?> values) { | |
| 263 | 0 | NotInExpression expression = new NotInExpression(propertyPath, CriteriaSupportUtils.determineCriteriaValueList(values)); |
| 264 | 0 | entries.add(new SimpleEntry(expression)); |
| 265 | 0 | return this; |
| 266 | } | |
| 267 | ||
| 268 | /** | |
| 269 | * Appends a {@link GreaterThanExpression} to the builder. Defines that | |
| 270 | * the property represented by the given path should be greater than the | |
| 271 | * specified value. | |
| 272 | * | |
| 273 | * <p>Supports the following types of values: | |
| 274 | * | |
| 275 | * <ul> | |
| 276 | * <li>decimals</li> | |
| 277 | * <li>integers</li> | |
| 278 | * <li>date-time</li> | |
| 279 | * </ul> | |
| 280 | * | |
| 281 | * @param propertyPath the path to the property which should be evaluated | |
| 282 | * @param value the value to compare with the property value located at the | |
| 283 | * propertyPath | |
| 284 | * | |
| 285 | * @return a reference to "this" CriteriaBuilder, allows for fluent | |
| 286 | * chaining of method calls | |
| 287 | * | |
| 288 | * @throws IllegalArgumentException if the propertyPath is null | |
| 289 | * @throws IllegalArgumentException if the value is null or of an invalid type | |
| 290 | * | |
| 291 | * @see GreaterThanExpression | |
| 292 | */ | |
| 293 | public CriteriaBuilder<T> greaterThan(String propertyPath, Object value) { | |
| 294 | 0 | GreaterThanExpression expression = new GreaterThanExpression(propertyPath, CriteriaSupportUtils.determineCriteriaValue(value)); |
| 295 | 0 | entries.add(new SimpleEntry(expression)); |
| 296 | 0 | return this; |
| 297 | } | |
| 298 | ||
| 299 | /** | |
| 300 | * Appends a {@link GreaterThanOrEqualExpression} to the builder. Defines | |
| 301 | * that the property represented by the given path should be greater than | |
| 302 | * or equal to the specified value. | |
| 303 | * | |
| 304 | * <p>Supports the following types of values: | |
| 305 | * | |
| 306 | * <ul> | |
| 307 | * <li>decimals</li> | |
| 308 | * <li>integers</li> | |
| 309 | * <li>date-time</li> | |
| 310 | * </ul> | |
| 311 | * | |
| 312 | * @param propertyPath the path to the property which should be evaluated | |
| 313 | * @param value the value to compare with the property value located at the | |
| 314 | * propertyPath | |
| 315 | * | |
| 316 | * @return a reference to "this" CriteriaBuilder, allows for fluent | |
| 317 | * chaining of method calls | |
| 318 | * | |
| 319 | * @throws IllegalArgumentException if the propertyPath is null | |
| 320 | * @throws IllegalArgumentException if the value is null or of an invalid type | |
| 321 | * | |
| 322 | * @see GreaterThanOrEqualExpression | |
| 323 | */ | |
| 324 | public CriteriaBuilder<T> greaterThanOrEqual(String propertyPath, Object value) { | |
| 325 | 0 | GreaterThanOrEqualExpression expression = new GreaterThanOrEqualExpression(propertyPath, CriteriaSupportUtils.determineCriteriaValue(value)); |
| 326 | 0 | entries.add(new SimpleEntry(expression)); |
| 327 | 0 | return this; |
| 328 | } | |
| 329 | ||
| 330 | /** | |
| 331 | * Appends a {@link LessThanExpression} to the builder. Defines that | |
| 332 | * the property represented by the given path should be less than the | |
| 333 | * specified value. | |
| 334 | * | |
| 335 | * <p>Supports the following types of values: | |
| 336 | * | |
| 337 | * <ul> | |
| 338 | * <li>decimals</li> | |
| 339 | * <li>integers</li> | |
| 340 | * <li>date-time</li> | |
| 341 | * </ul> | |
| 342 | * | |
| 343 | * @param propertyPath the path to the property which should be evaluated | |
| 344 | * @param value the value to compare with the property value located at the | |
| 345 | * propertyPath | |
| 346 | * | |
| 347 | * @return a reference to "this" CriteriaBuilder, allows for fluent | |
| 348 | * chaining of method calls | |
| 349 | * | |
| 350 | * @throws IllegalArgumentException if the propertyPath is null | |
| 351 | * @throws IllegalArgumentException if the value is null or of an invalid type | |
| 352 | * | |
| 353 | * @see LessThanExpression | |
| 354 | */ | |
| 355 | public CriteriaBuilder<T> lessThan(String propertyPath, Object value) { | |
| 356 | 0 | LessThanExpression expression = new LessThanExpression(propertyPath, CriteriaSupportUtils.determineCriteriaValue(value)); |
| 357 | 0 | entries.add(new SimpleEntry(expression)); |
| 358 | 0 | return this; |
| 359 | } | |
| 360 | ||
| 361 | /** | |
| 362 | * Appends a {@link LessThanOrEqualExpression} to the builder. Defines | |
| 363 | * that the property represented by the given path should be less than | |
| 364 | * or equal to the specified value. | |
| 365 | * | |
| 366 | * <p>Supports the following types of values: | |
| 367 | * | |
| 368 | * <ul> | |
| 369 | * <li>decimals</li> | |
| 370 | * <li>integers</li> | |
| 371 | * <li>date-time</li> | |
| 372 | * </ul> | |
| 373 | * | |
| 374 | * @param propertyPath the path to the property which should be evaluated | |
| 375 | * @param value the value to compare with the property value located at the | |
| 376 | * propertyPath | |
| 377 | * | |
| 378 | * @return a reference to "this" CriteriaBuilder, allows for fluent | |
| 379 | * chaining of method calls | |
| 380 | * | |
| 381 | * @throws IllegalArgumentException if the propertyPath is null | |
| 382 | * @throws IllegalArgumentException if the value is null or of an invalid type | |
| 383 | * | |
| 384 | * @see LessThanOrEqualExpression | |
| 385 | */ | |
| 386 | public CriteriaBuilder<T> lessThanOrEqual(String propertyPath, Object value) { | |
| 387 | 0 | LessThanOrEqualExpression expression = new LessThanOrEqualExpression(propertyPath, CriteriaSupportUtils.determineCriteriaValue(value)); |
| 388 | 0 | entries.add(new SimpleEntry(expression)); |
| 389 | 0 | return this; |
| 390 | } | |
| 391 | ||
| 392 | /** | |
| 393 | * Appends a {@link NullExpression} to the builder. Defines that the | |
| 394 | * property represented by the given path should be null. | |
| 395 | * | |
| 396 | * @param propertyPath the path to the property which should be evaluated | |
| 397 | * | |
| 398 | * @return a reference to "this" CriteriaBuilder, allows for fluent | |
| 399 | * chaining of method calls | |
| 400 | * | |
| 401 | * @throws IllegalArgumentException if the propertyPath is null | |
| 402 | * | |
| 403 | * @see NullExpression | |
| 404 | */ | |
| 405 | public CriteriaBuilder<T> isNull(String propertyPath) { | |
| 406 | 0 | NullExpression expression = new NullExpression(propertyPath); |
| 407 | 0 | entries.add(new SimpleEntry(expression)); |
| 408 | 0 | return this; |
| 409 | } | |
| 410 | ||
| 411 | /** | |
| 412 | * Appends a {@link NotNullExpression} to the builder. Defines that the | |
| 413 | * property represented by the given path should <strong>not</strong> be null. | |
| 414 | * | |
| 415 | * @param propertyPath the path to the property which should be evaluated | |
| 416 | * | |
| 417 | * @return a reference to "this" CriteriaBuilder, allows for fluent | |
| 418 | * chaining of method calls | |
| 419 | * | |
| 420 | * @throws IllegalArgumentException if the propertyPath is null | |
| 421 | * | |
| 422 | * @see NotNullExpression | |
| 423 | */ | |
| 424 | public CriteriaBuilder<T> isNotNull(String propertyPath) { | |
| 425 | 0 | NotNullExpression expression = new NotNullExpression(propertyPath); |
| 426 | 0 | entries.add(new SimpleEntry(expression)); |
| 427 | 0 | return this; |
| 428 | } | |
| 429 | ||
| 430 | /** | |
| 431 | * Appends an {@link AndExpression} to the builder and returns a reference | |
| 432 | * to a {@link CriteriaBuilder} that can be used to define the | |
| 433 | * expressions contained within the "and" expression. | |
| 434 | * | |
| 435 | * <p>An "and" expression will evaluate the truth value of all of it's | |
| 436 | * internal expressions and, if all of them evaluate to true, then | |
| 437 | * the and expression itself should evaluate to true. | |
| 438 | * | |
| 439 | * <p>It's important to note that the {@link CriteriaBuilder} returned | |
| 440 | * by the {@link #and()} method is not the same as the builder on which | |
| 441 | * it was invoked. Instead this is a new builder that can be used to | |
| 442 | * construct the contents of the "and". You should never have to manually | |
| 443 | * invoke {@link #build()} on the builder returned from this method | |
| 444 | * because it will be automatically invoked when the top-level builder's | |
| 445 | * {@link #build()} method is invoked. | |
| 446 | * | |
| 447 | * @return a reference to a {@link CriteriaBuilder} that can be used to | |
| 448 | * construct the contents of the and expression | |
| 449 | * | |
| 450 | * @see AndExpression | |
| 451 | */ | |
| 452 | public CriteriaBuilder<T> and() { | |
| 453 | 0 | CriteriaBuilder<T> criteriaBuilder = new CriteriaBuilder<T>(); |
| 454 | 0 | entries.add(new AndEntry<T>(criteriaBuilder)); |
| 455 | 0 | return criteriaBuilder; |
| 456 | } | |
| 457 | ||
| 458 | /** | |
| 459 | * Appends an {@link OrExpression} to the builder and returns a reference | |
| 460 | * to a {@link CriteriaBuilder} that can be used to define the | |
| 461 | * expressions contained within the "or" expression. | |
| 462 | * | |
| 463 | * <p>An "or" expression will evaluate the truth value of all of it's | |
| 464 | * internal expressions and, if any one of them evaluate to true, then | |
| 465 | * the and expression itself should evaluate to true. If all expressions | |
| 466 | * contained within the "or" evaluate to false, then the or iself will | |
| 467 | * evaluate to false. | |
| 468 | * | |
| 469 | * <p>It's important to note that the {@link CriteriaBuilder} returned | |
| 470 | * by the {@link #or()} method is not the same as the builder on which | |
| 471 | * it was invoked. Instead this is a new builder that can be used to | |
| 472 | * construct the contents of the "or". You should never have to manually | |
| 473 | * invoke {@link #build()} on the builder returned from this method | |
| 474 | * because it will be automatically invoked when the top-level builder's | |
| 475 | * {@link #build()} method is invoked. | |
| 476 | * | |
| 477 | * @return a reference to a {@link CriteriaBuilder} that can be used to | |
| 478 | * construct the contents of the or expression | |
| 479 | * | |
| 480 | * @see OrExpression | |
| 481 | */ | |
| 482 | public CriteriaBuilder<T> or() { | |
| 483 | 0 | CriteriaBuilder<T> criteriaBuilder = new CriteriaBuilder<T>(); |
| 484 | 0 | entries.add(new OrEntry<T>(criteriaBuilder)); |
| 485 | 0 | return criteriaBuilder; |
| 486 | } | |
| 487 | ||
| 488 | /** | |
| 489 | * Builds an immutable {@link Criteria} object which contains instantiated | |
| 490 | * forms of all the expressions that were defined during interaction with | |
| 491 | * this builder prior to the invocation of this method. | |
| 492 | * | |
| 493 | * <p>The resulting criteria will represent an implicit "and" of all | |
| 494 | * expressions included inside of it at the top level. When invoked, this | |
| 495 | * method will recursively invoke any builders created by the {@link #and()} | |
| 496 | * and {@link #or()} and use the expressions on the resulting criteria to | |
| 497 | * form the top-level criteria. | |
| 498 | * | |
| 499 | * <p>In general practice, this method should only be invoked once, but if | |
| 500 | * invoked more than once against the same builder state then it will | |
| 501 | * return an equivalent criteria object. Additionally, this builder is | |
| 502 | * still active after this method has been invoked, so additional | |
| 503 | * expressions could be added and a new criteria built at a later time. | |
| 504 | * | |
| 505 | * @return a Criteria object built from the expressions defined by this builder | |
| 506 | */ | |
| 507 | public Criteria build() { | |
| 508 | 0 | List<Expression> builtExpressions = new ArrayList<Expression>(); |
| 509 | 0 | for (Entry entry : entries) { |
| 510 | 0 | builtExpressions.add(entry.buildExpression()); |
| 511 | } | |
| 512 | 0 | return new Criteria(builtExpressions); |
| 513 | } | |
| 514 | ||
| 515 | private static interface Entry { | |
| 516 | ||
| 517 | public Expression buildExpression(); | |
| 518 | ||
| 519 | } | |
| 520 | ||
| 521 | private static final class SimpleEntry implements Entry { | |
| 522 | ||
| 523 | private Expression expression; | |
| 524 | ||
| 525 | 0 | SimpleEntry(Expression expression) { |
| 526 | 0 | this.expression = expression; |
| 527 | 0 | } |
| 528 | ||
| 529 | public Expression buildExpression() { | |
| 530 | 0 | return expression; |
| 531 | } | |
| 532 | ||
| 533 | } | |
| 534 | ||
| 535 | private static final class AndEntry<T> implements Entry { | |
| 536 | ||
| 537 | private CriteriaBuilder<T> criteriaBuilder; | |
| 538 | ||
| 539 | 0 | AndEntry(CriteriaBuilder<T> criteriaBuilder) { |
| 540 | 0 | this.criteriaBuilder = criteriaBuilder; |
| 541 | 0 | } |
| 542 | ||
| 543 | public Expression buildExpression() { | |
| 544 | 0 | Criteria criteria = criteriaBuilder.build(); |
| 545 | 0 | return new AndExpression(criteria.getExpressions()); |
| 546 | } | |
| 547 | ||
| 548 | } | |
| 549 | ||
| 550 | private static final class OrEntry<T> implements Entry { | |
| 551 | ||
| 552 | private CriteriaBuilder<T> criteriaBuilder; | |
| 553 | ||
| 554 | 0 | OrEntry(CriteriaBuilder<T> criteriaBuilder) { |
| 555 | 0 | this.criteriaBuilder = criteriaBuilder; |
| 556 | 0 | } |
| 557 | ||
| 558 | public Expression buildExpression() { | |
| 559 | 0 | Criteria criteria = criteriaBuilder.build(); |
| 560 | 0 | return new OrExpression(criteria.getExpressions()); |
| 561 | } | |
| 562 | ||
| 563 | } | |
| 564 | ||
| 565 | } |