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