Skip to content

Commit

Permalink
HSEARCH-3649 Add a method to query the accepted input types of a ToDo…
Browse files Browse the repository at this point in the history
…cumentFieldValueConverter
  • Loading branch information
yrodiere committed Sep 3, 2019
1 parent d1ca6a6 commit 7c611cb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
Expand Up @@ -17,6 +17,20 @@
*/
public interface ToDocumentFieldValueConverter<V, F> {

/**
* Check whether the given type is a valid type for values passed
* to {@link #convert(Object, ToDocumentFieldValueConvertContext)},
* which generally means the given type is a subtype of {@link V}.
* <p>
* This method is generally implemented like this:
* {@code return TheInputType.class.isAssignableFrom( inputTypeCandidate )}.
* @param inputTypeCandidate A candidate type for the input of {@link #convertUnknown(Object, ToDocumentFieldValueConvertContext)}.
* @return {@code true} if values of type {@code inputTypeCandidate}
* may be accepted by {@link #convertUnknown(Object, ToDocumentFieldValueConvertContext)},
* {@code false} otherwise.
*/
boolean isValidInputType(Class<?> inputTypeCandidate);

/**
* @param value The source value to convert.
* @param context A context that can be
Expand Down
Expand Up @@ -23,6 +23,11 @@ public String toString() {
return getClass().getSimpleName() + "[" + valueType + "]";
}

@Override
public boolean isValidInputType(Class<?> inputTypeCandidate) {
return valueType.isAssignableFrom( inputTypeCandidate );
}

@Override
public F convert(F value, ToDocumentFieldValueConvertContext context) {
return value;
Expand Down
Expand Up @@ -22,6 +22,10 @@
public final class ValueWrapper<T> {
public static <T> ToDocumentFieldValueConverter<ValueWrapper<T>, T> toIndexFieldConverter() {
return new ToDocumentFieldValueConverter<ValueWrapper<T>, T>() {
@Override
public boolean isValidInputType(Class<?> inputTypeCandidate) {
return ValueWrapper.class.isAssignableFrom( inputTypeCandidate );
}

@Override
public T convert(ValueWrapper<T> value, ToDocumentFieldValueConvertContext context) {
Expand Down
Expand Up @@ -282,6 +282,11 @@ private static final class IncompatibleType {

private static class IncompatibleToDocumentFieldValueConverter
implements ToDocumentFieldValueConverter<Object, Object> {
@Override
public boolean isValidInputType(Class<?> inputTypeCandidate) {
return Object.class.isAssignableFrom( inputTypeCandidate );
}

@Override
public Object convert(Object value, ToDocumentFieldValueConvertContext context) {
throw new UnsupportedOperationException();
Expand Down
Expand Up @@ -11,13 +11,13 @@
import org.hibernate.search.engine.backend.types.converter.runtime.ToDocumentFieldValueConvertContext;
import org.hibernate.search.mapper.pojo.bridge.ValueBridge;

final class PojoValueBridgeToDocumentFieldValueConverter<U, V extends U, F> implements
ToDocumentFieldValueConverter<V, F> {
final class PojoValueBridgeToDocumentFieldValueConverter<V, F>
implements ToDocumentFieldValueConverter<V, F> {

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

PojoValueBridgeToDocumentFieldValueConverter(ValueBridge<U, F> bridge, Class<U> expectedValueType) {
PojoValueBridgeToDocumentFieldValueConverter(ValueBridge<V, F> bridge, Class<V> expectedValueType) {
this.bridge = bridge;
this.expectedValueType = expectedValueType;
}
Expand All @@ -27,6 +27,11 @@ public String toString() {
return getClass().getSimpleName() + "[bridge=" + bridge + ", expectedValueType=" + expectedValueType + "]";
}

@Override
public boolean isValidInputType(Class<?> inputTypeCandidate) {
return expectedValueType.isAssignableFrom( inputTypeCandidate );
}

@Override
public F convert(V value, ToDocumentFieldValueConvertContext context) {
return bridge.toIndexedValue( value, context.extension( PojoValueBridgeContextExtension.INSTANCE ) );
Expand All @@ -42,8 +47,8 @@ public boolean isCompatibleWith(ToDocumentFieldValueConverter<?, ?> other) {
if ( other == null || !getClass().equals( other.getClass() ) ) {
return false;
}
PojoValueBridgeToDocumentFieldValueConverter<?, ?, ?> castedOther =
(PojoValueBridgeToDocumentFieldValueConverter<?, ?, ?>) other;
PojoValueBridgeToDocumentFieldValueConverter<?, ?> castedOther =
(PojoValueBridgeToDocumentFieldValueConverter<?, ?>) other;
return expectedValueType.equals( castedOther.expectedValueType )
&& bridge.isCompatibleWith( castedOther.bridge );
}
Expand Down

0 comments on commit 7c611cb

Please sign in to comment.