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

com.github.dm.jrt.function.AccumulateInvocation 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.channel.ResultChannel;
import com.github.dm.jrt.invocation.Invocation;
import com.github.dm.jrt.invocation.InvocationFactory;
import com.github.dm.jrt.invocation.TemplateInvocation;

import org.jetbrains.annotations.NotNull;

import static com.github.dm.jrt.function.Functions.wrapBiFunction;

/**
 * Invocation implementation accumulating the result returned by a bi-function instance.
 * 

* Created by davide-maestroni on 10/18/2015. * * @param the input data type. */ class AccumulateInvocation extends TemplateInvocation { private final BiFunction mFunction; private IN mAccumulated; private boolean mIsFirst; /** * Constructor. * * @param function the bi-function instance. */ private AccumulateInvocation( @NotNull final BiFunction function) { mFunction = function; } /** * Builds and returns a new accumulating invocation factory backed by the specified bi-function * instance. * * @param function the bi-function instance. * @param the input data type. * @return the invocation factory. */ @NotNull public static InvocationFactory functionFactory( @NotNull final BiFunction function) { return new AccumulateInvocationFactory(wrapBiFunction(function)); } @Override public void onInitialize() { mIsFirst = true; } @Override public void onInput(final IN input, @NotNull final ResultChannel result) { if (mIsFirst) { mIsFirst = false; mAccumulated = input; } else { mAccumulated = mFunction.apply(mAccumulated, input); } } @Override public void onResult(@NotNull final ResultChannel result) { result.pass(mAccumulated); } @Override public void onTerminate() { mAccumulated = null; } /** * Class implementing an accumulating invocation factory. * * @param the input data type. */ private static class AccumulateInvocationFactory extends InvocationFactory { private final BiFunction mFunction; /** * Constructor. * * @param function the bi-function instance. */ private AccumulateInvocationFactory( @NotNull final BiFunctionWrapper function) { if (!function.hasStaticContext()) { throw new IllegalArgumentException( "the bi-function class must have a static context: " + function.getClass()); } mFunction = function; } @Override public int hashCode() { return mFunction.hashCode(); } @Override public boolean equals(final Object o) { if (this == o) { return true; } if (!(o instanceof AccumulateInvocationFactory)) { return false; } final AccumulateInvocationFactory that = (AccumulateInvocationFactory) o; return mFunction.equals(that.mFunction); } @NotNull @Override public Invocation newInvocation() { return new AccumulateInvocation(mFunction); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy