<commit_before>/*
 * Copyright 2016 <NAME>
 *
 * 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.core.channel.Channel.OutputChannel;
import com.github.dm.jrt.core.channel.IOChannel;
import com.github.dm.jrt.core.channel.ResultChannel;
import com.github.dm.jrt.core.error.RoutineException;
import com.github.dm.jrt.core.invocation.TemplateInvocation;
import com.github.dm.jrt.core.util.ConstantConditions;

import org.jetbrains.annotations.NotNull;

/**
 * Abstract base invocation transforming data coming from an input channel into an output one.
 * <p>
 * Created by davide-maestroni on 06/11/2016.
 *
 * @param <IN>  the input data type.
 * @param <OUT> the output data type.
 */
public abstract class TransformInvocation<IN, OUT> extends TemplateInvocation<IN, OUT> {

    private IOChannel<IN> mInputChannel = null;

    private OutputChannel<OUT> mOutputChannel = null;

    @Override
    public final void onAbort(@NotNull final RoutineException reason) {

        mInputChannel.abort(reason);
    }

    @Override
    public final void onInitialize() {

        final IOChannel<IN> inputChannel = (mInputChannel = JRoutineCore.io().buildChannel());
        mOutputChannel = ConstantConditions.notNull("stream channel", onInputs(inputChannel));
    }

    @Override
    public final void onInput(final IN input, @NotNull final ResultChannel<OUT> result) {

        final OutputChannel<OUT> stream = mOutputChannel;
        if (!stream.isBound()) {
            stream.bind(result);
        }

        mInputChannel.pass(input);
    }

    @Override
    public final void onResult(@NotNull final ResultChannel<OUT> result) {

        mInputChannel.close();
    }

    @Override
    public final void onTerminate() {

        mInputChannel = null;
        mOutputChannel = null;
    }

    /**
     * Transforms the channel providing the input data into the output one.
     *
     * @param channel the input channel.
     * @return the output channel.
     */
    @NotNull
    protected abstract OutputChannel<OUT> onInputs(@NotNull OutputChannel<IN> channel);
}<commit_msg>Cosmetics<commit_after>/*
 * Copyright 2016 <NAME>
 *
 * 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.core.channel.Channel.OutputChannel;
import com.github.dm.jrt.core.channel.IOChannel;
import com.github.dm.jrt.core.channel.ResultChannel;
import com.github.dm.jrt.core.error.RoutineException;
import com.github.dm.jrt.core.invocation.TemplateInvocation;
import com.github.dm.jrt.core.util.ConstantConditions;

import org.jetbrains.annotations.NotNull;

/**
 * Abstract base invocation transforming data coming from an input channel into an output one.
 * <p>
 * Created by davide-maestroni on 06/11/2016.
 *
 * @param <IN>  the input data type.
 * @param <OUT> the output data type.
 */
public abstract class TransformInvocation<IN, OUT> extends TemplateInvocation<IN, OUT> {

    private IOChannel<IN> mInputChannel = null;

    private OutputChannel<OUT> mOutputChannel = null;

    @Override
    public final void onAbort(@NotNull final RoutineException reason) {

        mInputChannel.abort(reason);
    }

    @Override
    public final void onInitialize() throws Exception {

        final IOChannel<IN> inputChannel = (mInputChannel = JRoutineCore.io().buildChannel());
        mOutputChannel = ConstantConditions.notNull("stream channel", onInputs(inputChannel));
    }

    @Override
    public final void onInput(final IN input, @NotNull final ResultChannel<OUT> result) {

        final OutputChannel<OUT> stream = mOutputChannel;
        if (!stream.isBound()) {
            stream.bind(result);
        }

        mInputChannel.pass(input);
    }

    @Override
    public final void onResult(@NotNull final ResultChannel<OUT> result) {

        mInputChannel.close();
    }

    @Override
    public final void onTerminate() {

        mInputChannel = null;
        mOutputChannel = null;
    }

    /**
     * Transforms the channel providing the input data into the output one.
     *
     * @param channel the input channel.
     * @return the output channel.
     * @throws java.lang.Exception if an unexpected error occurs.
     */
    @NotNull
    protected abstract OutputChannel<OUT> onInputs(@NotNull OutputChannel<IN> channel) throws
            Exception;
}
{'fraction_non_alphanumeric': 0.09388888888888888, 'fraction_numerical': 0.005925925925925926, 'mean_word_length': 3.3591606133979015, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5711858', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1473, 'n_words': 477}