com.github.dm.jrt.function.AbstractFunctionalRoutine 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.function;
import com.github.dm.jrt.builder.InvocationConfiguration;
import com.github.dm.jrt.builder.InvocationConfiguration.Builder;
import com.github.dm.jrt.builder.InvocationConfiguration.Configurable;
import com.github.dm.jrt.channel.ResultChannel;
import com.github.dm.jrt.core.AbstractRoutine;
import com.github.dm.jrt.core.JRoutine;
import com.github.dm.jrt.invocation.DelegatingInvocation.DelegationType;
import com.github.dm.jrt.invocation.FilterInvocation;
import com.github.dm.jrt.routine.Routine;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import static com.github.dm.jrt.function.Functions.consumerFilter;
import static com.github.dm.jrt.function.Functions.functionFilter;
/**
* Abstract implementation of a functional routine.
*
* Created by davide-maestroni on 10/16/2015.
*
* @param the input data type.
* @param the output data type.
*/
abstract class AbstractFunctionalRoutine extends AbstractRoutine
implements FunctionalRoutine, Configurable> {
private InvocationConfiguration mConfiguration = InvocationConfiguration.DEFAULT_CONFIGURATION;
/**
* Constructor.
*/
AbstractFunctionalRoutine() {
super(InvocationConfiguration.DEFAULT_CONFIGURATION);
}
@NotNull
public Builder extends FunctionalRoutine> invocations() {
return new Builder>(this, mConfiguration);
}
@NotNull
@SuppressWarnings("ConstantConditions")
public FunctionalRoutine setConfiguration(
@NotNull final InvocationConfiguration configuration) {
if (configuration == null) {
throw new NullPointerException("the invocation configuration must not be null");
}
mConfiguration = configuration;
return this;
}
@NotNull
public FunctionalRoutine thenAsyncAccumulate(
@NotNull final BiFunction super OUT, ? super OUT, ? extends OUT> function) {
return andThenAccumulate(function, DelegationType.ASYNC);
}
@NotNull
public FunctionalRoutine thenAsyncFilter(
@NotNull final Predicate super OUT> predicate) {
return andThenFilter(predicate, DelegationType.ASYNC);
}
@NotNull
public FunctionalRoutine thenAsyncMap(
@NotNull final BiConsumer super OUT, ? super ResultChannel> consumer) {
return thenAsyncMap(consumerFilter(consumer));
}
@NotNull
public FunctionalRoutine thenAsyncMap(
@NotNull final FilterInvocation super OUT, AFTER> invocation) {
return thenAsyncMap(
JRoutine.on(invocation).invocations().with(mConfiguration).set().buildRoutine());
}
@NotNull
public FunctionalRoutine thenAsyncMap(
@NotNull final Function super OUT, AFTER> function) {
return thenAsyncMap(functionFilter(function));
}
@NotNull
public FunctionalRoutine thenAsyncMap(
@NotNull final Routine super OUT, AFTER> routine) {
return andThen(routine, DelegationType.ASYNC);
}
@NotNull
public FunctionalRoutine thenAsyncReduce(
@NotNull final BiConsumer super List extends OUT>, ? super ResultChannel>
consumer) {
return thenAsyncMap(JRoutine.on(Functions.consumerFactory(consumer))
.invocations()
.with(mConfiguration)
.set()
.buildRoutine());
}
@NotNull
public FunctionalRoutine thenAsyncReduce(
@NotNull final Function super List extends OUT>, AFTER> function) {
return thenAsyncMap(JRoutine.on(Functions.functionFactory(function))
.invocations()
.with(mConfiguration)
.set()
.buildRoutine());
}
@NotNull
public FunctionalRoutine thenFlatLift(
@NotNull final Function super FunctionalRoutine, ? extends
FunctionalRoutine> function) {
return function.apply(this);
}
@NotNull
public FunctionalRoutine thenParallelFilter(
@NotNull final Predicate super OUT> predicate) {
return andThenFilter(predicate, DelegationType.PARALLEL);
}
@NotNull
public FunctionalRoutine thenParallelMap(
@NotNull final BiConsumer super OUT, ? super ResultChannel> consumer) {
return thenParallelMap(consumerFilter(consumer));
}
@NotNull
public FunctionalRoutine thenParallelMap(
@NotNull final FilterInvocation super OUT, AFTER> invocation) {
return thenParallelMap(
JRoutine.on(invocation).invocations().with(mConfiguration).set().buildRoutine());
}
@NotNull
public FunctionalRoutine thenParallelMap(
@NotNull final Function super OUT, AFTER> function) {
return thenParallelMap(functionFilter(function));
}
@NotNull
public FunctionalRoutine thenParallelMap(
@NotNull final Routine super OUT, AFTER> routine) {
return andThen(routine, DelegationType.PARALLEL);
}
@NotNull
public FunctionalRoutine thenSyncAccumulate(
@NotNull final BiFunction super OUT, ? super OUT, ?
extends OUT> function) {
return andThenAccumulate(function, DelegationType.SYNC);
}
@NotNull
public FunctionalRoutine thenSyncFilter(
@NotNull final Predicate super OUT> predicate) {
return andThenFilter(predicate, DelegationType.SYNC);
}
@NotNull
public FunctionalRoutine thenSyncMap(
@NotNull final BiConsumer super OUT, ? super ResultChannel> consumer) {
return thenSyncMap(consumerFilter(consumer));
}
@NotNull
public FunctionalRoutine thenSyncMap(
@NotNull final FilterInvocation super OUT, AFTER> invocation) {
return thenSyncMap(
JRoutine.on(invocation).invocations().with(mConfiguration).set().buildRoutine());
}
@NotNull
public FunctionalRoutine thenSyncMap(
@NotNull final Function super OUT, AFTER> function) {
return thenSyncMap(functionFilter(function));
}
@NotNull
public FunctionalRoutine thenSyncMap(
@NotNull final Routine super OUT, AFTER> routine) {
return andThen(routine, DelegationType.SYNC);
}
@NotNull
public FunctionalRoutine thenSyncReduce(
@NotNull final BiConsumer super List extends OUT>, ? super ResultChannel>
consumer) {
return thenSyncMap(JRoutine.on(Functions.consumerFactory(consumer))
.invocations()
.with(mConfiguration)
.set()
.buildRoutine());
}
@NotNull
public FunctionalRoutine thenSyncReduce(
@NotNull final Function super List extends OUT>, AFTER> function) {
return thenSyncMap(JRoutine.on(Functions.functionFactory(function))
.invocations()
.with(mConfiguration)
.set()
.buildRoutine());
}
/**
* Concatenates a functional routine based on the specified instance to this one.
*
* @param routine the routine instance.
* @param delegationType the delegation type.
* @param the concatenation output type.
* @return the concatenated functional routine.
*/
@NotNull
protected abstract FunctionalRoutine andThen(
@NotNull Routine super OUT, AFTER> routine, @NotNull DelegationType delegationType);
@NotNull
private FunctionalRoutine andThenAccumulate(
@NotNull final BiFunction super OUT, ? super OUT, ?
extends OUT> function, @NotNull final DelegationType delegationType) {
return andThen(JRoutine.on(AccumulateInvocation.functionFactory(function))
.invocations()
.with(mConfiguration)
.set()
.buildRoutine(), delegationType);
}
@NotNull
private FunctionalRoutine andThenFilter(
@NotNull final Predicate super OUT> predicate,
@NotNull final DelegationType delegationType) {
return andThen(JRoutine.on(Functions.predicateFilter(predicate))
.invocations()
.with(mConfiguration)
.set()
.buildRoutine(), delegationType);
}
}