inponomarev commented on a change in pull request #9107:
URL: https://github.com/apache/kafka/pull/9107#discussion_r548515181



##########
File path: streams/src/main/java/org/apache/kafka/streams/kstream/Branched.java
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.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 <K> type of record key
+ * @param <V> type of record value
+ */
+public class Branched<K, V> implements NamedOperation<Branched<K, V>> {
+
+    protected final String name;
+    protected final Function<? super KStream<K, V>,
+            ? extends KStream<K, V>> chainFunction;
+    protected final Consumer<? super KStream<K, V>> chainConsumer;
+
+    protected Branched(final String name,
+                       final Function<? super KStream<K, V>, ? extends 
KStream<K, V>> chainFunction,
+                       final Consumer<? super KStream<K, V>> chainConsumer) {
+        this.name = name;
+        this.chainFunction = chainFunction;
+        this.chainConsumer = chainConsumer;
+    }
+
+    /**
+     * Create an instance of {@link Branched} from an existing instance.
+     *
+     * @param branched the instance of {@link Branched} to copy
+     */
+    protected Branched(final Branched<K, V> branched) {
+        this(branched.name, branched.chainFunction, branched.chainConsumer);
+    }
+
+    /**
+     * Configure the instance of {@link Branched} with a branch name postfix.
+     *
+     * @param name the branch name postfix to be used. If {@code null} a 
default branch name postfix will be generated (see
+     *             {@link BranchedKStream} description for details)
+     * @return {@code this}
+     */
+    @Override
+    public Branched<K, V> withName(final String name) {
+        return new Branched<>(name, chainFunction, chainConsumer);
+    }
+
+    /**
+     * Create an instance of {@link Branched} with provided branch name 
postfix.
+     *
+     * @param name the branch name postfix to be used. If {@code null}, a 
default branch name postfix will be generated
+     *             (see {@link BranchedKStream} description for details)
+     * @param <K>  key type
+     * @param <V>  value type
+     * @return a new instance of {@link Branched}
+     */
+    public static <K, V> Branched<K, V> as(final String name) {
+        return new Branched<>(name, null, null);
+    }
+
+    /**
+     * Create an instance of {@link Branched} with provided chain function.
+     *
+     * @param chain A function that will be applied to the branch. If {@code 
null}, the identity
+     *              {@code kStream -> kStream} function will be supposed. If 
this 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 <K>   key type
+     * @param <V>   value type
+     * @return a new instance of {@link Branched}
+     */
+    public static <K, V> Branched<K, V> withFunction(
+            final Function<? super KStream<K, V>,
+                    ? extends KStream<K, V>> chain) {
+        return new Branched<>(null, chain, null);
+    }
+
+    /**
+     * Create an instance of {@link Branched} with provided chain consumer.
+     *
+     * @param chain A consumer to which the branch will be sent. If a non-null 
branch is provided here,
+     *              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). If 
{@code null}, a no-op consumer will be supposed

Review comment:
       OK let me explain how I understood this: 
   
   Let's try to forget about Java type system and look at the problem 
conceptually, from set / category theory point of view
   
   We're using either **function** or **consumer** or **nothing** for branch 
processing. From mathematical point of view, we can consider consumer to be a 
function that maps its domain to the empty set. This explains our decision why 
consumer 'swallows' branch unlike function that 'forwards' its result to the 
resulting map. Then we agreed that the case when **nothing** is provided, like 
in this example
   
   ```java
   BranchedKStream<Integer, String> branch = source.split()
             .branch(isEven)
             .branch(isMultipleOfSeven);
   ```
   is to be interpreted like 
   
   ```java
   BranchedKStream<Integer, String> branch = source.split()
            .branch(isEven, Branched.withFunction(Function.identity()))
            .branch(isMultipleOfSeven, 
Branched.withFunction(Function.identity()));
   ```
   
   Now back to Java: in Java, for `nothing` we have `null`. My original 
intention was to try to emulate `Function|Consumer` union type using 
overloading, but Java didn't allow me to do this dirty trick -- and separate 
`withFunction` and `withConsumer` builder methods appeared 😄 
   
   If I had succeded, it would have made sence to treat `null` uniformely for 
both functions and consumers. 
   
   Hope this explains why I allowed `null`s and always treated it as an 
identity function.
   
   But now I think you're right. I'm ready to **just prohibit passing in nulls 
and throw explicit NPEs for both functions and consumers**. This, at least, can 
spare us from further debates :-) What do you think?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to