Skip to content

Commit

Permalink
HSEARCH-4859 Add parameterized methods
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta authored and yrodiere committed Jun 27, 2023
1 parent 8a75143 commit 719bd6f
Show file tree
Hide file tree
Showing 31 changed files with 184 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,23 @@ public SearchPredicateFactory predicate() {
}

@Override
public Object param(String name) {
public <T> T param(String name, Class<T> paramType) {
Contracts.assertNotNull( name, "name" );
Contracts.assertNotNull( paramType, "paramType" );

Object value = params.get( name );
if ( value == null ) {
throw log.paramNotDefined( name, predicateName, field.eventContext() );
}
return value;
return paramType.cast( value );
}

@Override
public Optional<Object> paramOptional(String name) {
public <T> Optional<T> paramOptional(String name, Class<T> paramType) {
Contracts.assertNotNull( name, "name" );
return Optional.ofNullable( params.get( name ) );
Contracts.assertNotNull( paramType, "paramType" );

return Optional.ofNullable( params.get( name ) ).map( paramType::cast );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,23 @@ public SearchPredicateFactory predicate() {
}

@Override
public Object param(String name) {
public <T> T param(String name, Class<T> paramType) {
Contracts.assertNotNull( name, "name" );
Contracts.assertNotNull( paramType, "paramType" );

Object value = params.get( name );
if ( value == null ) {
throw log.paramNotDefined( name, predicateName, field.eventContext() );
}
return value;
return paramType.cast( value );
}

@Override
public Optional<Object> paramOptional(String name) {
public <T> Optional<T> paramOptional(String name, Class<T> paramType) {
Contracts.assertNotNull( name, "name" );
return Optional.ofNullable( params.get( name ) );
Contracts.assertNotNull( paramType, "paramType" );

return Optional.ofNullable( params.get( name ) ).map( paramType::cast );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
public class OffsetIdentifierBinder implements IdentifierBinder {

@Override
@SuppressWarnings("unchecked")
public void bind(IdentifierBindingContext<?> context) {
String offset = (String) context.param( "offset" ); // <1>
String offset = context.param( "offset", String.class ); // <1>
context.bridge(
Integer.class,
new OffsetIdentifierBridge( Integer.parseInt( offset ) ) // <2>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private static class SkuIdentifierMatchPredicateDefinition implements PredicateD
public SearchPredicate create(PredicateDefinitionContext context) {
SearchPredicateFactory f = context.predicate(); // <2>

String pattern = (String) context.param( "pattern" ); // <3>
String pattern = context.param( "pattern", String.class ); // <3>

return f.and().with( and -> { // <4>
// An SKU identifier pattern is formatted this way: "<department code>.<collection code>.<item code>".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
public class InvoiceLineItemsSummaryBinder implements PropertyBinder {

@Override
@SuppressWarnings("uncheked")
public void bind(PropertyBindingContext context) {
context.dependencies()
.use( "category" )
.use( "amount" );

String fieldName = (String) context.param( "fieldName" ); // <1>
String fieldName = context.param( "fieldName", String.class ); // <1>
IndexSchemaObjectField summaryField = context.indexSchemaElement()
.objectField( fieldName ); // <2>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
public class FullNameBinder implements TypeBinder {

@Override
@SuppressWarnings("unchecked")
public void bind(TypeBindingContext context) {
context.dependencies()
.use( "firstName" )
Expand All @@ -29,7 +28,7 @@ public void bind(TypeBindingContext context) {
.toReference();

IndexFieldReference<String> fullNameSortField = null;
String sortField = (String) context.param( "sortField" ); // <1>
String sortField = context.param( "sortField", String.class ); // <1>
if ( "true".equalsIgnoreCase( sortField ) ) { // <2>
fullNameSortField = context.indexSchemaElement()
.field(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
public class BooleanAsStringBinder implements ValueBinder {

@Override
@SuppressWarnings("unchecked")
public void bind(ValueBindingContext<?> context) {
String trueAsString = (String) context.param( "trueAsString" ); // <1>
String falseAsString = (String) context.param( "falseAsString" );
String trueAsString = context.param( "trueAsString", String.class ); // <1>
String falseAsString = context.param( "falseAsString", String.class );

context.bridge( Boolean.class, // <2>
new BooleanAsStringBridge( trueAsString, falseAsString ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class MyFieldProjectionBinder implements ProjectionBinder {
@Override
public void bind(ProjectionBindingContext context) {
String fieldName = (String) context.param( "fieldName" ); // <1>
String fieldName = context.param( "fieldName", String.class ); // <1>
context.definition(
String.class,
new MyProjectionDefinition( fieldName ) // <2>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,43 @@ public interface PredicateDefinitionContext {
* @return The value provided to {@link NamedPredicateOptionsStep#param(String, Object)} for this parameter.
* @throws SearchException If no value was provided for this parameter.
* @see NamedPredicateOptionsStep#param(String, Object)
* @deprecated Use {@link #param(String, Class)} instead.
*/
Object param(String name);
@Deprecated
default Object param(String name) {
return param( name, Object.class );
}

/**
* @param name The name of the parameter.
* @param paramType The type of the parameter.
* @param <T> The type of the parameter.
* @return The value provided to {@link NamedPredicateOptionsStep#param(String, Object)} for this parameter.
* @throws SearchException If no value was provided for this parameter.
* @see NamedPredicateOptionsStep#param(String, Object)
*/
<T> T param(String name, Class<T> paramType);

/**
* @param name The name of the parameter.
* @return An optional containing the value provided to {@link NamedPredicateOptionsStep#param(String, Object)}
* for this parameter, or {@code Optional.empty()} if no value was provided for this parameter.
* @see NamedPredicateOptionsStep#param(String, Object)
* @deprecated Use {@link #paramOptional(String, Class)} instead.
*/
@Deprecated
default Optional<Object> paramOptional(String name) {
return paramOptional( name, Object.class );
}

/**
* @param name The name of the parameter.
* @param paramType The type of the parameter.
* @param <T> The type of the parameter.
* @return An optional containing the value provided to {@link NamedPredicateOptionsStep#param(String, Object)}
* for this parameter, or {@code Optional.empty()} if no value was provided for this parameter.
* @see NamedPredicateOptionsStep#param(String, Object)
*/
Optional<Object> paramOptional(String name);
<T> Optional<T> paramOptional(String name, Class<T> paramType);

}
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public static class StubPredicateDefinition implements PredicateDefinition {

@Override
public SearchPredicate create(PredicateDefinitionContext context) {
PredicateDefinition impl = (PredicateDefinition) context.param( IMPL_PARAM_NAME );
PredicateDefinition impl = context.param( IMPL_PARAM_NAME, PredicateDefinition.class );
return impl.create( context );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ public TestPredicateDefinition(String field1Name, String field2Name) {

@Override
public SearchPredicate create(PredicateDefinitionContext context) {
String word1 = (String) context.param( "value1" );
String word2 = (String) context.param( "value2" );
String word1 = context.param( "value1", String.class );
String word2 = context.param( "value2", String.class );
SearchPredicateFactory f = context.predicate();
return f.and(
f.match().field( field1Name ).matching( word1 ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public int hashCode() {
@Override
public SearchPredicate create(PredicateDefinitionContext context) {
return context.predicate().match().field( fieldName )
.matching( context.param( "value" ) )
.matching( context.param( "value", Object.class ) )
.toPredicate();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public void param() {
.param( "param3", givenParams[2] )
.param( "param4", givenParams[3] )
.param( "impl", (PredicateDefinition) context -> {
receivedParams[0] = context.param( "param1" );
receivedParams[1] = context.param( "param2" );
receivedParams[2] = context.param( "param3" );
receivedParams[3] = context.paramOptional( "param5" );
receivedParams[0] = context.param( "param1", String.class );
receivedParams[1] = context.param( "param2", Object.class );
receivedParams[2] = context.param( "param3", Long.class );
receivedParams[3] = context.paramOptional( "param5", Optional.class );
return context.predicate().matchAll().toPredicate();
} ) ) )
.hasDocRefHitsAnyOrder( index.typeName(), DOCUMENT_1 );
Expand All @@ -74,24 +74,24 @@ public void param() {
@Test
public void param_absent() {
Object[] expectedParams = new Object[] { Optional.empty() };
Object[] actualsParams = new Object[1];
Object[] actualParams = new Object[1];

assertThatQuery( index.query()
.where( f -> f.named( "stub-predicate" )
.param( "impl", (PredicateDefinition) context -> {
actualsParams[0] = context.paramOptional( "absent" );
actualParams[0] = context.paramOptional( "absent", Optional.class );
return context.predicate().matchAll().toPredicate();
} ) ) )
.hasDocRefHitsAnyOrder( index.typeName(), DOCUMENT_1 );

assertThat( actualsParams ).containsExactly( expectedParams );
assertThat( actualParams ).containsExactly( expectedParams );
}

@Test
public void param_nullName() {
assertThatThrownBy( () -> index.createScope().predicate().named( "stub-predicate" )
.param( "impl", (PredicateDefinition) context -> {
context.param( null );
context.param( null, String.class );
return context.predicate().matchAll().toPredicate();
} )
.toPredicate() )
Expand All @@ -103,7 +103,7 @@ public void param_nullName() {
public void missingParam() {
assertThatThrownBy( () -> index.createScope().predicate().named( "stub-predicate" )
.param( "impl", (PredicateDefinition) context -> {
context.param( "missing" );
context.param( "missing", String.class );
return context.predicate().matchAll().toPredicate();
} )
.toPredicate() )
Expand Down Expand Up @@ -148,7 +148,7 @@ static ObjectFieldBinding create(IndexSchemaElement parent, String relativeField
public static class StubPredicateDefinition implements PredicateDefinition {
@Override
public SearchPredicate create(PredicateDefinitionContext context) {
PredicateDefinition impl = (PredicateDefinition) context.param( "impl" );
PredicateDefinition impl = context.param( "impl", PredicateDefinition.class );
return impl.create( context );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,8 @@ public void bind(IdentifierBindingContext<?> context) {
}
}

@SuppressWarnings("uncheked")
private static String extractFixedPrefix(IdentifierBindingContext<?> context) {
return (String) context.param( "fixedPrefix" );
return context.param( "fixedPrefix", String.class );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,8 @@ public void bind(ValueBindingContext<?> context) {
}
}

@SuppressWarnings("uncheked")
private static String extractFixedPrefix(ValueBindingContext<?> context) {
return (String) context.param( "fixedPrefix" );
return context.param( "fixedPrefix", String.class );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,13 @@ public void bind(ValueBindingContext<?> context) {
}
}

@SuppressWarnings("uncheked")
private static int extractBase(ValueBindingContext<?> context) {
Optional<Object> optionalBase = context.paramOptional( "base" );
Optional<Integer> optionalBase = context.paramOptional( "base", Integer.class );
if ( optionalBase.isPresent() ) {
return (Integer) optionalBase.get();
return optionalBase.get();
}

String stringBase = (String) context.param( "stringBase" );
String stringBase = context.param( "stringBase", String.class );
return Integer.parseInt( stringBase );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1593,14 +1593,13 @@ public void bind(IdentifierBindingContext<?> context) {
context.bridge( Long.class, new Bridge( extractBase( context ) ) );
}

@SuppressWarnings("uncheked")
private static int extractBase(IdentifierBindingContext<?> context) {
Optional<Object> optionalBase = context.paramOptional( "base" );
Optional<Integer> optionalBase = context.paramOptional( "base", Integer.class );
if ( optionalBase.isPresent() ) {
return (Integer) optionalBase.get();
return optionalBase.get();
}

String stringBase = (String) context.param( "stringBase" );
String stringBase = context.param( "stringBase", String.class );
return Integer.parseInt( stringBase );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,8 @@ public void bind(ValueBindingContext<?> context) {
}
}

@SuppressWarnings("uncheked")
private static String extractFixedPrefix(ValueBindingContext<?> context) {
return (String) context.param( "fixedPrefix" );
return context.param( "fixedPrefix", String.class );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,13 @@ public void bind(MarkerBindingContext context) {
context.marker( new ScaleMarker( extractScale( context ) ) );
}

@SuppressWarnings("uncheked")
private static int extractScale(BindingContext context) {
Optional<Object> optionalScale = context.paramOptional( "scale" );
Optional<Integer> optionalScale = context.paramOptional( "scale", Integer.class );
if ( optionalScale.isPresent() ) {
return (Integer) optionalScale.get();
return optionalScale.get();
}

String stringScale = (String) context.param( "stringScale" );
String stringScale = context.param( "stringScale", String.class );
return Integer.parseInt( stringScale );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public MyProjection(String text) {
public static class ParametricBinder implements ProjectionBinder {
@Override
public void bind(ProjectionBindingContext context) {
String fieldPath = (String) context.param( "fieldPath" );
String fieldPath = context.param( "fieldPath", String.class );
context.definition( String.class,
(factory, context1) -> factory.field( fieldPath, String.class ).toProjection() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,13 @@ public void bind(PropertyBindingContext context) {
} );
}

@SuppressWarnings("uncheked")
private static int extractBase(PropertyBindingContext context) {
Optional<Object> optionalBase = context.paramOptional( "base" );
Optional<Integer> optionalBase = context.paramOptional( "base", Integer.class );
if ( optionalBase.isPresent() ) {
return (Integer) optionalBase.get();
return optionalBase.get();
}

String stringBase = (String) context.param( "stringBase" );
String stringBase = context.param( "stringBase", String.class );
return Integer.parseInt( stringBase );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,14 +764,13 @@ public void bind(RoutingBindingContext context) {
context.bridge( Object.class, new ParametricBridge( modulus( context ) ) );
}

@SuppressWarnings("uncheked")
private static int modulus(RoutingBindingContext context) {
Optional<Object> optionalModulus = context.paramOptional( "modulus" );
Optional<Integer> optionalModulus = context.paramOptional( "modulus", Integer.class );
if ( optionalModulus.isPresent() ) {
return (Integer) optionalModulus.get();
return optionalModulus.get();
}

String stringModulus = (String) context.param( "stringModulus" );
String stringModulus = context.param( "stringModulus", String.class );
return Integer.parseInt( stringModulus );
}
}
Expand Down

0 comments on commit 719bd6f

Please sign in to comment.