|
1 | 1 | /*
|
2 |
| - * Copyright 2013-2015 the original author or authors. |
| 2 | + * Copyright 2013-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
32 | 32 | *
|
33 | 33 | * @author Thomas Darimont
|
34 | 34 | * @author Oliver Gierke
|
| 35 | + * @author Christoph Strobl |
35 | 36 | */
|
36 | 37 | public class JpaSort extends Sort {
|
37 | 38 |
|
@@ -83,6 +84,10 @@ private JpaSort(List<Order> orders, Direction direction, List<Path<?, ?>> paths)
|
83 | 84 | super(combine(orders, direction, paths));
|
84 | 85 | }
|
85 | 86 |
|
| 87 | + private JpaSort(List<Order> orders) { |
| 88 | + super(orders); |
| 89 | + } |
| 90 | + |
86 | 91 | /**
|
87 | 92 | * Returns a new {@link JpaSort} with the given sorting criteria added to the current one.
|
88 | 93 | *
|
@@ -117,6 +122,30 @@ public JpaSort and(Direction direction, Path<?, ?>... paths) {
|
117 | 122 | return new JpaSort(existing, direction, Arrays.asList(paths));
|
118 | 123 | }
|
119 | 124 |
|
| 125 | + /** |
| 126 | + * Returns a new {@link JpaSort} with the given sorting criteria added to the current one. |
| 127 | + * |
| 128 | + * @param direction can be {@literal null}. |
| 129 | + * @param properties must not be {@literal null} or empty. |
| 130 | + * @return |
| 131 | + */ |
| 132 | + public JpaSort andUnsafe(Direction direction, String... properties) { |
| 133 | + |
| 134 | + Assert.notEmpty(properties, "Properties must not be null!"); |
| 135 | + |
| 136 | + List<Order> orders = new ArrayList<Order>(); |
| 137 | + |
| 138 | + for (Order order : this) { |
| 139 | + orders.add(order); |
| 140 | + } |
| 141 | + |
| 142 | + for (String property : properties) { |
| 143 | + orders.add(new JpaOrder(direction, property)); |
| 144 | + } |
| 145 | + |
| 146 | + return new JpaSort(orders, direction, Collections.<Path<?, ?>> emptyList()); |
| 147 | + } |
| 148 | + |
120 | 149 | /**
|
121 | 150 | * Turns the given {@link Attribute}s into {@link Path}s.
|
122 | 151 | *
|
@@ -174,6 +203,51 @@ public static <A extends Attribute<T, S>, T, S> Path<T, S> path(A attribute) {
|
174 | 203 | return new Path<T, S>(Arrays.asList(attribute));
|
175 | 204 | }
|
176 | 205 |
|
| 206 | + /** |
| 207 | + * Creates new unsafe {@link JpaSort} based on given properties. |
| 208 | + * |
| 209 | + * @param properties must not be {@literal null} or empty. |
| 210 | + * @return |
| 211 | + */ |
| 212 | + public static JpaSort unsafe(String... properties) { |
| 213 | + return unsafe(Sort.DEFAULT_DIRECTION, properties); |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Creates new unsafe {@link JpaSort} based on given {@link Direction} and properties. |
| 218 | + * |
| 219 | + * @param direction must not be {@literal null}. |
| 220 | + * @param properties must not be {@literal null} or empty. |
| 221 | + * @return |
| 222 | + */ |
| 223 | + public static JpaSort unsafe(Direction direction, String... properties) { |
| 224 | + |
| 225 | + Assert.notNull(direction, "Direction must not be null!"); |
| 226 | + Assert.notEmpty(properties, "Properties must not be empty!"); |
| 227 | + Assert.noNullElements(properties, "Properties must not contain null values!"); |
| 228 | + |
| 229 | + return unsafe(direction, Arrays.asList(properties)); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Creates new unsafe {@link JpaSort} based on given {@link Direction} and properties. |
| 234 | + * |
| 235 | + * @param direction must not be {@literal null}. |
| 236 | + * @param properties must not be {@literal null} or empty. |
| 237 | + * @return |
| 238 | + */ |
| 239 | + public static JpaSort unsafe(Direction direction, List<String> properties) { |
| 240 | + |
| 241 | + Assert.notEmpty(properties, "Properties must not be empty!"); |
| 242 | + |
| 243 | + List<Order> orders = new ArrayList<Order>(); |
| 244 | + for (String property : properties) { |
| 245 | + orders.add(new JpaOrder(direction, property)); |
| 246 | + } |
| 247 | + |
| 248 | + return new JpaSort(orders); |
| 249 | + } |
| 250 | + |
177 | 251 | /**
|
178 | 252 | * Value object to abstract a collection of {@link Attribute}s.
|
179 | 253 | *
|
@@ -233,4 +307,131 @@ public String toString() {
|
233 | 307 | return builder.length() == 0 ? "" : builder.substring(0, builder.lastIndexOf("."));
|
234 | 308 | }
|
235 | 309 | }
|
| 310 | + |
| 311 | + /** |
| 312 | + * @author Christoph Strobl |
| 313 | + */ |
| 314 | + public static class JpaOrder extends Order { |
| 315 | + |
| 316 | + private final boolean unsafe; |
| 317 | + private final boolean ignoreCase; |
| 318 | + |
| 319 | + /** |
| 320 | + * Creates a new {@link JpaOrder} instance. if order is {@literal null} then order defaults to |
| 321 | + * {@link Sort#DEFAULT_DIRECTION} |
| 322 | + * |
| 323 | + * @param direction can be {@literal null}, will default to {@link Sort#DEFAULT_DIRECTION}. |
| 324 | + * @param property must not be {@literal null}. |
| 325 | + */ |
| 326 | + private JpaOrder(Direction direction, String property) { |
| 327 | + this(direction, property, NullHandling.NATIVE); |
| 328 | + } |
| 329 | + |
| 330 | + /** |
| 331 | + * Creates a new {@link Order} instance. if order is {@literal null} then order defaults to |
| 332 | + * {@link Sort#DEFAULT_DIRECTION}. |
| 333 | + * |
| 334 | + * @param direction can be {@literal null}, will default to {@link Sort#DEFAULT_DIRECTION}. |
| 335 | + * @param property must not be {@literal null}. |
| 336 | + * @param nullHandlingHint can be {@literal null}, will default to {@link NullHandling#NATIVE}. |
| 337 | + */ |
| 338 | + private JpaOrder(Direction direction, String property, NullHandling nullHandlingHint) { |
| 339 | + this(direction, property, nullHandlingHint, false, true); |
| 340 | + } |
| 341 | + |
| 342 | + private JpaOrder(Direction direction, String property, NullHandling nullHandling, boolean ignoreCase, |
| 343 | + boolean unsafe) { |
| 344 | + |
| 345 | + super(direction, property, nullHandling); |
| 346 | + this.ignoreCase = ignoreCase; |
| 347 | + this.unsafe = unsafe; |
| 348 | + } |
| 349 | + |
| 350 | + /* |
| 351 | + * (non-Javadoc) |
| 352 | + * @see org.springframework.data.domain.Sort.Order#with(org.springframework.data.domain.Sort.Direction) |
| 353 | + */ |
| 354 | + @Override |
| 355 | + public JpaOrder with(Direction order) { |
| 356 | + return new JpaOrder(order, getProperty(), getNullHandling(), isIgnoreCase(), this.unsafe); |
| 357 | + } |
| 358 | + |
| 359 | + /* |
| 360 | + * (non-Javadoc) |
| 361 | + * @see org.springframework.data.domain.Sort.Order#with(org.springframework.data.domain.Sort.NullHandling) |
| 362 | + */ |
| 363 | + @Override |
| 364 | + public JpaOrder with(NullHandling nullHandling) { |
| 365 | + return new JpaOrder(getDirection(), getProperty(), nullHandling, isIgnoreCase(), this.unsafe); |
| 366 | + } |
| 367 | + |
| 368 | + /* |
| 369 | + * (non-Javadoc) |
| 370 | + * @see org.springframework.data.domain.Sort.Order#nullsFirst() |
| 371 | + */ |
| 372 | + @Override |
| 373 | + public JpaOrder nullsFirst() { |
| 374 | + return with(NullHandling.NULLS_FIRST); |
| 375 | + } |
| 376 | + |
| 377 | + /* |
| 378 | + * (non-Javadoc) |
| 379 | + * @see org.springframework.data.domain.Sort.Order#nullsLast() |
| 380 | + */ |
| 381 | + @Override |
| 382 | + public JpaOrder nullsLast() { |
| 383 | + return with(NullHandling.NULLS_LAST); |
| 384 | + } |
| 385 | + |
| 386 | + /* |
| 387 | + * (non-Javadoc) |
| 388 | + * @see org.springframework.data.domain.Sort.Order#nullsNative() |
| 389 | + */ |
| 390 | + public JpaOrder nullsNative() { |
| 391 | + return with(NullHandling.NATIVE); |
| 392 | + } |
| 393 | + |
| 394 | + /** |
| 395 | + * Creates new {@link Sort} with potentially unsafe {@link Order} instances. |
| 396 | + * |
| 397 | + * @param properties must not be {@literal null}. |
| 398 | + * @return |
| 399 | + */ |
| 400 | + public Sort withUnsafe(String... properties) { |
| 401 | + |
| 402 | + Assert.notEmpty(properties, "Properties must not be empty!"); |
| 403 | + Assert.noNullElements(properties, "Properties must not contain null values!"); |
| 404 | + |
| 405 | + List<Order> orders = new ArrayList<Order>(); |
| 406 | + for (String property : properties) { |
| 407 | + orders.add(new JpaOrder(getDirection(), property, getNullHandling(), isIgnoreCase(), this.unsafe)); |
| 408 | + } |
| 409 | + return new Sort(orders); |
| 410 | + } |
| 411 | + |
| 412 | + /* |
| 413 | + * (non-Javadoc) |
| 414 | + * @see org.springframework.data.domain.Sort.Order#ignoreCase() |
| 415 | + */ |
| 416 | + @Override |
| 417 | + public JpaOrder ignoreCase() { |
| 418 | + return new JpaOrder(getDirection(), getProperty(), getNullHandling(), true, this.unsafe); |
| 419 | + } |
| 420 | + |
| 421 | + /* |
| 422 | + * (non-Javadoc) |
| 423 | + * @see org.springframework.data.domain.Sort.Order#isIgnoreCase() |
| 424 | + */ |
| 425 | + @Override |
| 426 | + public boolean isIgnoreCase() { |
| 427 | + return super.isIgnoreCase() || ignoreCase; |
| 428 | + } |
| 429 | + |
| 430 | + /** |
| 431 | + * @return true if {@link JpaOrder} created {@link #withUnsafe(String...)}. |
| 432 | + */ |
| 433 | + public boolean isUnsafe() { |
| 434 | + return unsafe; |
| 435 | + } |
| 436 | + } |
236 | 437 | }
|
0 commit comments