|
| 1 | +/* |
| 2 | + * Copyright 2021 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.elasticsearch.core.query; |
| 17 | + |
| 18 | +import org.springframework.data.domain.Sort; |
| 19 | +import org.springframework.lang.Nullable; |
| 20 | + |
| 21 | +/** |
| 22 | + * Extends the {@link Sort.Order} with properties that can be set on Elasticsearch order options. |
| 23 | + * |
| 24 | + * @author Peter-Josef Meisch |
| 25 | + * @since 4.3 |
| 26 | + */ |
| 27 | +public class Order extends Sort.Order { |
| 28 | + |
| 29 | + public static final Mode DEFAULT_MODE = Mode.min; |
| 30 | + public static final Sort.NullHandling DEFAULT_NULL_HANDLING = Sort.NullHandling.NATIVE; |
| 31 | + |
| 32 | + protected final Mode mode; |
| 33 | + @Nullable protected final String unmappedType; |
| 34 | + |
| 35 | + public Order(Sort.Direction direction, String property) { |
| 36 | + this(direction, property, DEFAULT_MODE, null); |
| 37 | + } |
| 38 | + |
| 39 | + public Order(Sort.Direction direction, String property, Mode mode) { |
| 40 | + this(direction, property, DEFAULT_NULL_HANDLING, mode, null); |
| 41 | + } |
| 42 | + |
| 43 | + public Order(Sort.Direction direction, String property, @Nullable String unmappedType) { |
| 44 | + this(direction, property, DEFAULT_NULL_HANDLING, DEFAULT_MODE, unmappedType); |
| 45 | + } |
| 46 | + |
| 47 | + public Order(Sort.Direction direction, String property, Mode mode, @Nullable String unmappedType) { |
| 48 | + this(direction, property, DEFAULT_NULL_HANDLING, mode, unmappedType); |
| 49 | + } |
| 50 | + |
| 51 | + public Order(Sort.Direction direction, String property, Sort.NullHandling nullHandlingHint) { |
| 52 | + this(direction, property, nullHandlingHint, DEFAULT_MODE, null); |
| 53 | + } |
| 54 | + |
| 55 | + public Order(Sort.Direction direction, String property, Sort.NullHandling nullHandlingHint, Mode mode) { |
| 56 | + this(direction, property, nullHandlingHint, mode, null); |
| 57 | + } |
| 58 | + |
| 59 | + public Order(Sort.Direction direction, String property, Sort.NullHandling nullHandlingHint, |
| 60 | + @Nullable String unmappedType) { |
| 61 | + this(direction, property, nullHandlingHint, DEFAULT_MODE, unmappedType); |
| 62 | + } |
| 63 | + |
| 64 | + public Order(Sort.Direction direction, String property, Sort.NullHandling nullHandlingHint, Mode mode, |
| 65 | + @Nullable String unmappedType) { |
| 66 | + super(direction, property, nullHandlingHint); |
| 67 | + this.mode = mode; |
| 68 | + this.unmappedType = unmappedType; |
| 69 | + } |
| 70 | + |
| 71 | + @Nullable |
| 72 | + public String getUnmappedType() { |
| 73 | + return unmappedType; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Sort.Order with(Sort.Direction direction) { |
| 78 | + return new Order(direction, getProperty(), getNullHandling(), mode, unmappedType); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Sort.Order withProperty(String property) { |
| 83 | + return new Order(getDirection(), property, getNullHandling(), mode, unmappedType); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Sort.Order with(Sort.NullHandling nullHandling) { |
| 88 | + return new Order(getDirection(), getProperty(), nullHandling, getMode(), unmappedType); |
| 89 | + } |
| 90 | + |
| 91 | + public Order withUnmappedType(@Nullable String unmappedType) { |
| 92 | + return new Order(getDirection(), getProperty(), getNullHandling(), getMode(), unmappedType); |
| 93 | + } |
| 94 | + |
| 95 | + public Order with(Mode mode) { |
| 96 | + return new Order(getDirection(), getProperty(), getNullHandling(), mode, unmappedType); |
| 97 | + } |
| 98 | + |
| 99 | + public Mode getMode() { |
| 100 | + return mode; |
| 101 | + } |
| 102 | + |
| 103 | + public enum Mode { |
| 104 | + min, max, median, avg |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments