Skip to content

Commit

Permalink
HSEARCH-3608 Introduce optional param get
Browse files Browse the repository at this point in the history
To use in case we can expect that the param has not been defined for the current binding
  • Loading branch information
fax4ever committed Apr 29, 2021
1 parent 6942501 commit 02b6077
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Collections;
import java.util.Optional;

import org.hibernate.search.engine.backend.types.Aggregable;
import org.hibernate.search.engine.backend.types.Searchable;
Expand Down Expand Up @@ -374,16 +375,16 @@ public T toIndexedValue(String value, ValueBridgeToIndexedValueContext context)

public static class ParametricBridge implements ValueBridge<Integer, String> {

private final Integer base;
private final int base;

private ParametricBridge(Integer base) {
private ParametricBridge(int base) {
this.base = base;
}

@Override
public String toIndexedValue(Integer value, ValueBridgeToIndexedValueContext context) {
if ( value == null ) {
return base.toString();
return base + "";
}

return ( value % base ) + "";
Expand All @@ -398,17 +399,13 @@ public void bind(ValueBindingContext<?> context) {
}

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

String stringBase = (String) context.param( "stringBase" );
if ( stringBase == null ) {
return 0;
}

return Integer.parseInt( stringBase );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;

Expand Down Expand Up @@ -1414,10 +1415,10 @@ public void bind(IdentifierBindingContext<?> context) {
}

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

String stringBase = (String) context.param( "stringBase" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.lang.invoke.MethodHandles;
import java.util.Collections;
import java.util.Optional;

import org.hibernate.search.integrationtest.mapper.pojo.testsupport.util.rule.JavaBeanMappingSetupHelper;
import org.hibernate.search.mapper.pojo.bridge.binding.BindingContext;
Expand Down Expand Up @@ -85,10 +86,10 @@ public void bind(MarkerBindingContext context) {
}

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

String stringScale = (String) context.param( "stringScale" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.lang.invoke.MethodHandles;
import java.util.Collections;
import java.util.Optional;

import org.hibernate.search.engine.backend.document.DocumentElement;
import org.hibernate.search.engine.backend.document.IndexFieldReference;
Expand Down Expand Up @@ -184,7 +185,7 @@ public void bind(PropertyBindingContext context) {
.toReference();
IndexFieldReference<Integer> diff = context.indexSchemaElement().field( "diff", f -> f.asInteger() )
.toReference();
Integer base = extractBase( context );
int base = extractBase( context );

context.bridge( Integer.class, (DocumentElement target, Integer bridgedElement,
PropertyBridgeWriteContext writeContext) -> {
Expand All @@ -194,10 +195,10 @@ public void bind(PropertyBindingContext context) {
}

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

String stringBase = (String) context.param( "stringBase" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.lang.invoke.MethodHandles;
import java.util.Collections;
import java.util.Optional;

import org.hibernate.search.integrationtest.mapper.pojo.testsupport.util.rule.JavaBeanMappingSetupHelper;
import org.hibernate.search.integrationtest.mapper.pojo.work.operations.PojoIndexingPlanOperationIT;
Expand Down Expand Up @@ -731,9 +732,9 @@ public void bind(RoutingBindingContext context) {

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

String stringModulus = (String) context.param( "stringModulus" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collections;
import java.util.Optional;

import org.hibernate.search.engine.backend.types.Aggregable;
import org.hibernate.search.engine.backend.types.Searchable;
Expand Down Expand Up @@ -517,18 +518,13 @@ public void bind(ValueBindingContext<?> context) {

@SuppressWarnings("uncheked")
private static BigDecimal extractBaseDecimal(ValueBindingContext<?> context) {
BigDecimal baseDecimal = (BigDecimal) context.param( "baseDecimal" );
if ( baseDecimal != null ) {
return baseDecimal;
Optional<Object> optionalBaseDecimal = context.paramOptional( "baseDecimal" );
if ( optionalBaseDecimal.isPresent() ) {
return (BigDecimal) optionalBaseDecimal.get();
}

Object unscaledValParam = context.param( "unscaledVal" );
Object scaleParam = context.param( "scale" );

if ( unscaledValParam == null || scaleParam == null ) {
return BigDecimal.ZERO;
}

BigInteger unscaledVal = new BigInteger( (String) unscaledValParam );
int scale = Integer.parseInt( (String) scaleParam );
return new BigDecimal( unscaledVal, scale );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.lang.invoke.MethodHandles;
import java.util.Collections;
import java.util.Optional;

import org.hibernate.search.engine.backend.document.DocumentElement;
import org.hibernate.search.engine.backend.document.IndexFieldReference;
Expand Down Expand Up @@ -206,9 +207,9 @@ else if ( bridgedElement instanceof NotAnnotatedEntity ) {

@SuppressWarnings("uncheked")
private static Integer extractBase(TypeBindingContext context) {
Integer base = (Integer) context.param( "base" );
if ( base != null ) {
return base;
Optional<Object> optionalBase = context.paramOptional( "base" );
if ( optionalBase.isPresent() ) {
return (Integer) optionalBase.get();
}

String stringBase = (String) context.param( "stringBase" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package org.hibernate.search.mapper.pojo.bridge.binding;

import java.util.Optional;

import org.hibernate.search.engine.environment.bean.BeanResolver;
import org.hibernate.search.util.common.SearchException;

Expand All @@ -24,4 +26,11 @@ public interface BindingContext {
*/
Object param(String name);

/**
* @param name The name of the param
* @return Get an optional param defined for the binder by the given name,
* a param having such name may either exist or not.
*/
Optional<Object> paramOptional(String name);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.lang.invoke.MethodHandles;
import java.util.Map;
import java.util.Optional;

import org.hibernate.search.engine.environment.bean.BeanResolver;
import org.hibernate.search.mapper.pojo.bridge.binding.BindingContext;
Expand Down Expand Up @@ -40,4 +41,9 @@ public Object param(String name) {

return value;
}

@Override
public Optional<Object> paramOptional(String name) {
return Optional.ofNullable( params.get( name ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package org.hibernate.search.mapper.pojo.bridge.mapping.programmatic.identifiertovalue.impl;

import java.util.Optional;

import org.hibernate.search.engine.environment.bean.BeanHolder;
import org.hibernate.search.engine.environment.bean.BeanResolver;
import org.hibernate.search.mapper.pojo.bridge.IdentifierBridge;
Expand Down Expand Up @@ -44,6 +46,11 @@ public Object param(String name) {
return delegate.param( name );
}

@Override
public Optional<Object> paramOptional(String name) {
return delegate.paramOptional( name );
}

@Override
public BeanResolver beanResolver() {
return delegate.beanResolver();
Expand Down

0 comments on commit 02b6077

Please sign in to comment.