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

com.swirlds.common.wiring.transformers.WireTransformer Maven / Gradle / Ivy

Go to download

Swirlds is a software platform designed to build fully-distributed applications that harness the power of the cloud without servers. Now you can develop applications with fairness in decision making, speed, trust and reliability, at a fraction of the cost of traditional server-based platforms.

There is a newer version: 0.56.6
Show newest version
/*
 * Copyright (C) 2023-2024 Hedera Hashgraph, LLC
 *
 * 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.swirlds.common.wiring.transformers;

import com.swirlds.common.wiring.model.WiringModel;
import com.swirlds.common.wiring.schedulers.TaskScheduler;
import com.swirlds.common.wiring.schedulers.builders.TaskSchedulerType;
import com.swirlds.common.wiring.wires.input.BindableInputWire;
import com.swirlds.common.wiring.wires.input.InputWire;
import com.swirlds.common.wiring.wires.output.OutputWire;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Objects;
import java.util.function.Function;

/**
 * Transforms data on a wire from one type to another.
 *
 * @param  the input type
 * @param  the output type
 */
public class WireTransformer {

    private final BindableInputWire inputWire;
    private final OutputWire outputWire;

    /**
     * Constructor. Immediately binds the transformation function to the input wire.
     *
     * @param model                the wiring model containing this output channel
     * @param transformerName      the name of the transformer
     * @param transformerInputName the label for the input wire going into the transformer
     * @param transformer          an object that transforms from type A to type B. If this method returns null then no
     *                             data is forwarded. This method must be very fast. Putting large amounts of work into
     *                             this transformer violates the intended usage pattern of the wiring framework and may
     *                             result in very poor system performance.
     */
    public WireTransformer(
            @NonNull final WiringModel model,
            @NonNull final String transformerName,
            @NonNull final String transformerInputName,
            @NonNull final Function transformer) {
        Objects.requireNonNull(transformer);

        final TaskScheduler taskScheduler = model.schedulerBuilder(transformerName)
                .withType(TaskSchedulerType.DIRECT_THREADSAFE)
                .build()
                .cast();

        inputWire = taskScheduler.buildInputWire(transformerInputName);
        inputWire.bind(transformer);
        outputWire = taskScheduler.getOutputWire();
    }

    /**
     * Constructor. Requires the input wire to be bound later.
     *
     * @param model                the wiring model containing this output channel
     * @param transformerName      the name of the transformer
     * @param transformerInputName the label for the input wire going into the transformer
     */
    public WireTransformer(
            @NonNull final WiringModel model,
            @NonNull final String transformerName,
            @NonNull final String transformerInputName) {

        final TaskScheduler taskScheduler = model.schedulerBuilder(transformerName)
                .withType(TaskSchedulerType.DIRECT_THREADSAFE)
                .build()
                .cast();

        inputWire = taskScheduler.buildInputWire(transformerInputName);
        outputWire = taskScheduler.getOutputWire();
    }

    /**
     * Get the input wire for this transformer.
     *
     * @return the input wire
     */
    @NonNull
    public InputWire getInputWire() {
        return inputWire;
    }

    /**
     * Get the output wire for this transformer.
     *
     * @return the output wire
     */
    @NonNull
    public OutputWire getOutputWire() {
        return outputWire;
    }

    /**
     * Bind the transformation function to the input wire. Do not call this if the transformation function was provided
     * in the constructor. Must be called prior to use if the transformation function was not provided in the
     * constructor.
     *
     * @param transformer the transformation function
     */
    public void bind(@NonNull final Function transformer) {
        inputWire.bind(transformer);
    }
}