DATACMNS-89 - ChainingConverter now eagerly returns values.

The ChainingConverter now returns values as soon as one converter in the chain produces an instance that's compatible to the target type. This allows ResourceProcessor clients to provide a Converter to take care of the complete conversion themselves.
This commit is contained in:
Oliver Gierke
2015-12-15 13:34:36 +01:00
parent dbe79f89a9
commit 4af7fe3165
2 changed files with 34 additions and 11 deletions

View File

@@ -127,7 +127,7 @@ public class ResultProcessor {
Assert.notNull(preparingConverter, "Preparing converter must not be null!");
ChainingConverter converter = ChainingConverter.of(preparingConverter).and(this.converter);
ChainingConverter converter = ChainingConverter.of(type.getReturnedType(), preparingConverter).and(this.converter);
if (source instanceof Page && method.isPageQuery()) {
return (T) ((Page<?>) source).map(converter);
@@ -151,6 +151,7 @@ public class ResultProcessor {
@RequiredArgsConstructor(staticName = "of")
private static class ChainingConverter implements Converter<Object, Object> {
private final @NonNull Class<?> targetType;
private final @NonNull Converter<Object, Object> delegate;
/**
@@ -164,11 +165,13 @@ public class ResultProcessor {
Assert.notNull(converter, "Converter must not be null!");
return new ChainingConverter(new Converter<Object, Object>() {
return new ChainingConverter(targetType, new Converter<Object, Object>() {
@Override
public Object convert(Object source) {
return converter.convert(ChainingConverter.this.convert(source));
Object intermediate = ChainingConverter.this.convert(source);
return targetType.isInstance(intermediate) ? intermediate : converter.convert(intermediate);
}
});
}