/*
 * Copyright 2021 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.b2international.snowowl.core.request;

import java.util.Collection;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.b2international.snowowl.core.ServiceProvider;
import com.b2international.snowowl.core.domain.PageableCollectionResource;
import com.b2international.snowowl.core.events.AsyncRequest;
import com.b2international.snowowl.core.events.util.Promise;
import com.b2international.snowowl.eventbus.IEventBus;
import com.google.common.collect.Streams;

/**
 * @since 8.0
 * 
 * @param <B>
 * @param <C>
 * @param <R>
 */
public abstract class SearchPageableCollectionResourceRequestBuilder<B extends SearchPageableCollectionResourceRequestBuilder<B, C, R>, C extends ServiceProvider, R extends PageableCollectionResource<?>> 
		extends SearchResourceRequestBuilder<B, C, R> {

	public final Stream<R> stream(C context) {
		return Streams.stream(new SearchResourceRequestIterator<B, R>(getSelf(), (builder) -> builder.build().execute(context)));
	}
	
	public final Stream<R> streamAsync(ServiceProvider context, Function<B, AsyncRequest<R>> build) {
		return streamAsync(context.service(IEventBus.class), build);
	}
	
	public final Stream<R> streamAsync(IEventBus bus, Function<B, AsyncRequest<R>> build) {
		return Streams.stream(new SearchResourceRequestIterator<B, R>(getSelf(), (builder) -> build.apply(builder).execute(bus).getSync(3, TimeUnit.MINUTES)));
	}
	
	public final <T> Promise<Collection<T>> transformAsync(ServiceProvider context, Function<B, AsyncRequest<R>> build, Function<R, Stream<T>> transform) {
		return transformAsync(context.service(IEventBus.class), build, transform);
	}
	
	public final <T> Promise<Collection<T>> transformAsync(IEventBus bus, Function<B, AsyncRequest<R>> build, Function<R, Stream<T>> transform) {
		return Promise.wrap(() -> {
			return streamAsync(bus, build)
					.map(transform)
					.flatMap(t -> t)
					.collect(Collectors.toList());
		});
	}

}

@@ -13,16 +13,21 @@
 ght 2021
+-2022
  B2i Hea
@@ -1683,33 +1683,272 @@
 (context)));%0A%09%7D%0A
-%09
+%0A%09public final Stream%3CR%3E stream(ServiceProvider context, Function%3CB, AsyncRequest%3CR%3E%3E build) %7B%0A%09%09return Streams.stream(new SearchResourceRequestIterator%3CB, R%3E(getSelf(), (builder) -%3E build.apply(builder).getRequest().execute(context)));%0A%09%7D%0A
 %0A%09public final S
@@ -2093,26 +2093,25 @@
  build);%0A%09%7D%0A
-%09
 %0A
+
 %09public fina
@@ -2340,26 +2340,25 @@
 UTES)));%0A%09%7D%0A
-%09
 %0A
+
 %09public fina
@@ -2872,14 +2872,13 @@
 ;%0A%09%09%7D);%0A
+
 %09%7D%0A
-%0A
 %7D%0A

Introduce streaming helper method that uses the...