|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.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 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 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.springframework.data.keyvalue.core; |
| 17 | + |
| 18 | +import java.util.Comparator; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.springframework.data.mapping.PropertyPath; |
| 23 | +import org.springframework.data.util.Lazy; |
| 24 | +import org.springframework.util.comparator.NullSafeComparator; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Christoph Strobl |
| 28 | + * @since 3.1.10 |
| 29 | + */ |
| 30 | +public class PropertyPathComparator<T> implements Comparator<T> { |
| 31 | + |
| 32 | + private final String path; |
| 33 | + |
| 34 | + private boolean asc = true; |
| 35 | + private boolean nullsFirst = true; |
| 36 | + |
| 37 | + private final Map<Class<?>, PropertyPath> pathCache = new HashMap<>(2); |
| 38 | + private Lazy<Comparator<Object>> comparator = Lazy |
| 39 | + .of(() -> new NullSafeComparator(Comparator.naturalOrder(), this.nullsFirst)); |
| 40 | + |
| 41 | + public PropertyPathComparator(String path) { |
| 42 | + this.path = path; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public int compare(T o1, T o2) { |
| 47 | + |
| 48 | + if (o1 == null && o2 == null) { |
| 49 | + return 0; |
| 50 | + } |
| 51 | + if (o1 == null) { |
| 52 | + return nullsFirst ? 1 : -1; |
| 53 | + } |
| 54 | + if (o2 == null) { |
| 55 | + return nullsFirst ? 1 : -1; |
| 56 | + } |
| 57 | + |
| 58 | + PropertyPath propertyPath = pathCache.computeIfAbsent(o1.getClass(), it -> PropertyPath.from(path, it)); |
| 59 | + Object value1 = new SimplePropertyPathAccessor<>(o1).getValue(propertyPath); |
| 60 | + Object value2 = new SimplePropertyPathAccessor<>(o2).getValue(propertyPath); |
| 61 | + |
| 62 | + return comparator.get().compare(value1, value2) * (asc ? 1 : -1); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Sort {@literal ascending}. |
| 67 | + * |
| 68 | + * @return |
| 69 | + */ |
| 70 | + public PropertyPathComparator<T> asc() { |
| 71 | + this.asc = true; |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Sort {@literal descending}. |
| 77 | + * |
| 78 | + * @return |
| 79 | + */ |
| 80 | + public PropertyPathComparator<T> desc() { |
| 81 | + this.asc = false; |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Sort {@literal null} values first. |
| 87 | + * |
| 88 | + * @return |
| 89 | + */ |
| 90 | + public PropertyPathComparator<T> nullsFirst() { |
| 91 | + this.nullsFirst = true; |
| 92 | + return this; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Sort {@literal null} values last. |
| 97 | + * |
| 98 | + * @return |
| 99 | + */ |
| 100 | + public PropertyPathComparator<T> nullsLast() { |
| 101 | + this.nullsFirst = false; |
| 102 | + return this; |
| 103 | + } |
| 104 | +} |
0 commit comments