Skip to content

Commit

Permalink
refactor: reorder methods 🚞
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Apr 27, 2024
1 parent 1b68ec3 commit d4d4522
Show file tree
Hide file tree
Showing 7 changed files with 776 additions and 770 deletions.
137 changes: 58 additions & 79 deletions cffu-core/src/main/java/io/foldright/cffu/Cffu.java
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,6 @@ public Cffu<T> exceptionallyAsync(Function<Throwable, ? extends T> fn, Executor
*/
public Cffu<T> orTimeout(long timeout, TimeUnit unit) {
checkMinimalStage();

CompletableFutureUtils.orTimeout(cf, timeout, unit);
return this;
}
Expand All @@ -1304,7 +1303,6 @@ public Cffu<T> orTimeout(long timeout, TimeUnit unit) {
*/
public Cffu<T> completeOnTimeout(@Nullable T value, long timeout, TimeUnit unit) {
checkMinimalStage();

CompletableFutureUtils.completeOnTimeout(cf, value, timeout, unit);
return this;
}
Expand Down Expand Up @@ -1436,6 +1434,64 @@ public Cffu<T> exceptionallyComposeAsync(
return reset0(CompletableFutureUtils.exceptionallyComposeAsync(cf, fn, executor));
}

/**
* Returns a new Cffu that, when this cffu completes either normally or exceptionally,
* is executed with this cffu's result and exception as arguments to the supplied function.
* <p>
* When this cffu is complete, the given function is invoked with the result (or {@code null} if none)
* and the exception (or {@code null} if none) of this cffu as arguments,
* and the function's result is used to complete the returned cffu.
*
* @param fn the function to use to compute the value of the returned Cffu
* @param <U> the function's return type
* @return the new Cffu
*/
@CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `whenComplete`")
@Override
public <U> Cffu<U> handle(BiFunction<? super T, Throwable, ? extends U> fn) {
return reset0(cf.handle(fn));
}

/**
* Returns a new Cffu that, when this cffu completes either normally or exceptionally,
* is executed using {@link #defaultExecutor()},
* with this cffu's result and exception as arguments to the supplied function.
* <p>
* When this Cffu is complete, the given function is invoked with the result (or {@code null} if none)
* and the exception (or {@code null} if none) of this Cffu as arguments,
* and the function's result is used to complete the returned Cffu.
*
* @param fn the function to use to compute the value of the returned Cffu
* @param <U> the function's return type
* @return the new Cffu
*/
@CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `whenCompleteAsync`")
@Override
public <U> Cffu<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn) {
return handleAsync(fn, fac.defaultExecutor());
}

/**
* Returns a new Cffu that, when this cffu completes either normally or exceptionally,
* is executed using the supplied executor, with this cffu's result and exception
* as arguments to the supplied function.
* <p>
* When this cffu is complete, the given function is invoked with the result (or {@code null} if none)
* and the exception (or {@code null} if none) of this cffu as arguments,
* and the function's result is used to complete the returned cffu.
*
* @param fn the function to use to compute the value of the returned cffu
* @param executor the executor to use for asynchronous execution
* @param <U> the function's return type
* @return the new Cffu
*/
@CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `whenCompleteAsync`")
@Override
public <U> Cffu<U> handleAsync(
BiFunction<? super T, Throwable, ? extends U> fn, Executor executor) {
return reset0(cf.handleAsync(fn, executor));
}

/**
* Returns a new Cffu with the same result or exception as this stage,
* that executes the given action when this stage completes.
Expand Down Expand Up @@ -1573,64 +1629,6 @@ public Cffu<T> peekAsync(BiConsumer<? super T, ? super Throwable> action, Execut
return this;
}

/**
* Returns a new Cffu that, when this cffu completes either normally or exceptionally,
* is executed with this cffu's result and exception as arguments to the supplied function.
* <p>
* When this cffu is complete, the given function is invoked with the result (or {@code null} if none)
* and the exception (or {@code null} if none) of this cffu as arguments,
* and the function's result is used to complete the returned cffu.
*
* @param fn the function to use to compute the value of the returned Cffu
* @param <U> the function's return type
* @return the new Cffu
*/
@CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `whenComplete`")
@Override
public <U> Cffu<U> handle(BiFunction<? super T, Throwable, ? extends U> fn) {
return reset0(cf.handle(fn));
}

/**
* Returns a new Cffu that, when this cffu completes either normally or exceptionally,
* is executed using {@link #defaultExecutor()},
* with this cffu's result and exception as arguments to the supplied function.
* <p>
* When this Cffu is complete, the given function is invoked with the result (or {@code null} if none)
* and the exception (or {@code null} if none) of this Cffu as arguments,
* and the function's result is used to complete the returned Cffu.
*
* @param fn the function to use to compute the value of the returned Cffu
* @param <U> the function's return type
* @return the new Cffu
*/
@CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `whenCompleteAsync`")
@Override
public <U> Cffu<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn) {
return handleAsync(fn, fac.defaultExecutor());
}

/**
* Returns a new Cffu that, when this cffu completes either normally or exceptionally,
* is executed using the supplied executor, with this cffu's result and exception
* as arguments to the supplied function.
* <p>
* When this cffu is complete, the given function is invoked with the result (or {@code null} if none)
* and the exception (or {@code null} if none) of this cffu as arguments,
* and the function's result is used to complete the returned cffu.
*
* @param fn the function to use to compute the value of the returned cffu
* @param executor the executor to use for asynchronous execution
* @param <U> the function's return type
* @return the new Cffu
*/
@CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `whenCompleteAsync`")
@Override
public <U> Cffu<U> handleAsync(
BiFunction<? super T, Throwable, ? extends U> fn, Executor executor) {
return reset0(cf.handleAsync(fn, executor));
}

////////////////////////////////////////////////////////////////////////////////
//# Read(explicitly) methods of CompletableFuture
//
Expand Down Expand Up @@ -1673,7 +1671,6 @@ public <U> Cffu<U> handleAsync(
@Override
public T get() throws InterruptedException, ExecutionException {
checkMinimalStage();

return cf.get();
}

Expand All @@ -1699,7 +1696,6 @@ public T get() throws InterruptedException, ExecutionException {
@Override
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
checkMinimalStage();

return cf.get(timeout, unit);
}

Expand All @@ -1724,7 +1720,6 @@ public T get(long timeout, TimeUnit unit) throws InterruptedException, Execution
@Nullable
public T join() {
checkMinimalStage();

return cf.join();
}

Expand Down Expand Up @@ -1767,7 +1762,6 @@ public T join() {
@Nullable
public T join(long timeout, TimeUnit unit) {
checkMinimalStage();

return CompletableFutureUtils.join(cf, timeout, unit);
}

Expand All @@ -1790,7 +1784,6 @@ public T join(long timeout, TimeUnit unit) {
@Nullable
public T getNow(T valueIfAbsent) {
checkMinimalStage();

return cf.getNow(valueIfAbsent);
}

Expand All @@ -1814,7 +1807,6 @@ public T getNow(T valueIfAbsent) {
@Override
public T resultNow() {
checkMinimalStage();

return CompletableFutureUtils.resultNow(cf);
}

Expand All @@ -1832,7 +1824,6 @@ public T resultNow() {
@Override
public Throwable exceptionNow() {
checkMinimalStage();

return CompletableFutureUtils.exceptionNow(cf);
}

Expand All @@ -1848,7 +1839,6 @@ public Throwable exceptionNow() {
@Override
public boolean isDone() {
checkMinimalStage();

return cf.isDone();
}

Expand All @@ -1862,7 +1852,6 @@ public boolean isDone() {
@Contract(pure = true)
public boolean isCompletedExceptionally() {
checkMinimalStage();

return cf.isCompletedExceptionally();
}

Expand All @@ -1876,7 +1865,6 @@ public boolean isCompletedExceptionally() {
@Override
public boolean isCancelled() {
checkMinimalStage();

return cf.isCancelled();
}

Expand All @@ -1899,7 +1887,6 @@ public boolean isCancelled() {
@Override
public Future.State state() {
checkMinimalStage();

return cf.state();
}

Expand All @@ -1916,7 +1903,6 @@ public Future.State state() {
@Contract(pure = true)
public CffuState cffuState() {
checkMinimalStage();

return CompletableFutureUtils.state(cf);
}

Expand All @@ -1938,7 +1924,6 @@ public CffuState cffuState() {
*/
public boolean complete(@Nullable T value) {
checkMinimalStage();

return cf.complete(value);
}

Expand All @@ -1963,7 +1948,6 @@ public Cffu<T> completeAsync(Supplier<? extends T> supplier) {
*/
public Cffu<T> completeAsync(Supplier<? extends T> supplier, Executor executor) {
checkMinimalStage();

CompletableFutureUtils.completeAsync(cf, supplier, executor);
return this;
}
Expand All @@ -1976,7 +1960,6 @@ public Cffu<T> completeAsync(Supplier<? extends T> supplier, Executor executor)
*/
public boolean completeExceptionally(Throwable ex) {
checkMinimalStage();

return cf.completeExceptionally(ex);
}

Expand All @@ -1992,7 +1975,6 @@ public boolean completeExceptionally(Throwable ex) {
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
checkMinimalStage();

return cf.cancel(mayInterruptIfRunning);
}

Expand Down Expand Up @@ -2188,7 +2170,6 @@ public CompletableFuture<T> cffuUnwrap() {
@Contract(pure = true)
public int getNumberOfDependents() {
checkMinimalStage();

return cf.getNumberOfDependents();
}

Expand Down Expand Up @@ -2216,7 +2197,6 @@ public int getNumberOfDependents() {
public void obtrudeValue(@Nullable T value) {
checkMinimalStage();
checkForbidObtrudeMethods();

cf.obtrudeValue(value);
}

Expand All @@ -2233,7 +2213,6 @@ public void obtrudeValue(@Nullable T value) {
public void obtrudeException(Throwable ex) {
checkMinimalStage();
checkForbidObtrudeMethods();

cf.obtrudeException(ex);
}

Expand Down
Loading

0 comments on commit d4d4522

Please sign in to comment.