From 43b838cc3dd335509cd082bab6c40e5d20f9d583 Mon Sep 17 00:00:00 2001 From: marko-bekhta Date: Mon, 20 May 2024 12:30:30 +0200 Subject: [PATCH] HSEARCH-3319 Use single impl for field sort options --- .../AbstractFieldSortOptionsGenericStep.java | 197 ++++++++++++++++++ .../impl/FieldSortOptionsGenericStepImpl.java | 112 ---------- .../dsl/impl/FieldSortOptionsStepImpl.java | 103 --------- .../dsl/spi/AbstractSearchSortFactory.java | 9 +- 4 files changed, 201 insertions(+), 220 deletions(-) create mode 100644 engine/src/main/java/org/hibernate/search/engine/search/sort/dsl/impl/AbstractFieldSortOptionsGenericStep.java delete mode 100644 engine/src/main/java/org/hibernate/search/engine/search/sort/dsl/impl/FieldSortOptionsGenericStepImpl.java delete mode 100644 engine/src/main/java/org/hibernate/search/engine/search/sort/dsl/impl/FieldSortOptionsStepImpl.java diff --git a/engine/src/main/java/org/hibernate/search/engine/search/sort/dsl/impl/AbstractFieldSortOptionsGenericStep.java b/engine/src/main/java/org/hibernate/search/engine/search/sort/dsl/impl/AbstractFieldSortOptionsGenericStep.java new file mode 100644 index 00000000000..8146580e184 --- /dev/null +++ b/engine/src/main/java/org/hibernate/search/engine/search/sort/dsl/impl/AbstractFieldSortOptionsGenericStep.java @@ -0,0 +1,197 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.search.engine.search.sort.dsl.impl; + +import java.util.function.Function; + +import org.hibernate.search.engine.search.common.SortMode; +import org.hibernate.search.engine.search.common.ValueModel; +import org.hibernate.search.engine.search.predicate.SearchPredicate; +import org.hibernate.search.engine.search.predicate.dsl.PredicateFinalStep; +import org.hibernate.search.engine.search.predicate.dsl.SearchPredicateFactory; +import org.hibernate.search.engine.search.reference.sort.FieldSortFieldReference; +import org.hibernate.search.engine.search.sort.SearchSort; +import org.hibernate.search.engine.search.sort.dsl.FieldSortMissingValueBehaviorGenericStep; +import org.hibernate.search.engine.search.sort.dsl.FieldSortMissingValueBehaviorStep; +import org.hibernate.search.engine.search.sort.dsl.FieldSortOptionsGenericStep; +import org.hibernate.search.engine.search.sort.dsl.FieldSortOptionsStep; +import org.hibernate.search.engine.search.sort.dsl.SortOrder; +import org.hibernate.search.engine.search.sort.dsl.spi.AbstractSortThenStep; +import org.hibernate.search.engine.search.sort.dsl.spi.SearchSortDslContext; +import org.hibernate.search.engine.search.sort.spi.FieldSortBuilder; +import org.hibernate.search.engine.search.sort.spi.SearchSortIndexScope; +import org.hibernate.search.engine.search.sort.spi.SortTypeKeys; + +public abstract class AbstractFieldSortOptionsGenericStep< + SR, + T, + PDF extends SearchPredicateFactory, + S extends AbstractFieldSortOptionsGenericStep, + N extends FieldSortMissingValueBehaviorGenericStep> + extends AbstractSortThenStep + implements + FieldSortOptionsGenericStep { + + + private final SearchSortDslContext dslContext; + protected final FieldSortBuilder builder; + + public AbstractFieldSortOptionsGenericStep(SearchSortDslContext dslContext, + String fieldPath) { + super( dslContext ); + this.dslContext = dslContext; + this.builder = dslContext.scope().fieldQueryElement( fieldPath, SortTypeKeys.FIELD ); + } + + public static < + T, + SR, + SC extends SearchSortIndexScope, + PDF extends SearchPredicateFactory< + SR>> FieldSortOptionsGenericStep> create( + SearchSortDslContext dslContext, + FieldSortFieldReference fieldReference) { + return new FieldReferenceFieldSortOptionsStep<>( dslContext, fieldReference ); + } + + public static < + PDF extends SearchPredicateFactory, + SR, + SC extends SearchSortIndexScope> FieldSortOptionsStep create( + SearchSortDslContext dslContext, String fieldPath) { + return new StringFieldSortOptionsStep<>( dslContext, fieldPath ); + } + + @Override + public S order(SortOrder order) { + builder.order( order ); + return thisAsS(); + } + + @Override + public S mode(SortMode mode) { + builder.mode( mode ); + return thisAsS(); + } + + @Override + public N missing() { + return thisAsN(); + } + + @Override + public S filter( + Function clauseContributor) { + SearchPredicate predicate = clauseContributor.apply( dslContext.predicateFactory() ).toPredicate(); + + return filter( predicate ); + } + + @Override + public S filter(SearchPredicate searchPredicate) { + builder.filter( searchPredicate ); + return thisAsS(); + } + + public S first() { + builder.missingFirst(); + return thisAsS(); + } + + public S last() { + builder.missingLast(); + return thisAsS(); + } + + public S highest() { + builder.missingHighest(); + return thisAsS(); + } + + public S lowest() { + builder.missingLowest(); + return thisAsS(); + } + + + @Override + protected SearchSort build() { + return builder.build(); + } + + protected abstract S thisAsS(); + + protected abstract N thisAsN(); + + + private static class FieldReferenceFieldSortOptionsStep> + extends + AbstractFieldSortOptionsGenericStep, + FieldReferenceFieldSortOptionsStep> + implements FieldSortMissingValueBehaviorGenericStep> { + private final ValueModel valueModel; + + public FieldReferenceFieldSortOptionsStep(SearchSortDslContext dslContext, + FieldSortFieldReference fieldReference) { + super( dslContext, fieldReference.absolutePath() ); + this.valueModel = fieldReference.valueModel(); + } + + @Override + public FieldReferenceFieldSortOptionsStep use(T value) { + builder.missingAs( value, valueModel ); + return this; + } + + @Override + protected FieldReferenceFieldSortOptionsStep thisAsS() { + return this; + } + + @Override + protected FieldReferenceFieldSortOptionsStep thisAsN() { + return this; + } + } + + private static class StringFieldSortOptionsStep> + extends + AbstractFieldSortOptionsGenericStep, + FieldSortMissingValueBehaviorStep>> + implements FieldSortOptionsStep, PDF>, + FieldSortMissingValueBehaviorStep> { + + public StringFieldSortOptionsStep(SearchSortDslContext dslContext, + String fieldPath) { + super( dslContext, fieldPath ); + } + + @Override + public StringFieldSortOptionsStep use(Object value, ValueModel valueModel) { + builder.missingAs( value, valueModel ); + return this; + } + + @Override + protected StringFieldSortOptionsStep thisAsS() { + return this; + } + + @Override + protected StringFieldSortOptionsStep thisAsN() { + return this; + } + } +} diff --git a/engine/src/main/java/org/hibernate/search/engine/search/sort/dsl/impl/FieldSortOptionsGenericStepImpl.java b/engine/src/main/java/org/hibernate/search/engine/search/sort/dsl/impl/FieldSortOptionsGenericStepImpl.java deleted file mode 100644 index d09455472c6..00000000000 --- a/engine/src/main/java/org/hibernate/search/engine/search/sort/dsl/impl/FieldSortOptionsGenericStepImpl.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.search.engine.search.sort.dsl.impl; - -import java.util.function.Function; - -import org.hibernate.search.engine.search.common.SortMode; -import org.hibernate.search.engine.search.common.ValueModel; -import org.hibernate.search.engine.search.predicate.SearchPredicate; -import org.hibernate.search.engine.search.predicate.dsl.PredicateFinalStep; -import org.hibernate.search.engine.search.predicate.dsl.SearchPredicateFactory; -import org.hibernate.search.engine.search.reference.sort.FieldSortFieldReference; -import org.hibernate.search.engine.search.sort.SearchSort; -import org.hibernate.search.engine.search.sort.dsl.FieldSortMissingValueBehaviorGenericStep; -import org.hibernate.search.engine.search.sort.dsl.FieldSortOptionsGenericStep; -import org.hibernate.search.engine.search.sort.dsl.SortOrder; -import org.hibernate.search.engine.search.sort.dsl.spi.AbstractSortThenStep; -import org.hibernate.search.engine.search.sort.dsl.spi.SearchSortDslContext; -import org.hibernate.search.engine.search.sort.spi.FieldSortBuilder; -import org.hibernate.search.engine.search.sort.spi.SortTypeKeys; - -public class FieldSortOptionsGenericStepImpl> - extends AbstractSortThenStep - implements - FieldSortOptionsGenericStep, - FieldSortOptionsGenericStepImpl, - PDF>, - FieldSortMissingValueBehaviorGenericStep> { - - private final SearchSortDslContext dslContext; - private final FieldSortBuilder builder; - private final ValueModel valueModel; - - public FieldSortOptionsGenericStepImpl(SearchSortDslContext dslContext, - FieldSortFieldReference fieldReference) { - super( dslContext ); - this.dslContext = dslContext; - this.builder = dslContext.scope().fieldQueryElement( fieldReference.absolutePath(), SortTypeKeys.FIELD ); - this.valueModel = fieldReference.valueModel(); - } - - @Override - public FieldSortOptionsGenericStepImpl order(SortOrder order) { - builder.order( order ); - return this; - } - - @Override - public FieldSortOptionsGenericStepImpl mode(SortMode mode) { - builder.mode( mode ); - return this; - } - - @Override - public FieldSortOptionsGenericStepImpl missing() { - return this; - } - - @Override - public FieldSortOptionsGenericStepImpl filter( - Function clauseContributor) { - SearchPredicate predicate = clauseContributor.apply( dslContext.predicateFactory() ).toPredicate(); - - return filter( predicate ); - } - - @Override - public FieldSortOptionsGenericStepImpl filter(SearchPredicate searchPredicate) { - builder.filter( searchPredicate ); - return this; - } - - @Override - public FieldSortOptionsGenericStepImpl first() { - builder.missingFirst(); - return this; - } - - @Override - public FieldSortOptionsGenericStepImpl last() { - builder.missingLast(); - return this; - } - - @Override - public FieldSortOptionsGenericStepImpl highest() { - builder.missingHighest(); - return this; - } - - @Override - public FieldSortOptionsGenericStepImpl lowest() { - builder.missingLowest(); - return this; - } - - @Override - public FieldSortOptionsGenericStepImpl use(T value) { - builder.missingAs( value, valueModel ); - return this; - } - - @Override - protected SearchSort build() { - return builder.build(); - } - -} diff --git a/engine/src/main/java/org/hibernate/search/engine/search/sort/dsl/impl/FieldSortOptionsStepImpl.java b/engine/src/main/java/org/hibernate/search/engine/search/sort/dsl/impl/FieldSortOptionsStepImpl.java deleted file mode 100644 index b903606df8a..00000000000 --- a/engine/src/main/java/org/hibernate/search/engine/search/sort/dsl/impl/FieldSortOptionsStepImpl.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.search.engine.search.sort.dsl.impl; - -import java.util.function.Function; - -import org.hibernate.search.engine.search.common.SortMode; -import org.hibernate.search.engine.search.common.ValueModel; -import org.hibernate.search.engine.search.predicate.SearchPredicate; -import org.hibernate.search.engine.search.predicate.dsl.PredicateFinalStep; -import org.hibernate.search.engine.search.predicate.dsl.SearchPredicateFactory; -import org.hibernate.search.engine.search.sort.SearchSort; -import org.hibernate.search.engine.search.sort.dsl.FieldSortMissingValueBehaviorStep; -import org.hibernate.search.engine.search.sort.dsl.FieldSortOptionsStep; -import org.hibernate.search.engine.search.sort.dsl.SortOrder; -import org.hibernate.search.engine.search.sort.dsl.spi.AbstractSortThenStep; -import org.hibernate.search.engine.search.sort.dsl.spi.SearchSortDslContext; -import org.hibernate.search.engine.search.sort.spi.FieldSortBuilder; -import org.hibernate.search.engine.search.sort.spi.SortTypeKeys; - -public class FieldSortOptionsStepImpl> - extends AbstractSortThenStep - implements FieldSortOptionsStep, PDF>, - FieldSortMissingValueBehaviorStep> { - - private final SearchSortDslContext dslContext; - private final FieldSortBuilder builder; - - public FieldSortOptionsStepImpl(SearchSortDslContext dslContext, String fieldPath) { - super( dslContext ); - this.dslContext = dslContext; - this.builder = dslContext.scope().fieldQueryElement( fieldPath, SortTypeKeys.FIELD ); - } - - @Override - public FieldSortOptionsStepImpl order(SortOrder order) { - builder.order( order ); - return this; - } - - @Override - public FieldSortOptionsStepImpl mode(SortMode mode) { - builder.mode( mode ); - return this; - } - - @Override - public FieldSortMissingValueBehaviorStep> missing() { - return this; - } - - @Override - public FieldSortOptionsStepImpl filter( - Function clauseContributor) { - SearchPredicate predicate = clauseContributor.apply( dslContext.predicateFactory() ).toPredicate(); - - return filter( predicate ); - } - - @Override - public FieldSortOptionsStepImpl filter(SearchPredicate searchPredicate) { - builder.filter( searchPredicate ); - return this; - } - - @Override - public FieldSortOptionsStepImpl first() { - builder.missingFirst(); - return this; - } - - @Override - public FieldSortOptionsStepImpl last() { - builder.missingLast(); - return this; - } - - @Override - public FieldSortOptionsStepImpl highest() { - builder.missingHighest(); - return this; - } - - @Override - public FieldSortOptionsStepImpl lowest() { - builder.missingLowest(); - return this; - } - - @Override - public FieldSortOptionsStepImpl use(Object value, ValueModel valueModel) { - builder.missingAs( value, valueModel ); - return this; - } - - @Override - protected SearchSort build() { - return builder.build(); - } - -} diff --git a/engine/src/main/java/org/hibernate/search/engine/search/sort/dsl/spi/AbstractSearchSortFactory.java b/engine/src/main/java/org/hibernate/search/engine/search/sort/dsl/spi/AbstractSearchSortFactory.java index 46a191bbfdf..3a5d93209b3 100644 --- a/engine/src/main/java/org/hibernate/search/engine/search/sort/dsl/spi/AbstractSearchSortFactory.java +++ b/engine/src/main/java/org/hibernate/search/engine/search/sort/dsl/spi/AbstractSearchSortFactory.java @@ -22,10 +22,9 @@ import org.hibernate.search.engine.search.sort.dsl.SearchSortFactoryExtensionIfSupportedStep; import org.hibernate.search.engine.search.sort.dsl.SortFinalStep; import org.hibernate.search.engine.search.sort.dsl.SortThenStep; +import org.hibernate.search.engine.search.sort.dsl.impl.AbstractFieldSortOptionsGenericStep; import org.hibernate.search.engine.search.sort.dsl.impl.CompositeSortComponentsStepImpl; import org.hibernate.search.engine.search.sort.dsl.impl.DistanceSortOptionsStepImpl; -import org.hibernate.search.engine.search.sort.dsl.impl.FieldSortOptionsGenericStepImpl; -import org.hibernate.search.engine.search.sort.dsl.impl.FieldSortOptionsStepImpl; import org.hibernate.search.engine.search.sort.dsl.impl.ScoreSortOptionsStepImpl; import org.hibernate.search.engine.search.sort.dsl.impl.SearchSortFactoryExtensionStep; import org.hibernate.search.engine.search.sort.dsl.impl.WithParametersSortFinalStep; @@ -57,13 +56,13 @@ public SortThenStep indexOrder() { @Override public FieldSortOptionsStep field(String fieldPath) { - return new FieldSortOptionsStepImpl<>( dslContext, fieldPath ); + return AbstractFieldSortOptionsGenericStep.create( dslContext, fieldPath ); } @Override public FieldSortOptionsGenericStep> field( FieldSortFieldReference fieldReference) { - return new FieldSortOptionsGenericStepImpl<>( dslContext, fieldReference ); + return AbstractFieldSortOptionsGenericStep.create( dslContext, fieldReference ); } @Override @@ -87,7 +86,7 @@ public SortThenStep composite(Consumer withParameters(Function sortCreator) { - return new WithParametersSortFinalStep( dslContext, sortCreator ); + return new WithParametersSortFinalStep<>( dslContext, sortCreator ); } @Override