com.gh.bmd.jrt.routine.DefaultStandaloneChannel 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.gh.bmd.jrt.routine;
import com.gh.bmd.jrt.builder.RoutineConfiguration;
import com.gh.bmd.jrt.builder.RoutineConfiguration.OrderType;
import com.gh.bmd.jrt.builder.RoutineConfiguration.RunnerType;
import com.gh.bmd.jrt.channel.OutputChannel;
import com.gh.bmd.jrt.channel.OutputConsumer;
import com.gh.bmd.jrt.channel.StandaloneChannel;
import com.gh.bmd.jrt.log.Logger;
import com.gh.bmd.jrt.routine.DefaultResultChannel.AbortHandler;
import com.gh.bmd.jrt.runner.Runners;
import com.gh.bmd.jrt.time.TimeDuration;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* Default implementation of a standalone channel.
*
* Created by davide on 10/24/14.
*
* @param the data type.
*/
class DefaultStandaloneChannel implements StandaloneChannel {
private final DefaultStandaloneInput mInputChannel;
private final DefaultStandaloneOutput mOutputChannel;
/**
* Constructor.
*
* @param configuration the routine configuration.
*/
DefaultStandaloneChannel(@Nonnull final RoutineConfiguration configuration) {
final Logger logger = Logger.newLogger(configuration, this);
final ChannelAbortHandler abortHandler = new ChannelAbortHandler();
final DefaultResultChannel inputChannel =
new DefaultResultChannel(configuration, abortHandler,
configuration.getRunnerOr(Runners.sharedRunner()),
logger);
abortHandler.setChannel(inputChannel);
mInputChannel = new DefaultStandaloneInput(inputChannel);
mOutputChannel = new DefaultStandaloneOutput(inputChannel.getOutput());
logger.dbg("building standalone channel with configuration: %s", configuration);
warn(logger, configuration);
}
private static void warn(@Nonnull final Logger logger,
@Nonnull final RoutineConfiguration configuration) {
final RunnerType syncRunner = configuration.getSyncRunnerOr(null);
if (syncRunner != null) {
logger.wrn("the specified synchronous runner type will be ignored: %s", syncRunner);
}
final int maxInvocations = configuration.getMaxInvocationsOr(RoutineConfiguration.DEFAULT);
if (maxInvocations != RoutineConfiguration.DEFAULT) {
logger.wrn("the specified maximum running invocations will be ignored: %d",
maxInvocations);
}
final int coreInvocations =
configuration.getCoreInvocationsOr(RoutineConfiguration.DEFAULT);
if (coreInvocations != RoutineConfiguration.DEFAULT) {
logger.wrn("the specified core invocations will be ignored: %d", coreInvocations);
}
final TimeDuration availableTimeout = configuration.getAvailTimeoutOr(null);
if (availableTimeout != null) {
logger.wrn("the specified available invocation timeout will be ignored: %s",
availableTimeout);
}
final OrderType inputOrder = configuration.getInputOrderOr(null);
if (inputOrder != null) {
logger.wrn("the specified input order will be ignored: %s", inputOrder);
}
final int inputSize = configuration.getInputSizeOr(RoutineConfiguration.DEFAULT);
if (inputSize != RoutineConfiguration.DEFAULT) {
logger.wrn("the specified maximum input size will be ignored: %d", inputSize);
}
final TimeDuration inputTimeout = configuration.getInputTimeoutOr(null);
if (inputTimeout != null) {
logger.wrn("the specified input timeout will be ignored: %s", inputTimeout);
}
}
@Nonnull
@Override
public StandaloneInput input() {
return mInputChannel;
}
@Nonnull
@Override
public StandaloneOutput output() {
return mOutputChannel;
}
/**
* Abort handler used to close the input channel on abort.
*/
private static class ChannelAbortHandler implements AbortHandler {
private DefaultResultChannel> mChannel;
@Override
public void onAbort(@Nullable final Throwable reason, final long delay,
@Nonnull final TimeUnit timeUnit) {
mChannel.close(reason);
}
public void setChannel(@Nonnull final DefaultResultChannel> channel) {
mChannel = channel;
}
}
/**
* Default implementation of a standalone channel input.
*
* @param the input data type.
*/
private static class DefaultStandaloneInput implements StandaloneInput {
private final DefaultResultChannel mChannel;
/**
* Constructor.
*
* @param wrapped the wrapped result channel.
*/
private DefaultStandaloneInput(@Nonnull final DefaultResultChannel wrapped) {
mChannel = wrapped;
}
@Override
public boolean abort() {
return mChannel.abort();
}
@Nonnull
@Override
public StandaloneInput after(@Nonnull final TimeDuration delay) {
mChannel.after(delay);
return this;
}
@Nonnull
@Override
public StandaloneInput after(final long delay, @Nonnull final TimeUnit timeUnit) {
mChannel.after(delay, timeUnit);
return this;
}
@Nonnull
@Override
public StandaloneInput now() {
mChannel.now();
return this;
}
@Nonnull
@Override
public StandaloneInput pass(@Nullable final OutputChannel channel) {
mChannel.pass(channel);
return this;
}
@Nonnull
@Override
public StandaloneInput pass(@Nullable final Iterable extends INPUT> inputs) {
mChannel.pass(inputs);
return this;
}
@Nonnull
@Override
public StandaloneInput pass(@Nullable final INPUT input) {
mChannel.pass(input);
return this;
}
@Nonnull
@Override
public StandaloneInput pass(@Nullable final INPUT... inputs) {
mChannel.pass(inputs);
return this;
}
@Override
public void close() {
mChannel.close();
}
@Override
public boolean abort(@Nullable final Throwable reason) {
return mChannel.abort(reason);
}
@Override
public boolean isOpen() {
return mChannel.isOpen();
}
}
/**
* Default implementation of a standalone channel output.
*
* @param