use of org.springframework.integration.util.FunctionIterator in project spring-integration by spring-projects.
the class AbstractMessageSplitter method handleRequestMessage.
@Override
@SuppressWarnings("unchecked")
protected final Object handleRequestMessage(Message<?> message) {
Object result = this.splitMessage(message);
// return null if 'null'
if (result == null) {
return null;
}
boolean reactive = getOutputChannel() instanceof ReactiveStreamsSubscribableChannel;
setAsync(reactive);
Iterator<Object> iterator = null;
Flux<Object> flux = null;
final int sequenceSize;
if (result instanceof Iterable<?>) {
Iterable<Object> iterable = (Iterable<Object>) result;
sequenceSize = obtainSizeIfPossible(iterable);
if (reactive) {
flux = Flux.fromIterable(iterable);
} else {
iterator = iterable.iterator();
}
} else if (result.getClass().isArray()) {
Object[] items = (Object[]) result;
sequenceSize = items.length;
if (reactive) {
flux = Flux.fromArray(items);
} else {
iterator = Arrays.asList(items).iterator();
}
} else if (result instanceof Iterator<?>) {
Iterator<Object> iter = (Iterator<Object>) result;
sequenceSize = obtainSizeIfPossible(iter);
if (reactive) {
flux = Flux.fromIterable(() -> iter);
} else {
iterator = iter;
}
} else if (result instanceof Stream<?>) {
Stream<Object> stream = ((Stream<Object>) result);
sequenceSize = 0;
if (reactive) {
flux = Flux.fromStream(stream);
} else {
iterator = stream.iterator();
}
} else if (result instanceof Publisher<?>) {
Publisher<Object> publisher = (Publisher<Object>) result;
sequenceSize = 0;
if (reactive) {
flux = Flux.from(publisher);
} else {
iterator = Flux.from((Publisher<Object>) result).toIterable().iterator();
}
} else {
sequenceSize = 1;
if (reactive) {
flux = Flux.just(result);
} else {
iterator = Collections.singleton(result).iterator();
}
}
if (iterator != null && !iterator.hasNext()) {
return null;
}
Map<String, Object> messageHeaders = message.getHeaders();
if (willAddHeaders(message)) {
messageHeaders = new HashMap<>(messageHeaders);
addHeaders(message, messageHeaders);
}
final Map<String, Object> headers = messageHeaders;
final Object correlationId = message.getHeaders().getId();
final AtomicInteger sequenceNumber = new AtomicInteger(1);
Function<Object, AbstractIntegrationMessageBuilder<?>> messageBuilderFunction = object -> createBuilder(object, headers, correlationId, sequenceNumber.getAndIncrement(), sequenceSize);
if (reactive) {
return flux.map(messageBuilderFunction);
} else {
return new FunctionIterator<>(iterator, messageBuilderFunction);
}
}
Aggregations