Skip to content

Commit

Permalink
refactor: declare QA annotations on package instead of classes, compl…
Browse files Browse the repository at this point in the history
…ement QA annotations in `DelayExecutionHelpers.java` 🦺
  • Loading branch information
oldratlee committed Jun 12, 2024
1 parent 6d2ebbd commit 6ffa1fc
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 41 deletions.
10 changes: 4 additions & 6 deletions cffu-core/src/main/java/io/foldright/cffu/Cffu.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.foldright.cffu;

import edu.umd.cs.findbugs.annotations.*;
import edu.umd.cs.findbugs.annotations.CheckReturnValue;
import edu.umd.cs.findbugs.annotations.Nullable;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.jetbrains.annotations.Blocking;
import org.jetbrains.annotations.Contract;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.concurrent.*;
import java.util.function.*;

Expand All @@ -24,14 +25,11 @@
* @see CompletionStage
* @see CompletableFuture
*/
@ParametersAreNonnullByDefault
@ReturnValuesAreNonnullByDefault
public final class Cffu<T> implements Future<T>, CompletionStage<T> {
@NonNull
private final CffuFactory fac;

private final boolean isMinimalStage;
@NonNull

private final CompletableFuture<T> cf;

Cffu(CffuFactory cffuFactory, boolean isMinimalStage, CompletableFuture<T> cf) {
Expand Down
8 changes: 0 additions & 8 deletions cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package io.foldright.cffu;

import edu.umd.cs.findbugs.annotations.CheckReturnValue;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault;
import io.foldright.cffu.tuple.Tuple2;
import io.foldright.cffu.tuple.Tuple3;
import io.foldright.cffu.tuple.Tuple4;
import io.foldright.cffu.tuple.Tuple5;
import org.jetbrains.annotations.Contract;

import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.ThreadSafe;
import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -43,10 +40,7 @@
* @see CompletableFuture
*/
@ThreadSafe
@ParametersAreNonnullByDefault
@ReturnValuesAreNonnullByDefault
public final class CffuFactory {
@NonNull
private final Executor defaultExecutor;

private final boolean forbidObtrudeMethods;
Expand Down Expand Up @@ -193,7 +187,6 @@ public Cffu<Void> runAsync(Runnable action, Executor executor) {
* @see CompletableFuture#supplyAsync(Supplier)
*/
@CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `runAsync`")
@SuppressWarnings("BoundedWildcard")
public <T> Cffu<T> supplyAsync(Supplier<T> supplier) {
return supplyAsync(supplier, defaultExecutor);
}
Expand All @@ -209,7 +202,6 @@ public <T> Cffu<T> supplyAsync(Supplier<T> supplier) {
* @see CompletableFuture#supplyAsync(Supplier, Executor)
*/
@CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `runAsync`")
@SuppressWarnings("BoundedWildcard")
public <T> Cffu<T> supplyAsync(Supplier<T> supplier, Executor executor) {
return create(CompletableFuture.supplyAsync(supplier, executor));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package io.foldright.cffu;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault;
import io.foldright.cffu.spi.ExecutorWrapperProvider;
import org.jetbrains.annotations.Contract;

import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.ThreadSafe;
import java.util.List;
import java.util.ServiceLoader;
Expand All @@ -25,10 +22,7 @@
* @see Cffu
*/
@ThreadSafe
@ParametersAreNonnullByDefault
@ReturnValuesAreNonnullByDefault
public final class CffuFactoryBuilder {
@NonNull
private final Executor defaultExecutor;

private volatile boolean forbidObtrudeMethods = false;
Expand Down
4 changes: 0 additions & 4 deletions cffu-core/src/main/java/io/foldright/cffu/CffuState.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package io.foldright.cffu;

import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault;
import org.jetbrains.annotations.Contract;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.concurrent.Future;

import static java.util.Objects.requireNonNull;
Expand All @@ -17,8 +15,6 @@
* @see CompletableFutureUtils#state(Future)
* @see Cffu#cffuState()
*/
@ParametersAreNonnullByDefault
@ReturnValuesAreNonnullByDefault
public enum CffuState {
/**
* The task has not completed.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package io.foldright.cffu;

import edu.umd.cs.findbugs.annotations.Nullable;
import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault;
import io.foldright.cffu.tuple.Tuple2;
import io.foldright.cffu.tuple.Tuple3;
import io.foldright.cffu.tuple.Tuple4;
import io.foldright.cffu.tuple.Tuple5;
import org.jetbrains.annotations.Blocking;
import org.jetbrains.annotations.Contract;

import javax.annotation.ParametersAreNonnullByDefault;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
Expand All @@ -28,8 +26,6 @@
*
* @author Jerry Lee (oldratlee at gmail dot com)
*/
@ParametersAreNonnullByDefault
@ReturnValuesAreNonnullByDefault
public final class CompletableFutureUtils {
////////////////////////////////////////////////////////////////////////////////
//# allOf*/mostResultsOfSuccess methods
Expand Down Expand Up @@ -1355,6 +1351,7 @@ C peekAsync(C cf, BiConsumer<? super T, ? super Throwable> action, Executor exec
}

@Nullable
@SuppressWarnings("SameReturnValue")
private static <T> T reportException(String msg, Throwable ex) {
StringWriter sw = new StringWriter(4096);
PrintWriter writer = new PrintWriter(sw);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
// below code is copied from CompletableFuture with small adoption
////////////////////////////////////////////////////////////////////////////////

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.util.concurrent.*;
import java.util.function.BiConsumer;
import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;


/**
* Singleton delay scheduler, used only for starting and cancelling tasks
Expand All @@ -34,16 +36,16 @@ static ScheduledFuture<?> delay(Runnable command, long delay, TimeUnit unit) {
* @return a Future can be used to cancel the delayed task(timeout CF)
* @see FutureCanceller
*/
public static ScheduledFuture<?> delayToTimoutCf(CompletableFuture<?> cf, long delay, TimeUnit unit) {
static ScheduledFuture<?> delayToTimoutCf(CompletableFuture<?> cf, long delay, TimeUnit unit) {
return delay(new CfTimeout(cf), delay, unit);
}

/**
* @return a Future can be used to cancel the delayed task(complete CF)
* @see FutureCanceller
*/
public static <T> ScheduledFuture<?> delayToCompleteCf(
CompletableFuture<? super T> cf, T value, long delay, TimeUnit unit) {
static <T> ScheduledFuture<?> delayToCompleteCf(
CompletableFuture<? super T> cf, @Nullable T value, long delay, TimeUnit unit) {
return delay(new CfCompleter<>(cf, value), delay, unit);
}

Expand Down Expand Up @@ -78,7 +80,7 @@ private static class DelayerHolder {

private static final class DaemonThreadFactory implements ThreadFactory {
@Override
public Thread newThread(@NonNull Runnable r) {
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
t.setName(THREAD_NAME_OF_CFFU_DELAY_SCHEDULER);
Expand Down Expand Up @@ -108,10 +110,9 @@ final class DelayedExecutor implements Executor {
}

@Override
public void execute(@NonNull Runnable r) {
Delayer.delay(new TaskSubmitter(executor, r), delay, unit);
public void execute(Runnable r) {
Delayer.delay(new TaskSubmitter(executor, requireNonNull(r, "runnable is null")), delay, unit);
}

}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -154,7 +155,7 @@ final class CfTimeout implements Runnable {

@Override
public void run() {
if (cf != null && !cf.isDone())
if (!cf.isDone())
cf.completeExceptionally(new TimeoutException());
}
}
Expand All @@ -167,16 +168,17 @@ public void run() {
@SuppressWarnings("JavadocReference")
final class CfCompleter<T> implements Runnable {
private final CompletableFuture<? super T> cf;
@Nullable
private final T value;

CfCompleter(CompletableFuture<? super T> cf, T value) {
CfCompleter(CompletableFuture<? super T> cf, @Nullable T value) {
this.cf = cf;
this.value = value;
}

@Override
public void run() {
if (cf != null) cf.complete(value);
cf.complete(value);
}
}

Expand All @@ -191,14 +193,15 @@ public void run() {
*/
@SuppressWarnings("JavadocReference")
final class FutureCanceller implements BiConsumer<Object, Throwable> {
@Nullable
private final Future<?> f;

FutureCanceller(Future<?> f) {
this.f = f;
this.f = requireNonNull(f);
}

@Override
public void accept(Object ignore, Throwable ex) {
public void accept(Object ignore, @Nullable Throwable ex) {
if (ex == null && f != null && !f.isDone())
f.cancel(false);
}
Expand All @@ -210,7 +213,9 @@ public void accept(Object ignore, Throwable ex) {
@SuppressFBWarnings("SE_BAD_FIELD")
final class CfCompleterBySupplier<T> extends ForkJoinTask<Void>
implements Runnable, CompletableFuture.AsynchronousCompletionTask {
@Nullable
private CompletableFuture<? super T> dep;
@Nullable
private Supplier<? extends T> fn;

CfCompleterBySupplier(CompletableFuture<? super T> dep, Supplier<? extends T> fn) {
Expand Down Expand Up @@ -257,7 +262,9 @@ public void run() {
@SuppressFBWarnings("SE_BAD_FIELD")
final class CfExCompleterBySupplier extends ForkJoinTask<Void>
implements Runnable, CompletableFuture.AsynchronousCompletionTask {
@Nullable
private CompletableFuture<?> dep;
@Nullable
private Supplier<? extends Throwable> fn;

CfExCompleterBySupplier(CompletableFuture<?> dep, Supplier<? extends Throwable> fn) {
Expand Down
9 changes: 9 additions & 0 deletions cffu-core/src/main/java/io/foldright/cffu/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@
* @see io.foldright.cffu.CffuFactoryBuilder
* @see io.foldright.cffu.CompletableFutureUtils
*/
@DefaultAnnotation(NonNull.class)
@ParametersAreNonnullByDefault
@ReturnValuesAreNonnullByDefault
package io.foldright.cffu;

import edu.umd.cs.findbugs.annotations.DefaultAnnotation;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault;

import javax.annotation.ParametersAreNonnullByDefault;

0 comments on commit 6ffa1fc

Please sign in to comment.