Skip to content

Commit

Permalink
HSEARCH-3687 Use self-types in projection DSL interfaces
Browse files Browse the repository at this point in the history
So that we won't need to break APIs if one day we need backend-specific
extensions to add options to standard projections.
  • Loading branch information
yrodiere committed Sep 18, 2019
1 parent bb42532 commit dc43656
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 55 deletions.
Expand Up @@ -8,7 +8,11 @@

/**
* The initial and final step in a composite projection definition, where optional parameters can be set.
*
* @param <S> The "self" type (the actual exposed type of this step).
* @param <T> The type of composed projections.
*/
public interface CompositeProjectionOptionsStep<T> extends ProjectionFinalStep<T> {
public interface CompositeProjectionOptionsStep<S extends CompositeProjectionOptionsStep<? extends S, T>, T>
extends ProjectionFinalStep<T> {

}
Expand Up @@ -10,8 +10,11 @@

/**
* The initial and final step in a "distance to field" projection definition, where optional parameters can be set.
*
* @param <S> The "self" type (the actual exposed type of this step).
*/
public interface DistanceToFieldProjectionOptionsStep extends ProjectionFinalStep<Double> {
public interface DistanceToFieldProjectionOptionsStep<S extends DistanceToFieldProjectionOptionsStep>
extends ProjectionFinalStep<Double> {

/**
* Defines the unit of the computed distance (default is meters).
Expand Down
Expand Up @@ -10,7 +10,10 @@

/**
* The initial and final step in a "document reference" projection definition, where optional parameters can be set.
*
* @param <S> The "self" type (the actual exposed type of this step).
*/
public interface DocumentReferenceProjectionOptionsStep extends ProjectionFinalStep<DocumentReference> {
public interface DocumentReferenceProjectionOptionsStep<S extends DocumentReferenceProjectionOptionsStep<? extends S>>
extends ProjectionFinalStep<DocumentReference> {

}
Expand Up @@ -8,7 +8,11 @@

/**
* The initial and final step in an "entity" projection definition, where optional parameters can be set.
*
* @param <S> The "self" type (the actual exposed type of this step).
* @param <E> The type of projected entities.
*/
public interface EntityProjectionOptionsStep<E> extends ProjectionFinalStep<E> {
public interface EntityProjectionOptionsStep<S extends EntityProjectionOptionsStep<? extends S, E>, E>
extends ProjectionFinalStep<E> {

}
Expand Up @@ -8,7 +8,11 @@

/**
* The initial and final step in an "entity reference" projection definition, where optional parameters can be set.
*
* @param <S> The "self" type (the actual exposed type of this step).
* @param <R> The type of projected entity references.
*/
public interface EntityReferenceProjectionOptionsStep<R> extends ProjectionFinalStep<R> {
public interface EntityReferenceProjectionOptionsStep<S extends EntityReferenceProjectionOptionsStep<? extends S, R>, R>
extends ProjectionFinalStep<R> {

}
Expand Up @@ -8,7 +8,11 @@

/**
* The initial and final step in a "field" projection definition, where optional parameters can be set.
*
* @param <S> The "self" type (the actual exposed type of this step).
* @param <T> The type of projected field values.
*/
public interface FieldProjectionOptionsStep<T> extends ProjectionFinalStep<T> {
public interface FieldProjectionOptionsStep<S extends FieldProjectionOptionsStep<? extends S, T>, T>
extends ProjectionFinalStep<T> {

}
Expand Up @@ -8,7 +8,9 @@

/**
* The initial and final step in a "score" projection definition, where optional parameters can be set.
*
* @param <S> The "self" type (the actual exposed type of this step).
*/
public interface ScoreProjectionOptionsStep extends ProjectionFinalStep<Float> {
public interface ScoreProjectionOptionsStep<S extends ScoreProjectionOptionsStep> extends ProjectionFinalStep<Float> {

}
Expand Up @@ -32,7 +32,7 @@ public interface SearchProjectionFactory<R, E> {
*
* @return A DSL step where the "document reference" projection can be defined in more details.
*/
DocumentReferenceProjectionOptionsStep documentReference();
DocumentReferenceProjectionOptionsStep<?> documentReference();

/**
* Project to a reference to the entity that was originally indexed.
Expand All @@ -42,7 +42,7 @@ public interface SearchProjectionFactory<R, E> {
*
* @return A DSL step where the "entity reference" projection can be defined in more details.
*/
EntityReferenceProjectionOptionsStep<R> entityReference();
EntityReferenceProjectionOptionsStep<?, R> entityReference();

/**
* Project to the entity was originally indexed.
Expand All @@ -53,7 +53,7 @@ public interface SearchProjectionFactory<R, E> {
*
* @return A DSL step where the "entity" projection can be defined in more details.
*/
EntityProjectionOptionsStep<E> entity();
EntityProjectionOptionsStep<?, E> entity();

/**
* Project to the value of a field in the indexed document.
Expand All @@ -66,7 +66,7 @@ public interface SearchProjectionFactory<R, E> {
* @param <T> The resulting type of the projection.
* @return A DSL step where the "field" projection can be defined in more details.
*/
default <T> FieldProjectionOptionsStep<T> field(String absoluteFieldPath, Class<T> type) {
default <T> FieldProjectionOptionsStep<?, T> field(String absoluteFieldPath, Class<T> type) {
return field( absoluteFieldPath, type, ValueConvert.YES );
}

Expand All @@ -80,7 +80,7 @@ default <T> FieldProjectionOptionsStep<T> field(String absoluteFieldPath, Class<
* See {@link ValueConvert}.
* @return A DSL step where the "field" projection can be defined in more details.
*/
<T> FieldProjectionOptionsStep<T> field(String absoluteFieldPath, Class<T> type, ValueConvert convert);
<T> FieldProjectionOptionsStep<?, T> field(String absoluteFieldPath, Class<T> type, ValueConvert convert);

/**
* Project to the value of a field in the indexed document, without specifying a type.
Expand All @@ -91,7 +91,7 @@ default <T> FieldProjectionOptionsStep<T> field(String absoluteFieldPath, Class<
* @param absoluteFieldPath The absolute path of the field.
* @return A DSL step where the "field" projection can be defined in more details.
*/
default FieldProjectionOptionsStep<Object> field(String absoluteFieldPath) {
default FieldProjectionOptionsStep<?, Object> field(String absoluteFieldPath) {
return field( absoluteFieldPath, ValueConvert.YES );
}

Expand All @@ -103,14 +103,14 @@ default FieldProjectionOptionsStep<Object> field(String absoluteFieldPath) {
* See {@link ValueConvert}.
* @return A DSL step where the "field" projection can be defined in more details.
*/
FieldProjectionOptionsStep<Object> field(String absoluteFieldPath, ValueConvert convert);
FieldProjectionOptionsStep<?, Object> field(String absoluteFieldPath, ValueConvert convert);

/**
* Project on the score of the hit.
*
* @return A DSL step where the "score" projection can be defined in more details.
*/
ScoreProjectionOptionsStep score();
ScoreProjectionOptionsStep<?> score();

/**
* Project on the distance from the center to a {@link GeoPoint} field.
Expand All @@ -119,15 +119,15 @@ default FieldProjectionOptionsStep<Object> field(String absoluteFieldPath) {
* @param center The center to compute the distance from.
* @return A DSL step where the "distance" projection can be defined in more details.
*/
DistanceToFieldProjectionOptionsStep distance(String absoluteFieldPath, GeoPoint center);
DistanceToFieldProjectionOptionsStep<?> distance(String absoluteFieldPath, GeoPoint center);

/**
* Create a projection that will compose a {@link List} based on the given projections.
*
* @param projections The projections used to populate the list, in order.
* @return A DSL step where the "composite" projection can be defined in more details.
*/
default CompositeProjectionOptionsStep<List<?>> composite(SearchProjection<?>... projections) {
default CompositeProjectionOptionsStep<?, List<?>> composite(SearchProjection<?>... projections) {
return composite( Function.identity(), projections );
}

Expand All @@ -137,7 +137,7 @@ default CompositeProjectionOptionsStep<List<?>> composite(SearchProjection<?>...
* @param dslFinalSteps The final steps in the projection DSL allowing the retrieval of {@link SearchProjection}s.
* @return A DSL step where the "composite" projection can be defined in more details.
*/
default CompositeProjectionOptionsStep<List<?>> composite(ProjectionFinalStep<?>... dslFinalSteps) {
default CompositeProjectionOptionsStep<?, List<?>> composite(ProjectionFinalStep<?>... dslFinalSteps) {
return composite( Function.identity(), dslFinalSteps );
}

Expand All @@ -149,7 +149,7 @@ default CompositeProjectionOptionsStep<List<?>> composite(ProjectionFinalStep<?>
* @param <T> The type of the custom object composing the projected elements.
* @return A DSL step where the "composite" projection can be defined in more details.
*/
<T> CompositeProjectionOptionsStep<T> composite(Function<List<?>, T> transformer, SearchProjection<?>... projections);
<T> CompositeProjectionOptionsStep<?, T> composite(Function<List<?>, T> transformer, SearchProjection<?>... projections);

/**
* Create a projection that will compose a custom object based on the given almost-built projections.
Expand All @@ -159,7 +159,7 @@ default CompositeProjectionOptionsStep<List<?>> composite(ProjectionFinalStep<?>
* @param <T> The type of the custom object composing the projected elements.
* @return A DSL step where the "composite" projection can be defined in more details.
*/
default <T> CompositeProjectionOptionsStep<T> composite(Function<List<?>, T> transformer,
default <T> CompositeProjectionOptionsStep<?, T> composite(Function<List<?>, T> transformer,
ProjectionFinalStep<?>... dslFinalSteps) {
SearchProjection<?>[] projections = new SearchProjection<?>[dslFinalSteps.length];
for ( int i = 0; i < dslFinalSteps.length; i++ ) {
Expand All @@ -177,7 +177,7 @@ default <T> CompositeProjectionOptionsStep<T> composite(Function<List<?>, T> tra
* @param <T> The type of the custom object composing the projected element.
* @return A DSL step where the "composite" projection can be defined in more details.
*/
<P, T> CompositeProjectionOptionsStep<T> composite(Function<P, T> transformer, SearchProjection<P> projection);
<P, T> CompositeProjectionOptionsStep<?, T> composite(Function<P, T> transformer, SearchProjection<P> projection);

/**
* Create a projection that will compose a custom object based on one almost-built projection.
Expand All @@ -189,7 +189,7 @@ default <T> CompositeProjectionOptionsStep<T> composite(Function<List<?>, T> tra
* @param <T> The type of the custom object composing the projected element.
* @return A DSL step where the "composite" projection can be defined in more details.
*/
default <P, T> CompositeProjectionOptionsStep<T> composite(Function<P, T> transformer, ProjectionFinalStep<P> dslFinalStep) {
default <P, T> CompositeProjectionOptionsStep<?, T> composite(Function<P, T> transformer, ProjectionFinalStep<P> dslFinalStep) {
return composite( transformer, dslFinalStep.toProjection() );
}

Expand All @@ -204,7 +204,7 @@ default <P, T> CompositeProjectionOptionsStep<T> composite(Function<P, T> transf
* @param <T> The type of the custom object composing the projected elements.
* @return A DSL step where the "composite" projection can be defined in more details.
*/
<P1, P2, T> CompositeProjectionOptionsStep<T> composite(BiFunction<P1, P2, T> transformer,
<P1, P2, T> CompositeProjectionOptionsStep<?, T> composite(BiFunction<P1, P2, T> transformer,
SearchProjection<P1> projection1, SearchProjection<P2> projection2);

/**
Expand All @@ -220,7 +220,7 @@ <P1, P2, T> CompositeProjectionOptionsStep<T> composite(BiFunction<P1, P2, T> tr
* @param <T> The type of the custom object composing the projected elements.
* @return A DSL step where the "composite" projection can be defined in more details.
*/
default <P1, P2, T> CompositeProjectionOptionsStep<T> composite(BiFunction<P1, P2, T> transformer,
default <P1, P2, T> CompositeProjectionOptionsStep<?, T> composite(BiFunction<P1, P2, T> transformer,
ProjectionFinalStep<P1> dslFinalStep1, ProjectionFinalStep<P2> dslFinalStep2) {
return composite(
transformer,
Expand All @@ -241,7 +241,7 @@ default <P1, P2, T> CompositeProjectionOptionsStep<T> composite(BiFunction<P1, P
* @param <T> The type of the custom object composing the projected elements.
* @return A DSL step where the "composite" projection can be defined in more details.
*/
<P1, P2, P3, T> CompositeProjectionOptionsStep<T> composite(TriFunction<P1, P2, P3, T> transformer,
<P1, P2, P3, T> CompositeProjectionOptionsStep<?, T> composite(TriFunction<P1, P2, P3, T> transformer,
SearchProjection<P1> projection1, SearchProjection<P2> projection2, SearchProjection<P3> projection3);

/**
Expand All @@ -260,7 +260,7 @@ <P1, P2, P3, T> CompositeProjectionOptionsStep<T> composite(TriFunction<P1, P2,
* @param <T> The type of the custom object composing the projected elements.
* @return A DSL step where the "composite" projection can be defined in more details.
*/
default <P1, P2, P3, T> CompositeProjectionOptionsStep<T> composite(TriFunction<P1, P2, P3, T> transformer,
default <P1, P2, P3, T> CompositeProjectionOptionsStep<?, T> composite(TriFunction<P1, P2, P3, T> transformer,
ProjectionFinalStep<P1> dslFinalStep1, ProjectionFinalStep<P2> dslFinalStep2,
ProjectionFinalStep<P3> dslFinalStep3) {
return composite(
Expand Down
Expand Up @@ -17,7 +17,8 @@
import org.hibernate.search.util.common.function.TriFunction;


public class CompositeProjectionOptionsStepImpl<T> implements CompositeProjectionOptionsStep<T> {
public class CompositeProjectionOptionsStepImpl<T>
implements CompositeProjectionOptionsStep<CompositeProjectionOptionsStepImpl<T>, T> {

private final CompositeProjectionBuilder<T> compositeProjectionBuilder;

Expand Down
Expand Up @@ -38,46 +38,46 @@ public DefaultSearchProjectionFactory(SearchProjectionBuilderFactory factory) {
}

@Override
public DocumentReferenceProjectionOptionsStep documentReference() {
public DocumentReferenceProjectionOptionsStep<?> documentReference() {
return new DocumentReferenceProjectionOptionsStepImpl( factory );
}

@Override
public <T> FieldProjectionOptionsStep<T> field(String absoluteFieldPath, Class<T> clazz, ValueConvert convert) {
public <T> FieldProjectionOptionsStep<?, T> field(String absoluteFieldPath, Class<T> clazz, ValueConvert convert) {
Contracts.assertNotNull( clazz, "clazz" );

return new FieldProjectionOptionsStepImpl<>( factory, absoluteFieldPath, clazz, convert );
}

@Override
public FieldProjectionOptionsStep<Object> field(String absoluteFieldPath, ValueConvert convert) {
public FieldProjectionOptionsStep<?, Object> field(String absoluteFieldPath, ValueConvert convert) {
return field( absoluteFieldPath, Object.class, convert );
}

@Override
public EntityReferenceProjectionOptionsStep<R> entityReference() {
public EntityReferenceProjectionOptionsStep<?, R> entityReference() {
return new EntityReferenceProjectionOptionsStepImpl<>( factory );
}

@Override
public EntityProjectionOptionsStep<E> entity() {
public EntityProjectionOptionsStep<?, E> entity() {
return new EntityProjectionOptionsStepImpl<>( factory );
}

@Override
public ScoreProjectionOptionsStep score() {
public ScoreProjectionOptionsStep<?> score() {
return new ScoreProjectionOptionsStepImpl( factory );
}

@Override
public DistanceToFieldProjectionOptionsStep distance(String absoluteFieldPath, GeoPoint center) {
public DistanceToFieldProjectionOptionsStep<?> distance(String absoluteFieldPath, GeoPoint center) {
Contracts.assertNotNull( center, "center" );

return new DistanceToFieldProjectionOptionsStepImpl( factory, absoluteFieldPath, center );
}

@Override
public <T> CompositeProjectionOptionsStep<T> composite(Function<List<?>, T> transformer,
public <T> CompositeProjectionOptionsStep<?, T> composite(Function<List<?>, T> transformer,
SearchProjection<?>... projections) {
Contracts.assertNotNull( transformer, "transformer" );
Contracts.assertNotNullNorEmpty( projections, "projections" );
Expand All @@ -86,15 +86,15 @@ public <T> CompositeProjectionOptionsStep<T> composite(Function<List<?>, T> tran
}

@Override
public <P, T> CompositeProjectionOptionsStep<T> composite(Function<P, T> transformer, SearchProjection<P> projection) {
public <P, T> CompositeProjectionOptionsStep<?, T> composite(Function<P, T> transformer, SearchProjection<P> projection) {
Contracts.assertNotNull( transformer, "transformer" );
Contracts.assertNotNull( projection, "projection" );

return new CompositeProjectionOptionsStepImpl<>( factory, transformer, projection );
}

@Override
public <P1, P2, T> CompositeProjectionOptionsStep<T> composite(BiFunction<P1, P2, T> transformer,
public <P1, P2, T> CompositeProjectionOptionsStep<?, T> composite(BiFunction<P1, P2, T> transformer,
SearchProjection<P1> projection1, SearchProjection<P2> projection2) {
Contracts.assertNotNull( transformer, "transformer" );
Contracts.assertNotNull( projection1, "projection1" );
Expand All @@ -104,7 +104,7 @@ public <P1, P2, T> CompositeProjectionOptionsStep<T> composite(BiFunction<P1, P2
}

@Override
public <P1, P2, P3, T> CompositeProjectionOptionsStep<T> composite(TriFunction<P1, P2, P3, T> transformer,
public <P1, P2, P3, T> CompositeProjectionOptionsStep<?, T> composite(TriFunction<P1, P2, P3, T> transformer,
SearchProjection<P1> projection1, SearchProjection<P2> projection2, SearchProjection<P3> projection3) {
Contracts.assertNotNull( transformer, "transformer" );
Contracts.assertNotNull( projection1, "projection1" );
Expand Down
Expand Up @@ -16,7 +16,8 @@
import org.hibernate.search.util.common.impl.Contracts;


public class DistanceToFieldProjectionOptionsStepImpl implements DistanceToFieldProjectionOptionsStep {
public class DistanceToFieldProjectionOptionsStepImpl
implements DistanceToFieldProjectionOptionsStep<DistanceToFieldProjectionOptionsStepImpl> {

private final DistanceToFieldProjectionBuilder distanceFieldProjectionBuilder;

Expand Down
Expand Up @@ -13,7 +13,8 @@
import org.hibernate.search.engine.search.projection.spi.SearchProjectionBuilderFactory;


public class DocumentReferenceProjectionOptionsStepImpl implements DocumentReferenceProjectionOptionsStep {
public class DocumentReferenceProjectionOptionsStepImpl
implements DocumentReferenceProjectionOptionsStep<DocumentReferenceProjectionOptionsStepImpl> {

private final DocumentReferenceProjectionBuilder documentReferenceProjectionBuilder;

Expand Down
Expand Up @@ -12,7 +12,8 @@
import org.hibernate.search.engine.search.projection.spi.SearchProjectionBuilderFactory;


public class EntityProjectionOptionsStepImpl<E> implements EntityProjectionOptionsStep<E> {
public class EntityProjectionOptionsStepImpl<E>
implements EntityProjectionOptionsStep<EntityProjectionOptionsStepImpl<E>, E> {

private final EntityProjectionBuilder<E> entityProjectionBuilder;

Expand Down
Expand Up @@ -12,7 +12,8 @@
import org.hibernate.search.engine.search.projection.spi.SearchProjectionBuilderFactory;


public class EntityReferenceProjectionOptionsStepImpl<R> implements EntityReferenceProjectionOptionsStep<R> {
public class EntityReferenceProjectionOptionsStepImpl<R>
implements EntityReferenceProjectionOptionsStep<EntityReferenceProjectionOptionsStepImpl<R>, R> {

private final EntityReferenceProjectionBuilder<R> entityReferenceProjectionBuilder;

Expand Down
Expand Up @@ -13,7 +13,8 @@
import org.hibernate.search.engine.search.projection.spi.SearchProjectionBuilderFactory;


public class FieldProjectionOptionsStepImpl<T> implements FieldProjectionOptionsStep<T> {
public class FieldProjectionOptionsStepImpl<T>
implements FieldProjectionOptionsStep<FieldProjectionOptionsStepImpl<T>, T> {

private final FieldProjectionBuilder<T> fieldProjectionBuilder;

Expand Down

0 comments on commit dc43656

Please sign in to comment.