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

org.apache.kafka.streams.kstream.Branched Maven / Gradle / Ivy

There is a newer version: 3.9.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.kafka.streams.kstream;

import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * The {@code Branched} class is used to define the optional parameters when building branches with
 * {@link BranchedKStream}.
 *
 * @param  type of record key
 * @param  type of record value
 */
public class Branched implements NamedOperation> {

    protected final String name;
    protected final Function, ? extends KStream> chainFunction;
    protected final Consumer> chainConsumer;

    protected Branched(final String name,
                       final Function, ? extends KStream> chainFunction,
                       final Consumer> chainConsumer) {
        this.name = name;
        this.chainFunction = chainFunction;
        this.chainConsumer = chainConsumer;
    }

    /**
     * Create an instance of {@code Branched} with provided branch name suffix.
     *
     * @param name the branch name suffix to be used (see {@link BranchedKStream} description for details)
     * @param   key type
     * @param   value type
     * @return a new instance of {@code Branched}
     */
    public static  Branched as(final String name) {
        Objects.requireNonNull(name, "name cannot be null");
        return new Branched<>(name, null, null);
    }

    /**
     * Create an instance of {@code Branched} with provided chain function.
     *
     * @param chain A function that will be applied to the branch. If the provided function returns
     *              {@code null}, its result is ignored, otherwise it is added to the {@code Map} returned
     *              by {@link BranchedKStream#defaultBranch()} or {@link BranchedKStream#noDefaultBranch()} (see
     *              {@link BranchedKStream} description for details).
     * @param    key type
     * @param    value type
     * @return a new instance of {@code Branched}
     */
    public static  Branched withFunction(
            final Function, ? extends KStream> chain) {
        Objects.requireNonNull(chain, "chain function cannot be null");
        return new Branched<>(null, chain, null);
    }

    /**
     * Create an instance of {@code Branched} with provided chain consumer.
     *
     * @param chain A consumer to which the branch will be sent. If a consumer is provided,
     *              the respective branch will not be added to the resulting {@code Map} returned
     *              by {@link BranchedKStream#defaultBranch()} or {@link BranchedKStream#noDefaultBranch()} (see
     *              {@link BranchedKStream} description for details).
     * @param    key type
     * @param    value type
     * @return a new instance of {@code Branched}
     */
    public static  Branched withConsumer(final Consumer> chain) {
        Objects.requireNonNull(chain, "chain consumer cannot be null");
        return new Branched<>(null, null, chain);
    }

    /**
     * Create an instance of {@code Branched} with provided chain function and branch name suffix.
     *
     * @param chain A function that will be applied to the branch. If the provided function returns
     *              {@code null}, its result is ignored, otherwise it is added to the {@code Map} returned
     *              by {@link BranchedKStream#defaultBranch()} or {@link BranchedKStream#noDefaultBranch()} (see
     *              {@link BranchedKStream} description for details).
     * @param name  the branch name suffix to be used. If {@code null}, a default branch name suffix will be generated
     *              (see {@link BranchedKStream} description for details)
     * @param    key type
     * @param    value type
     * @return a new instance of {@code Branched}
     */
    public static  Branched withFunction(
            final Function, ? extends KStream> chain, final String name) {
        Objects.requireNonNull(chain, "chain function cannot be null");
        return new Branched<>(name, chain, null);
    }

    /**
     * Create an instance of {@code Branched} with provided chain consumer and branch name suffix.
     *
     * @param chain A consumer to which the branch will be sent. If a non-null consumer is provided,
     *              the respective branch will not be added to the resulting {@code Map} returned
     *              by {@link BranchedKStream#defaultBranch()} or {@link BranchedKStream#noDefaultBranch()} (see
     *              {@link BranchedKStream} description for details).
     * @param name  the branch name suffix to be used. If {@code null}, a default branch name suffix will be generated
     *              (see {@link BranchedKStream} description for details)
     * @param    key type
     * @param    value type
     * @return a new instance of {@code Branched}
     */
    public static  Branched withConsumer(final Consumer> chain,
                                                     final String name) {
        Objects.requireNonNull(chain, "chain consumer cannot be null");
        return new Branched<>(name, null, chain);
    }

    /**
     * Create an instance of {@code Branched} from an existing instance.
     *
     * @param branched the instance of {@code Branched} to copy
     */
    protected Branched(final Branched branched) {
        this(branched.name, branched.chainFunction, branched.chainConsumer);
    }

    /**
     * Configure the instance of {@code Branched} with a branch name suffix.
     *
     * @param name the branch name suffix to be used. If {@code null} a default branch name suffix will be generated (see
     *             {@link BranchedKStream} description for details)
     * @return {@code this}
     */
    @Override
    public Branched withName(final String name) {
        Objects.requireNonNull(name, "name cannot be null");
        return new Branched<>(name, chainFunction, chainConsumer);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy