All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.datakernel.datastream.StreamConsumerWithResult Maven / Gradle / Ivy

/*
 * Copyright (C) 2015 SoftIndex LLC.
 *
 * 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 io.datakernel.datastream;

import io.datakernel.promise.Promise;

import java.util.function.Function;

public final class StreamConsumerWithResult {
	private final StreamConsumer consumer;
	private final Promise result;

	private StreamConsumerWithResult(StreamConsumer consumer, Promise result) {
		this.consumer = consumer;
		this.result = result;
	}

	public static  StreamConsumerWithResult of(StreamConsumer consumer, Promise result) {
		return new StreamConsumerWithResult<>(consumer, result);
	}

	protected StreamConsumerWithResult sanitize() {
		return new StreamConsumerWithResult<>(consumer,
				consumer.getAcknowledgement().combine(result.whenException(consumer::close), ($, v) -> v).post());
	}

	public  StreamConsumerWithResult transform(
			Function, StreamConsumer> consumerTransformer,
			Function, Promise> resultTransformer) {
		return new StreamConsumerWithResult<>(
				consumerTransformer.apply(consumer),
				resultTransformer.apply(result));
	}

	public  StreamConsumerWithResult transformConsumer(Function, StreamConsumer> consumerTransformer) {
		return transform(consumerTransformer, Function.identity());
	}

	public  StreamConsumerWithResult transformResult(Function, Promise> resultTransformer) {
		return transform(Function.identity(), resultTransformer);
	}

	public static  StreamConsumerWithResult ofPromise(Promise> promise) {
		return of(
				StreamConsumer.ofPromise(promise.map(StreamConsumerWithResult::getConsumer)),
				promise.then(StreamConsumerWithResult::getResult));
	}

	public StreamConsumer getConsumer() {
		return consumer;
	}

	public Promise getResult() {
		return result;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy