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

com.github.dm.jrt.function.AbstractFunctionalRoutine Maven / Gradle / Ivy

There is a newer version: 5.9.0
Show newest version
/*
 * 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> 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 function) { return andThenAccumulate(function, DelegationType.ASYNC); } @NotNull public FunctionalRoutine thenAsyncFilter( @NotNull final com.github.dm.jrt.function.Predicate predicate) { return andThenFilter(predicate, DelegationType.ASYNC); } @NotNull public FunctionalRoutine thenAsyncMap( @NotNull final BiConsumer> consumer) { return thenAsyncMap(consumerFilter(consumer)); } @NotNull public FunctionalRoutine thenAsyncMap( @NotNull final FilterInvocation invocation) { return thenAsyncMap( JRoutine.on(invocation).invocations().with(mConfiguration).set().buildRoutine()); } @NotNull public FunctionalRoutine thenAsyncMap( @NotNull final Function function) { return thenAsyncMap(functionFilter(function)); } @NotNull public FunctionalRoutine thenAsyncMap( @NotNull final Routine routine) { return andThen(routine, DelegationType.ASYNC); } @NotNull public FunctionalRoutine thenAsyncReduce( @NotNull final BiConsumer, ? super ResultChannel> consumer) { return thenAsyncMap(JRoutine.on(Functions.consumerFactory(consumer)) .invocations() .with(mConfiguration) .set() .buildRoutine()); } @NotNull public FunctionalRoutine thenAsyncReduce( @NotNull final Function, AFTER> function) { return thenAsyncMap(JRoutine.on(Functions.functionFactory(function)) .invocations() .with(mConfiguration) .set() .buildRoutine()); } @NotNull public FunctionalRoutine thenFlatLift( @NotNull final Function, ? extends FunctionalRoutine> function) { return function.apply(this); } @NotNull public FunctionalRoutine thenParallelFilter( @NotNull final Predicate predicate) { return andThenFilter(predicate, DelegationType.PARALLEL); } @NotNull public FunctionalRoutine thenParallelMap( @NotNull final BiConsumer> consumer) { return thenParallelMap(consumerFilter(consumer)); } @NotNull public FunctionalRoutine thenParallelMap( @NotNull final FilterInvocation invocation) { return thenParallelMap( JRoutine.on(invocation).invocations().with(mConfiguration).set().buildRoutine()); } @NotNull public FunctionalRoutine thenParallelMap( @NotNull final Function function) { return thenParallelMap(functionFilter(function)); } @NotNull public FunctionalRoutine thenParallelMap( @NotNull final Routine routine) { return andThen(routine, DelegationType.PARALLEL); } @NotNull public FunctionalRoutine thenSyncAccumulate( @NotNull final BiFunction function) { return andThenAccumulate(function, DelegationType.SYNC); } @NotNull public FunctionalRoutine thenSyncFilter( @NotNull final Predicate predicate) { return andThenFilter(predicate, DelegationType.SYNC); } @NotNull public FunctionalRoutine thenSyncMap( @NotNull final BiConsumer> consumer) { return thenSyncMap(consumerFilter(consumer)); } @NotNull public FunctionalRoutine thenSyncMap( @NotNull final FilterInvocation invocation) { return thenSyncMap( JRoutine.on(invocation).invocations().with(mConfiguration).set().buildRoutine()); } @NotNull public FunctionalRoutine thenSyncMap( @NotNull final Function function) { return thenSyncMap(functionFilter(function)); } @NotNull public FunctionalRoutine thenSyncMap( @NotNull final Routine routine) { return andThen(routine, DelegationType.SYNC); } @NotNull public FunctionalRoutine thenSyncReduce( @NotNull final BiConsumer, ? super ResultChannel> consumer) { return thenSyncMap(JRoutine.on(Functions.consumerFactory(consumer)) .invocations() .with(mConfiguration) .set() .buildRoutine()); } @NotNull public FunctionalRoutine thenSyncReduce( @NotNull final Function, 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 routine, @NotNull DelegationType delegationType); @NotNull private FunctionalRoutine andThenAccumulate( @NotNull final BiFunction 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 predicate, @NotNull final DelegationType delegationType) { return andThen(JRoutine.on(Functions.predicateFilter(predicate)) .invocations() .with(mConfiguration) .set() .buildRoutine(), delegationType); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy