dcapwell commented on code in PR #17: URL: https://github.com/apache/cassandra-accord/pull/17#discussion_r1110266944
########## accord-core/src/main/java/accord/utils/async/AsyncChains.java: ########## @@ -0,0 +1,432 @@ +/* + * 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 accord.utils.async; + +import java.util.List; +import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.BiConsumer; +import java.util.function.BiFunction; +import java.util.function.Function; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; + +import static accord.utils.async.AsyncChainCombiner.Reduce; + +public abstract class AsyncChains<V> implements AsyncChain<V> +{ + static class Immediate<V> implements AsyncChain<V> + { + static class FailureHolder + { + final Throwable cause; + FailureHolder(Throwable cause) + { + this.cause = cause; + } + } + + final private Object value; + private Immediate(V success) { this.value = success; } + private Immediate(Throwable failure) { this.value = new FailureHolder(failure); } + + @Override + public <T> AsyncChain<T> map(Function<? super V, ? extends T> mapper) + { + if (value != null && value.getClass() == FailureHolder.class) + return (AsyncChain<T>) this; + return new Immediate<>(mapper.apply((V) value)); + } + + @Override + public <T> AsyncChain<T> flatMap(Function<? super V, ? extends AsyncChain<T>> mapper) + { + if (value != null && value.getClass() == FailureHolder.class) + return (AsyncChain<T>) this; + return mapper.apply((V) value); + } + + @Override + public AsyncChain<V> addCallback(BiConsumer<? super V, Throwable> callback) + { + if (value == null || value.getClass() != FailureHolder.class) + callback.accept((V) value, null); + else + callback.accept(null, ((FailureHolder)value).cause); + return this; + } + + @Override + public void begin(BiConsumer<? super V, Throwable> callback) + { + addCallback(callback); + } + } + + public abstract static class Head<V> extends AsyncChains<V> implements BiConsumer<V, Throwable> + { + protected Head() + { + super(null); + next = this; + } + + void begin() + { + begin(next); + } Review Comment: This is still an issue, so created a test case to show it; `Head` still allows! ``` @Test void headRejectsSecondBegin() { AsyncChain<String> chain = new AsyncChains.Head<>() { @Override public void begin(BiConsumer<? super String, Throwable> callback) { callback.accept("success", null); } }; chain.begin((i1, i2) -> {}); assertThrows(() -> chain.begin((i1, i2) -> {})); } @Test void chainRejectsSecondBegin() { AsyncChain<String> chain = new AsyncChains.Head<>() { @Override public void begin(BiConsumer<? super String, Throwable> callback) { callback.accept("success", null); } }; chain = chain.map(s -> s + " is true"); chain.begin((i1, i2) -> {}); AsyncChain<String> finalChain = chain; assertThrows(() -> finalChain.begin((i1, i2) -> {})); } private static void assertThrows(Runnable fn) { try { fn.run(); Assertions.fail("Should have been rejected"); } catch (AssertionError e) { if ("Should have been rejected".equals(e.getMessage())) throw e; } catch (Throwable t) { // expected } } ``` -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

