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

org.hyperledger.fabric.client.DefaultCallOptions Maven / Gradle / Ivy

/*
 * Copyright 2021 IBM All Rights Reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

package org.hyperledger.fabric.client;

import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.MethodDescriptor;
import io.grpc.stub.AbstractStub;
import java.util.function.UnaryOperator;

final class DefaultCallOptions {
    private final UnaryOperator evaluate;
    private final UnaryOperator endorse;
    private final UnaryOperator submit;
    private final UnaryOperator commitStatus;
    private final UnaryOperator chaincodeEvents;
    private final UnaryOperator blockEvents;
    private final UnaryOperator filteredBlockEvents;
    private final UnaryOperator blockAndPrivateDataEvents;

    private DefaultCallOptions(final Builder builder) {
        evaluate = builder.evaluate;
        endorse = builder.endorse;
        submit = builder.submit;
        commitStatus = builder.commitStatus;
        chaincodeEvents = builder.chaincodeEvents;
        blockEvents = builder.blockEvents;
        filteredBlockEvents = builder.filteredBlockEvents;
        blockAndPrivateDataEvents = builder.blockAndPrivateDataEvents;
    }

    public static Builder newBuiler() {
        return new Builder();
    }

    public > T applyEvaluate(final T stub, final UnaryOperator additional) {
        return applyOptions(applyOptions(stub, additional), evaluate);
    }

    public > T applyEndorse(final T stub, final UnaryOperator additional) {
        return applyOptions(applyOptions(stub, additional), endorse);
    }

    public > T applySubmit(final T stub, final UnaryOperator additional) {
        return applyOptions(applyOptions(stub, additional), submit);
    }

    public > T applyCommitStatus(final T stub, final UnaryOperator additional) {
        return applyOptions(applyOptions(stub, additional), commitStatus);
    }

    public > T applyChaincodeEvents(
            final T stub, final UnaryOperator additional) {
        return applyOptions(applyOptions(stub, additional), chaincodeEvents);
    }

    public > T applyBlockEvents(final T stub, final UnaryOperator additional) {
        return applyOptions(applyOptions(stub, additional), blockEvents);
    }

    public > T applyFilteredBlockEvents(
            final T stub, final UnaryOperator additional) {
        return applyOptions(applyOptions(stub, additional), filteredBlockEvents);
    }

    public > T applyBlockAndPrivateDataEvents(
            final T stub, final UnaryOperator additional) {
        return applyOptions(applyOptions(stub, additional), blockAndPrivateDataEvents);
    }

    private static > T applyOptions(final T stub, final UnaryOperator operator) {
        if (operator == null) {
            return stub;
        }

        return stub.withInterceptors(new ClientInterceptor() {
            @Override
            @SuppressWarnings("PMD.GenericsNaming")
            public  ClientCall interceptCall(
                    final MethodDescriptor methodDescriptor,
                    final CallOptions callOptions,
                    final Channel channel) {
                return channel.newCall(methodDescriptor, operator.apply(callOptions));
            }
        });
    }

    public static final class Builder {
        private UnaryOperator evaluate;
        private UnaryOperator endorse;
        private UnaryOperator submit;
        private UnaryOperator commitStatus;
        private UnaryOperator chaincodeEvents;
        private UnaryOperator blockEvents;
        private UnaryOperator filteredBlockEvents;
        private UnaryOperator blockAndPrivateDataEvents;

        private Builder() {
            // Nothing to do
        }

        public Builder evaluate(final UnaryOperator options) {
            evaluate = options;
            return this;
        }

        public Builder endorse(final UnaryOperator options) {
            endorse = options;
            return this;
        }

        public Builder submit(final UnaryOperator options) {
            submit = options;
            return this;
        }

        public Builder commitStatus(final UnaryOperator options) {
            commitStatus = options;
            return this;
        }

        public Builder chaincodeEvents(final UnaryOperator options) {
            chaincodeEvents = options;
            return this;
        }

        public Builder blockEvents(final UnaryOperator options) {
            blockEvents = options;
            return this;
        }

        public Builder filteredBlockEvents(final UnaryOperator options) {
            filteredBlockEvents = options;
            return this;
        }

        public Builder blockAndPrivateDataEvents(final UnaryOperator options) {
            blockAndPrivateDataEvents = options;
            return this;
        }

        public DefaultCallOptions build() {
            return new DefaultCallOptions(this);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy