|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.search.engine.search.sort.dsl.impl; |
| 6 | + |
| 7 | +import java.util.function.Function; |
| 8 | + |
| 9 | +import org.hibernate.search.engine.search.common.SortMode; |
| 10 | +import org.hibernate.search.engine.search.common.ValueModel; |
| 11 | +import org.hibernate.search.engine.search.predicate.SearchPredicate; |
| 12 | +import org.hibernate.search.engine.search.predicate.dsl.PredicateFinalStep; |
| 13 | +import org.hibernate.search.engine.search.predicate.dsl.SearchPredicateFactory; |
| 14 | +import org.hibernate.search.engine.search.reference.sort.FieldSortFieldReference; |
| 15 | +import org.hibernate.search.engine.search.sort.SearchSort; |
| 16 | +import org.hibernate.search.engine.search.sort.dsl.FieldSortMissingValueBehaviorGenericStep; |
| 17 | +import org.hibernate.search.engine.search.sort.dsl.FieldSortMissingValueBehaviorStep; |
| 18 | +import org.hibernate.search.engine.search.sort.dsl.FieldSortOptionsGenericStep; |
| 19 | +import org.hibernate.search.engine.search.sort.dsl.FieldSortOptionsStep; |
| 20 | +import org.hibernate.search.engine.search.sort.dsl.SortOrder; |
| 21 | +import org.hibernate.search.engine.search.sort.dsl.spi.AbstractSortThenStep; |
| 22 | +import org.hibernate.search.engine.search.sort.dsl.spi.SearchSortDslContext; |
| 23 | +import org.hibernate.search.engine.search.sort.spi.FieldSortBuilder; |
| 24 | +import org.hibernate.search.engine.search.sort.spi.SearchSortIndexScope; |
| 25 | +import org.hibernate.search.engine.search.sort.spi.SortTypeKeys; |
| 26 | + |
| 27 | +public abstract class AbstractFieldSortOptionsGenericStep< |
| 28 | + SR, |
| 29 | + T, |
| 30 | + PDF extends SearchPredicateFactory<SR>, |
| 31 | + S extends AbstractFieldSortOptionsGenericStep<SR, T, PDF, S, N>, |
| 32 | + N extends FieldSortMissingValueBehaviorGenericStep<T, S>> |
| 33 | + extends AbstractSortThenStep<SR> |
| 34 | + implements |
| 35 | + FieldSortOptionsGenericStep<SR, |
| 36 | + T, |
| 37 | + S, |
| 38 | + N, |
| 39 | + PDF> { |
| 40 | + |
| 41 | + |
| 42 | + private final SearchSortDslContext<SR, ?, ? extends PDF> dslContext; |
| 43 | + protected final FieldSortBuilder builder; |
| 44 | + |
| 45 | + public AbstractFieldSortOptionsGenericStep(SearchSortDslContext<SR, ?, ? extends PDF> dslContext, |
| 46 | + String fieldPath) { |
| 47 | + super( dslContext ); |
| 48 | + this.dslContext = dslContext; |
| 49 | + this.builder = dslContext.scope().fieldQueryElement( fieldPath, SortTypeKeys.FIELD ); |
| 50 | + } |
| 51 | + |
| 52 | + public static < |
| 53 | + T, |
| 54 | + SR, |
| 55 | + SC extends SearchSortIndexScope<?>, |
| 56 | + PDF extends SearchPredicateFactory< |
| 57 | + SR>> FieldSortOptionsGenericStep<SR, T, ?, ?, ? extends SearchPredicateFactory<SR>> create( |
| 58 | + SearchSortDslContext<SR, SC, PDF> dslContext, |
| 59 | + FieldSortFieldReference<? super SR, T> fieldReference) { |
| 60 | + return new FieldReferenceFieldSortOptionsStep<>( dslContext, fieldReference ); |
| 61 | + } |
| 62 | + |
| 63 | + public static < |
| 64 | + PDF extends SearchPredicateFactory<SR>, |
| 65 | + SR, |
| 66 | + SC extends SearchSortIndexScope<?>> FieldSortOptionsStep<SR, ?, PDF> create( |
| 67 | + SearchSortDslContext<SR, SC, PDF> dslContext, String fieldPath) { |
| 68 | + return new StringFieldSortOptionsStep<>( dslContext, fieldPath ); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public S order(SortOrder order) { |
| 73 | + builder.order( order ); |
| 74 | + return thisAsS(); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public S mode(SortMode mode) { |
| 79 | + builder.mode( mode ); |
| 80 | + return thisAsS(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public N missing() { |
| 85 | + return thisAsN(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public S filter( |
| 90 | + Function<? super PDF, ? extends PredicateFinalStep> clauseContributor) { |
| 91 | + SearchPredicate predicate = clauseContributor.apply( dslContext.predicateFactory() ).toPredicate(); |
| 92 | + |
| 93 | + return filter( predicate ); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public S filter(SearchPredicate searchPredicate) { |
| 98 | + builder.filter( searchPredicate ); |
| 99 | + return thisAsS(); |
| 100 | + } |
| 101 | + |
| 102 | + public S first() { |
| 103 | + builder.missingFirst(); |
| 104 | + return thisAsS(); |
| 105 | + } |
| 106 | + |
| 107 | + public S last() { |
| 108 | + builder.missingLast(); |
| 109 | + return thisAsS(); |
| 110 | + } |
| 111 | + |
| 112 | + public S highest() { |
| 113 | + builder.missingHighest(); |
| 114 | + return thisAsS(); |
| 115 | + } |
| 116 | + |
| 117 | + public S lowest() { |
| 118 | + builder.missingLowest(); |
| 119 | + return thisAsS(); |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + @Override |
| 124 | + protected SearchSort build() { |
| 125 | + return builder.build(); |
| 126 | + } |
| 127 | + |
| 128 | + protected abstract S thisAsS(); |
| 129 | + |
| 130 | + protected abstract N thisAsN(); |
| 131 | + |
| 132 | + |
| 133 | + private static class FieldReferenceFieldSortOptionsStep<SR, T, PDF extends SearchPredicateFactory<SR>> |
| 134 | + extends |
| 135 | + AbstractFieldSortOptionsGenericStep<SR, |
| 136 | + T, |
| 137 | + PDF, |
| 138 | + FieldReferenceFieldSortOptionsStep<SR, T, PDF>, |
| 139 | + FieldReferenceFieldSortOptionsStep<SR, T, PDF>> |
| 140 | + implements FieldSortMissingValueBehaviorGenericStep<T, FieldReferenceFieldSortOptionsStep<SR, T, PDF>> { |
| 141 | + private final ValueModel valueModel; |
| 142 | + |
| 143 | + public FieldReferenceFieldSortOptionsStep(SearchSortDslContext<SR, ?, ? extends PDF> dslContext, |
| 144 | + FieldSortFieldReference<? super SR, ?> fieldReference) { |
| 145 | + super( dslContext, fieldReference.absolutePath() ); |
| 146 | + this.valueModel = fieldReference.valueModel(); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public FieldReferenceFieldSortOptionsStep<SR, T, PDF> use(T value) { |
| 151 | + builder.missingAs( value, valueModel ); |
| 152 | + return this; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + protected FieldReferenceFieldSortOptionsStep<SR, T, PDF> thisAsS() { |
| 157 | + return this; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + protected FieldReferenceFieldSortOptionsStep<SR, T, PDF> thisAsN() { |
| 162 | + return this; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + private static class StringFieldSortOptionsStep<SR, PDF extends SearchPredicateFactory<SR>> |
| 167 | + extends |
| 168 | + AbstractFieldSortOptionsGenericStep<SR, |
| 169 | + Object, |
| 170 | + PDF, |
| 171 | + StringFieldSortOptionsStep<SR, PDF>, |
| 172 | + FieldSortMissingValueBehaviorStep<StringFieldSortOptionsStep<SR, PDF>>> |
| 173 | + implements FieldSortOptionsStep<SR, StringFieldSortOptionsStep<SR, PDF>, PDF>, |
| 174 | + FieldSortMissingValueBehaviorStep<StringFieldSortOptionsStep<SR, PDF>> { |
| 175 | + |
| 176 | + public StringFieldSortOptionsStep(SearchSortDslContext<SR, ?, ? extends PDF> dslContext, |
| 177 | + String fieldPath) { |
| 178 | + super( dslContext, fieldPath ); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public StringFieldSortOptionsStep<SR, PDF> use(Object value, ValueModel valueModel) { |
| 183 | + builder.missingAs( value, valueModel ); |
| 184 | + return this; |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + protected StringFieldSortOptionsStep<SR, PDF> thisAsS() { |
| 189 | + return this; |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + protected StringFieldSortOptionsStep<SR, PDF> thisAsN() { |
| 194 | + return this; |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments