com.github.dm.jrt.core.DefaultStreamingChannel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jroutine Show documentation
Show all versions of jroutine Show documentation
Parallel programming on the go
/*
* 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.github.dm.jrt.core;
import com.github.dm.jrt.channel.IOChannel;
import com.github.dm.jrt.channel.InputChannel;
import com.github.dm.jrt.channel.OutputChannel;
import com.github.dm.jrt.channel.OutputConsumer;
import com.github.dm.jrt.channel.StreamingChannel;
import com.github.dm.jrt.util.TimeDuration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* Default implementation of a streaming channel.
*
* Created by davide-maestroni on 09/24/2015.
*
* @param the input data type.
* @param the output data type.
*/
class DefaultStreamingChannel implements StreamingChannel {
private final IOChannel mInputChannel;
private final OutputChannel mOutputChannel;
/**
* Constructor.
*
* @param inputChannel the input channel.
* @param outputChannel the output channel.
*/
@SuppressWarnings("ConstantConditions")
DefaultStreamingChannel(@NotNull final IOChannel inputChannel,
@NotNull final OutputChannel outputChannel) {
if (inputChannel == null) {
throw new NullPointerException("the input channel must not be null");
}
if (outputChannel == null) {
throw new NullPointerException("the output channel must not be null");
}
mInputChannel = inputChannel;
mOutputChannel = outputChannel;
}
public boolean abort() {
return mInputChannel.abort() || mOutputChannel.abort();
}
public boolean abort(@Nullable final Throwable reason) {
return mInputChannel.abort(reason) || mOutputChannel.abort(reason);
}
public boolean isEmpty() {
return mInputChannel.isEmpty() && mOutputChannel.isEmpty();
}
public boolean isOpen() {
return mInputChannel.isOpen();
}
@NotNull
public StreamingChannel after(@NotNull final TimeDuration delay) {
mInputChannel.after(delay);
return this;
}
@NotNull
public StreamingChannel after(final long delay, @NotNull final TimeUnit timeUnit) {
mInputChannel.after(delay, timeUnit);
return this;
}
@NotNull
public StreamingChannel now() {
mInputChannel.now();
return this;
}
@NotNull
public StreamingChannel orderByCall() {
mInputChannel.orderByCall();
return this;
}
@NotNull
public StreamingChannel orderByChance() {
mInputChannel.orderByChance();
return this;
}
@NotNull
public StreamingChannel orderByDelay() {
mInputChannel.orderByDelay();
return this;
}
@NotNull
public StreamingChannel pass(@Nullable final OutputChannel extends IN> channel) {
mInputChannel.pass(channel);
return this;
}
@NotNull
public StreamingChannel pass(@Nullable final Iterable extends IN> inputs) {
mInputChannel.pass(inputs);
return this;
}
@NotNull
public StreamingChannel pass(@Nullable final IN input) {
mInputChannel.pass(input);
return this;
}
@NotNull
public StreamingChannel pass(@Nullable final IN... inputs) {
mInputChannel.pass(inputs);
return this;
}
@NotNull
public StreamingChannel afterMax(@NotNull final TimeDuration timeout) {
mOutputChannel.afterMax(timeout);
return this;
}
@NotNull
public StreamingChannel afterMax(final long timeout,
@NotNull final TimeUnit timeUnit) {
mOutputChannel.afterMax(timeout, timeUnit);
return this;
}
@NotNull
public StreamingChannel allInto(@NotNull final Collection super OUT> results) {
mOutputChannel.allInto(results);
return this;
}
@NotNull
public StreamingChannel eventuallyAbort() {
mOutputChannel.eventuallyAbort();
return this;
}
@NotNull
public StreamingChannel eventuallyExit() {
mOutputChannel.eventuallyExit();
return this;
}
@NotNull
public StreamingChannel eventuallyThrow() {
mOutputChannel.eventuallyThrow();
return this;
}
@NotNull
public StreamingChannel immediately() {
mOutputChannel.immediately();
return this;
}
@NotNull
public StreamingChannel passTo(@NotNull final OutputConsumer super OUT> consumer) {
mOutputChannel.passTo(consumer);
return this;
}
@NotNull
public StreamingChannel skip(final int count) {
mOutputChannel.skip(count);
return this;
}
@NotNull
public StreamingChannel close() {
mInputChannel.close();
return this;
}
@NotNull
public StreamingChannel combine(
@NotNull final IOChannel channel) {
mInputChannel.pass(channel).close();
return new DefaultStreamingChannel(channel, mOutputChannel);
}
@NotNull
public StreamingChannel concat(
@NotNull final IOChannel super OUT, AFTER> channel) {
mOutputChannel.passTo(channel);
return new DefaultStreamingChannel(mInputChannel, channel.close());
}
@NotNull
public List all() {
return mOutputChannel.all();
}
public boolean checkComplete() {
return mOutputChannel.checkComplete();
}
public boolean hasNext() {
return mOutputChannel.hasNext();
}
public OUT next() {
return mOutputChannel.next();
}
public boolean isBound() {
return mOutputChannel.isBound();
}
@NotNull
public List next(final int count) {
return mOutputChannel.next(count);
}
@NotNull
public > CHANNEL passTo(
@NotNull final CHANNEL channel) {
return mOutputChannel.passTo(channel);
}
@NotNull
public InputChannel asInput() {
return this;
}
@NotNull
public OutputChannel asOutput() {
return this;
}
public Iterator iterator() {
return mOutputChannel.iterator();
}
public void remove() {
mOutputChannel.remove();
}
}