Skip to content

Commit

Permalink
HSEARCH-3609 Remove the cast() method from identifier and value bridges
Browse files Browse the repository at this point in the history
It's no longer needed, now that the binder passes the expected value
type to Hibernate Search explicitly.
  • Loading branch information
yrodiere committed Jul 15, 2019
1 parent b3a47d9 commit 35d2d51
Show file tree
Hide file tree
Showing 42 changed files with 31 additions and 235 deletions.
Expand Up @@ -166,11 +166,6 @@ public static class AuthenticationOutcomeBridge implements ValueBridge<Authentic
public String toIndexedValue(AuthenticationOutcome value, ValueBridgeToIndexedValueContext context) {
return value == null ? null : value.text;
}

@Override
public AuthenticationOutcome cast(Object value) {
return (AuthenticationOutcome) value;
}
}

}
Expand Up @@ -231,10 +231,5 @@ public String toDocumentIdentifier(BookId propertyValue, IdentifierBridgeToDocum
public BookId fromDocumentIdentifier(String documentIdentifier, IdentifierBridgeFromDocumentIdentifierContext context) {
return BookId.fromString( documentIdentifier );
}

@Override
public BookId cast(Object value) {
return (BookId) value;
}
}
}
Expand Up @@ -17,11 +17,6 @@ public String toIndexedValue(Integer value,
return value == null ? null : value.toString();
}

@Override
public Integer cast(Object value) {
return (Integer) value;
}

@Override
public boolean isCompatibleWith(ValueBridge<?, ?> other) {
return getClass().equals( other.getClass() );
Expand Down
Expand Up @@ -21,11 +21,6 @@ public String toIndexedValue(OptionalInt value,
: String.valueOf( value.getAsInt() );
}

@Override
public OptionalInt cast(Object value) {
return (OptionalInt) value;
}

@Override
public boolean isCompatibleWith(ValueBridge<?, ?> other) {
return getClass().equals( other.getClass() );
Expand Down
Expand Up @@ -363,10 +363,6 @@ public String toIndexedValue(String value,
ValueBridgeToIndexedValueContext context) {
return value == null ? null : PREFIX + value;
}
@Override
public String cast(Object value) {
return (String) value;
}
}

interface TestModelProvider {
Expand Down
Expand Up @@ -175,10 +175,6 @@ public Integer getId() {
public static class MyStringBridge implements IdentifierBridge<String> {
private static String TOSTRING = "<MyStringBridge toString() result>";
@Override
public String cast(Object value) {
throw new UnsupportedOperationException( "Should not be called" );
}
@Override
public String fromDocumentIdentifier(String documentIdentifier,
IdentifierBridgeFromDocumentIdentifierContext context) {
throw new UnsupportedOperationException( "Should not be called" );
Expand Down
Expand Up @@ -234,10 +234,6 @@ public List<Integer> getNumbers() {
public static class MyStringBridge implements ValueBridge<String, String> {
private static String TOSTRING = "<MyStringBridge toString() result>";
@Override
public String cast(Object value) {
throw new UnsupportedOperationException( "Should not be called" );
}
@Override
public String toIndexedValue(String value,
ValueBridgeToIndexedValueContext context) {
throw new UnsupportedOperationException( "Should not be called" );
Expand Down Expand Up @@ -354,10 +350,5 @@ public NoParsingValueBridge() {
public Integer toIndexedValue(Integer value, ValueBridgeToIndexedValueContext context) {
return value;
}

@Override
public Integer cast(Object value) {
return (Integer) value;
}
}
}
Expand Up @@ -537,11 +537,6 @@ public String toIndexedValue(Collection<String> value,
ValueBridgeToIndexedValueContext context) {
return value == null || value.isEmpty() ? null : value.iterator().next();
}
@Override
@SuppressWarnings("unchecked")
public Collection<String> cast(Object value) {
return (Collection<String>) value;
}
}

}
Expand Up @@ -418,11 +418,6 @@ public String toIndexedValue(WrappedValue value, ValueBridgeToIndexedValueContex
return value == null ? null : value.wrapped;
}

@Override
public WrappedValue cast(Object value) {
throw new UnsupportedOperationException( "Should not be called" );
}

public static class ExplictFieldTypeBinder implements ValueBinder {
@Override
public void bind(ValueBindingContext<?> context) {
Expand All @@ -437,11 +432,6 @@ public Integer toIndexedValue(WrappedValue value, ValueBridgeToIndexedValueConte
throw new UnsupportedOperationException( "Should not be called" );
}

@Override
public WrappedValue cast(Object value) {
throw new UnsupportedOperationException( "Should not be called" );
}

public static class ExplictFieldTypeBinder implements ValueBinder {
@Override
public void bind(ValueBindingContext<?> context) {
Expand Down
Expand Up @@ -390,11 +390,6 @@ public String toIndexedValue(WrappedValue value, ValueBridgeToIndexedValueContex
return value == null ? null : value.wrapped;
}

@Override
public WrappedValue cast(Object value) {
throw new UnsupportedOperationException( "Should not be called" );
}

public static class ExplictFieldTypeBinder implements ValueBinder {
@Override
public void bind(ValueBindingContext<?> context) {
Expand All @@ -409,11 +404,6 @@ public Integer toIndexedValue(WrappedValue value, ValueBridgeToIndexedValueConte
throw new UnsupportedOperationException( "Should not be called" );
}

@Override
public WrappedValue cast(Object value) {
throw new UnsupportedOperationException( "Should not be called" );
}

public static class ExplictFieldTypeBinder implements ValueBinder {
@Override
public void bind(ValueBindingContext<?> context) {
Expand Down
Expand Up @@ -328,11 +328,6 @@ public BigDecimal toIndexedValue(WrappedValue value, ValueBridgeToIndexedValueCo
return value == null ? null : value.wrapped;
}

@Override
public WrappedValue cast(Object value) {
throw new UnsupportedOperationException( "Should not be called" );
}

public static class ExplictFieldTypeBinder implements ValueBinder {
@Override
public void bind(ValueBindingContext<?> context) {
Expand All @@ -347,11 +342,6 @@ public Integer toIndexedValue(WrappedValue value, ValueBridgeToIndexedValueConte
throw new UnsupportedOperationException( "Should not be called" );
}

@Override
public WrappedValue cast(Object value) {
throw new UnsupportedOperationException( "Should not be called" );
}

public static class ExplictFieldTypeBinder implements ValueBinder {
@Override
public void bind(ValueBindingContext<?> context) {
Expand Down
Expand Up @@ -11,11 +11,6 @@

public final class IntegerAsStringValueBridge implements ValueBridge<Integer, String> {

@Override
public Integer cast(Object value) {
return (Integer) value;
}

@Override
public String toIndexedValue(Integer value,
ValueBridgeToIndexedValueContext context) {
Expand Down
Expand Up @@ -21,11 +21,6 @@ public String toIndexedValue(OptionalInt value,
: String.valueOf( value.getAsInt() );
}

@Override
public OptionalInt cast(Object value) {
return (OptionalInt) value;
}

@Override
public void close() {
// Nothing to do
Expand Down
Expand Up @@ -102,11 +102,6 @@ public void write(DocumentElement target, Object bridgedElement, PropertyBridgeW
throw unexpectedRuntimeUse();
}

@Override
public T cast(Object value) {
throw unexpectedRuntimeUse();
}

@Override
public String toIndexedValue(Object value,
ValueBridgeToIndexedValueContext context) {
Expand Down
Expand Up @@ -31,11 +31,6 @@ public ISBN fromIndexedValue(String value, ValueBridgeFromIndexedValueContext co
return value == null ? null : new ISBN( value );
}

@Override
public ISBN cast(Object value) {
return (ISBN) value;
}

@Override
public boolean isCompatibleWith(ValueBridge<?, ?> other) {
return getClass().equals( other.getClass() );
Expand Down
Expand Up @@ -47,17 +47,6 @@ public interface IdentifierBridge<I> extends AutoCloseable {
*/
I fromDocumentIdentifier(String documentIdentifier, IdentifierBridgeFromDocumentIdentifierContext context);

/**
* Cast an input value to the expected type {@link I}.
* <p>
* Called for values passed to the predicate DSL in particular.
*
* @param value The value to convert.
* @return The checked value.
* @throws RuntimeException If the value does not match the expected type.
*/
I cast(Object value);

default boolean isCompatibleWith(IdentifierBridge<?> other) {
return equals( other );
}
Expand Down
Expand Up @@ -47,17 +47,6 @@ default V fromIndexedValue(F value, ValueBridgeFromIndexedValueContext context)
throw new UnsupportedOperationException( "Bridge " + this + " does not implement fromIndexedValue(...)." );
}

/**
* Cast an input value to the expected type {@link V}.
* <p>
* Called for values passed to the predicate DSL in particular.
*
* @param value The value to convert.
* @return The checked value.
* @throws RuntimeException If the value does not match the expected type.
*/
V cast(Object value);

/**
* Parse an input String to the raw index field value.
*
Expand All @@ -72,9 +61,11 @@ default F parse(String value) {
/**
* @param other Another {@link ValueBridge}, never {@code null}.
* @return {@code true} if the given object is also a {@link ValueBridge}
* that behaves exactly the same as this object, i.e. its {@link #cast(Object)} and {@link #toIndexedValue(Object, ValueBridgeToIndexedValueContext)}
* methods are guaranteed to always return the same value as this object's
* when given the same input. {@code false} otherwise, or when in doubt.
* that behaves exactly the same as this object, i.e. its {@link #toIndexedValue(Object, ValueBridgeToIndexedValueContext)}
* method is guaranteed to accept the same values as this object's
* and to always return the same value as this object's
* when given the same input.
* {@code false} otherwise, or when in doubt.
*/
default boolean isCompatibleWith(ValueBridge<?, ?> other) {
return equals( other );
Expand Down
Expand Up @@ -60,8 +60,10 @@ public <I2> void setBridge(Class<I2> expectedValueType, BeanHolder<? extends Ide
@SuppressWarnings("unchecked") // We check that I2 equals I explicitly using reflection (see above)
BeanHolder<? extends IdentifierBridge<I>> castedBridgeHolder =
(BeanHolder<? extends IdentifierBridge<I>>) bridgeHolder;
@SuppressWarnings("unchecked") // We check that I2 equals I explicitly using reflection (see above)
Class<I> castedExpectedType = (Class<I>) expectedValueType;

this.partialBinding = new PartialBinding<>( castedBridgeHolder );
this.partialBinding = new PartialBinding<>( castedBridgeHolder, castedExpectedType );
}
catch (RuntimeException e) {
abortBridge( new SuppressingCloser( e ), bridgeHolder );
Expand Down Expand Up @@ -102,9 +104,12 @@ private static void abortBridge(AbstractCloser<?, ?> closer, BeanHolder<? extend

private static class PartialBinding<I> {
private final BeanHolder<? extends IdentifierBridge<I>> bridgeHolder;
private final Class<I> expectedValueType;

private PartialBinding(BeanHolder<? extends IdentifierBridge<I>> bridgeHolder) {
private PartialBinding(BeanHolder<? extends IdentifierBridge<I>> bridgeHolder,
Class<I> expectedValueType) {
this.bridgeHolder = bridgeHolder;
this.expectedValueType = expectedValueType;
}

void abort(AbstractCloser<?, ?> closer) {
Expand All @@ -113,7 +118,9 @@ void abort(AbstractCloser<?, ?> closer) {

BoundIdentifierBridge<I> complete(IndexedEntityBindingContext indexedEntityBindingContext) {
indexedEntityBindingContext.idDslConverter(
new PojoIdentifierBridgeToDocumentIdentifierValueConverter<>( bridgeHolder.get() )
new PojoIdentifierBridgeToDocumentIdentifierValueConverter<>(
bridgeHolder.get(), expectedValueType
)
);

return new BoundIdentifierBridge<>( bridgeHolder );
Expand Down
Expand Up @@ -19,25 +19,27 @@
final class PojoIdentifierBridgeToDocumentIdentifierValueConverter<I> implements ToDocumentIdentifierValueConverter<I> {

private final IdentifierBridge<I> bridge;
private final Class<I> expectedValueType;

PojoIdentifierBridgeToDocumentIdentifierValueConverter(IdentifierBridge<I> bridge) {
PojoIdentifierBridgeToDocumentIdentifierValueConverter(IdentifierBridge<I> bridge, Class<I> expectedValueType) {
this.bridge = bridge;
this.expectedValueType = expectedValueType;
}

@Override
public String toString() {
return getClass().getSimpleName() + "[" + bridge + "]";
return getClass().getSimpleName() + "[bridge=" + bridge + ",expectedValueType=" + expectedValueType + "]";
}

@Override
public String convert(I value, ToDocumentIdentifierValueConvertContext context) {
IdentifierBridgeToDocumentIdentifierContext extension = context.extension( PojoIdentifierBridgeContextExtension.INSTANCE );
return bridge.toDocumentIdentifier( bridge.cast( value ), extension );
return bridge.toDocumentIdentifier( value, extension );
}

@Override
public String convertUnknown(Object value, ToDocumentIdentifierValueConvertContext context) {
return convert( bridge.cast( value ), context );
return convert( expectedValueType.cast( value ), context );
}

@Override
Expand All @@ -47,7 +49,8 @@ public boolean isCompatibleWith(ToDocumentIdentifierValueConverter<?> other) {
}
PojoIdentifierBridgeToDocumentIdentifierValueConverter<?> castedOther =
(PojoIdentifierBridgeToDocumentIdentifierValueConverter<?>) other;
return bridge.isCompatibleWith( castedOther.bridge );
return expectedValueType.equals( castedOther.expectedValueType )
&& bridge.isCompatibleWith( castedOther.bridge );
}

private static class PojoIdentifierBridgeContextExtension
Expand Down
Expand Up @@ -15,14 +15,16 @@ final class PojoValueBridgeToDocumentFieldValueConverter<U, V extends U, F> impl
ToDocumentFieldValueConverter<V, F> {

private final ValueBridge<U, F> bridge;
private final Class<U> expectedValueType;

PojoValueBridgeToDocumentFieldValueConverter(ValueBridge<U, F> bridge) {
PojoValueBridgeToDocumentFieldValueConverter(ValueBridge<U, F> bridge, Class<U> expectedValueType) {
this.bridge = bridge;
this.expectedValueType = expectedValueType;
}

@Override
public String toString() {
return getClass().getSimpleName() + "[" + bridge + "]";
return getClass().getSimpleName() + "[bridge=" + bridge + ", expectedValueType=" + expectedValueType + "]";
}

@Override
Expand All @@ -32,7 +34,7 @@ public F convert(V value, ToDocumentFieldValueConvertContext context) {

@Override
public F convertUnknown(Object value, ToDocumentFieldValueConvertContext context) {
return bridge.toIndexedValue( bridge.cast( value ), context.extension( PojoValueBridgeContextExtension.INSTANCE ) );
return bridge.toIndexedValue( expectedValueType.cast( value ), context.extension( PojoValueBridgeContextExtension.INSTANCE ) );
}

@Override
Expand All @@ -42,6 +44,7 @@ public boolean isCompatibleWith(ToDocumentFieldValueConverter<?, ?> other) {
}
PojoValueBridgeToDocumentFieldValueConverter<?, ?, ?> castedOther =
(PojoValueBridgeToDocumentFieldValueConverter<?, ?, ?>) other;
return bridge.isCompatibleWith( castedOther.bridge );
return expectedValueType.equals( castedOther.expectedValueType )
&& bridge.isCompatibleWith( castedOther.bridge );
}
}
Expand Up @@ -152,7 +152,7 @@ private <V2, F> IndexFieldReference<F> createFieldReference(Class<V2> expectedVa

// Then register the bridge itself as a converter to use in the DSL
fieldTypeOptionsStep.dslConverter(
new PojoValueBridgeToDocumentFieldValueConverter<>( bridge )
new PojoValueBridgeToDocumentFieldValueConverter<>( bridge, expectedValueType )
);

// Then register the bridge itself as a converter to use in projections
Expand Down

0 comments on commit 35d2d51

Please sign in to comment.