io.datakernel.stream.AbstractStreamTransformer_M_1 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of async-streams Show documentation
Show all versions of async-streams Show documentation
Composable asynchronous/reactive streams with powerful data processing capabilities.
The newest version!
/*
* 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.stream;
import io.datakernel.eventloop.Eventloop;
import java.util.ArrayList;
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Represents {@link AbstractStreamProducer} with few {@link AbstractStreamConsumer}.
*
* @param type of sending items
*/
@SuppressWarnings("unchecked")
public abstract class AbstractStreamTransformer_M_1 extends AbstractStreamProducer {
protected final List> inputs = new ArrayList<>();
/**
* Creates a new instance of this object
*
* @param eventloop event loop in which this producer will run
*/
public AbstractStreamTransformer_M_1(Eventloop eventloop) {
super(eventloop);
}
@Override
public void bindDataReceiver() {
super.bindDataReceiver();
for (AbstractStreamConsumer> input : inputs) {
if (input.getUpstream() != null) {
input.getUpstream().bindDataReceiver();
}
}
}
@Override
protected void onInternalError(Exception e) {
closeWithError(e);
sendError(e);
for (AbstractStreamConsumer> input : inputs) {
input.closeUpstreamWithError(e);
}
}
/**
* Action which will take place after changing status to complete
*/
@Override
protected void onClosed() {
closeAllUpstreams();
}
/**
* If consumer has exception, all inputs handle this exception
*/
@Override
protected void onClosedWithError(Exception e) {
downstreamConsumer.onError(e);
for (AbstractStreamConsumer> input : inputs) {
input.onError(e);
}
}
/**
* Adds a new stream consumer to this producer
*
* @param streamConsumer stream consumer for adding
* @param type of stream consumer
* @return new stream consumer
*/
protected > T addInput(T streamConsumer) {
checkNotNull(streamConsumer);
inputs.add(streamConsumer);
return streamConsumer;
}
protected void suspendAllUpstreams() {
for (AbstractStreamConsumer> input : inputs) {
input.suspendUpstream();
}
}
protected void resumeAllUpstreams() {
for (AbstractStreamConsumer> input : inputs) {
input.resumeUpstream();
}
}
protected void closeAllUpstreams() {
for (AbstractStreamConsumer> input : inputs) {
input.closeUpstream();
}
}
protected boolean allUpstreamsEndOfStream() {
for (AbstractStreamConsumer> input : inputs) {
if (input.getUpstream() == null || input.getUpstreamStatus() != END_OF_STREAM)
return false;
}
return true;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy