Skip to content

Commit

Permalink
suppress warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
tonivade committed Mar 9, 2024
1 parent fcea80b commit 537d72e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ private Option<Try<T>> current(Consumer1<? super Try<? extends T>> consumer) {
return getValue();
}

@SuppressWarnings("NullAway")
private Try<T> get() {
return getValue().getOrElse(
() -> Try.failure(new IllegalStateException("promise not completed")));
return TryOf.narrowK(reference.get());
}

private Option<Try<T>> getValue() {
return Option.of(reference.get()).map(TryOf::narrowK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ private static <R, E, A> Promise<Either<E, A>> runAsync(@Nullable R env, PureIO<

@SuppressWarnings("unchecked")
private static <R, E, F, G, A, B, C> Promise<Either<E, A>> runAsync(
R env, Kind<Kind<Kind<PureIO_, R>, E>, A> current, PureIOConnection connection, CallStack<R, E, A> stack, Promise<Either<E, A>> promise) {
@Nullable R env, Kind<Kind<Kind<PureIO_, R>, E>, A> current, PureIOConnection connection, CallStack<R, E, A> stack, Promise<Either<E, A>> promise) {
while (true) {
try {
current = unwrap(env, current, stack, identity());
Expand Down Expand Up @@ -584,7 +584,7 @@ private static <R, E, F, G, A, B, C> Promise<Either<E, A>> runAsync(

@SuppressWarnings("unchecked")
private static <R, E, F, A, B> Kind<Kind<Kind<PureIO_, R>, E>, A> unwrap(
R env, Kind<Kind<Kind<PureIO_, R>, E>, A> current, CallStack<R, F, B> stack,
@Nullable R env, Kind<Kind<Kind<PureIO_, R>, E>, A> current, CallStack<R, F, B> stack,
Function1<Kind<Kind<Kind<PureIO_, R>, E>, ? extends A>, Kind<Kind<Kind<PureIO_, R>, F>, ? extends B>> next) {
while (true) {
if (current instanceof Failure) {
Expand All @@ -601,7 +601,7 @@ private static <R, E, F, A, B> Kind<Kind<Kind<PureIO_, R>, E>, A> unwrap(
stack.add(recover.mapper.andThen(next));
current = (PureIO<R, E, A>) recover.current;
} else if (current instanceof AccessM<R, E, A> accessM) {
current = accessM.function.apply(env).fix(PureIOOf::narrowK);
current = accessM(env, accessM);
} else if (current instanceof Suspend<R, E, A> suspend) {
current = suspend.lazy.get().fix(PureIOOf::narrowK);
} else if (current instanceof Delay<R, E, A> delay) {
Expand All @@ -616,12 +616,19 @@ private static <R, E, F, A, B> Kind<Kind<Kind<PureIO_, R>, E>, A> unwrap(
}
}

private static <R, E, A> Promise<Either<E, A>> executeAsync(R env, Async<R, E, A> current, PureIOConnection connection, Promise<Either<E, A>> promise) {
@SuppressWarnings("NullAway")
static <R, E, A> Kind<Kind<Kind<PureIO_, R>, E>, A> accessM(@Nullable R env, AccessM<R, E, A> accessM) {
Kind<Kind<Kind<PureIO_, R>, E>, A> current;
current = accessM.function.apply(env).fix(PureIOOf::narrowK);
return current;
}

private static <R, E, A> Promise<Either<E, A>> executeAsync(@Nullable R env, Async<R, E, A> current, PureIOConnection connection, Promise<Either<E, A>> promise) {
if (connection.isCancellable() && !connection.updateState(StateIO::startingNow).isRunnable()) {
return promise.cancel();
}

connection.setCancelToken(current.callback.apply(env, result -> promise.tryComplete(result.map(EitherOf::narrowK))));
setCancelToken(env, current, connection, promise);

promise.thenRun(() -> connection.setCancelToken(UNIT));

Expand All @@ -632,6 +639,12 @@ private static <R, E, A> Promise<Either<E, A>> executeAsync(R env, Async<R, E, A
return promise;
}

@SuppressWarnings("NullAway")
private static <R, E, A> void setCancelToken(
@Nullable R env, Async<R, E, A> current, PureIOConnection connection, Promise<Either<E, A>> promise) {
connection.setCancelToken(current.callback.apply(env, result -> promise.tryComplete(result.map(EitherOf::narrowK))));
}

final class Pure<R, E, A> implements PureIO<R, E, A> {

private final A value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public String toString() {
return String.format("Ref(%s)", value.get());
}

@SuppressWarnings("NullAway")
private A safeGet() {
return Option.of(value.get()).getOrElseThrow();
return value.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static Concurrent<Kind<EIO_, Throwable>> concurrent() {
static Concurrent<Kind<EIO_, Throwable>> concurrent(Executor executor) {
return EIOConcurrent.instance(executor);
}

static <E> Runtime<Kind<EIO_, E>> runtime() {
return EIORuntime.INSTANCE;
}
Expand Down Expand Up @@ -188,21 +188,21 @@ default EIO<Throwable, Unit> sleep(Duration duration) {
interface EIOAsync extends Async<Kind<EIO_, Throwable>>, EIOMonadDefer {

EIOAsync INSTANCE = new EIOAsync() {};

@Override
default <A> EIO<Throwable, A> asyncF(Function1<Consumer1<? super Try<? extends A>>, Kind<Kind<EIO_, Throwable>, Unit>> consumer) {
return EIO.cancellable(cb -> consumer.andThen(EIOOf::narrowK).apply(e -> cb.accept(Try.success(e.toEither()))));
}
}

interface EIOConcurrent extends EIOAsync, Concurrent<Kind<EIO_, Throwable>> {

static EIOConcurrent instance(Executor executor) {
return () -> executor;
}

Executor executor();

@Override
default <A, B> EIO<Throwable, Either<Tuple2<A, Fiber<Kind<EIO_, Throwable>, B>>, Tuple2<Fiber<Kind<EIO_, Throwable>, A>, B>>> racePair(
Kind<Kind<EIO_, Throwable>, ? extends A> fa, Kind<Kind<EIO_, Throwable>, ? extends B> fb) {
Expand All @@ -211,21 +211,21 @@ default <A, B> EIO<Throwable, Either<Tuple2<A, Fiber<Kind<EIO_, Throwable>, B>>,

@Override
default <A> EIO<Throwable, Fiber<Kind<EIO_, Throwable>, A>> fork(Kind<Kind<EIO_, Throwable>, ? extends A> value) {
// TODO Auto-generated method stub
return null;
EIO<Throwable, A> fix = value.fix(EIOOf::narrowK);
return fix.fork();
}
}

interface EIORuntime<E> extends Runtime<Kind<EIO_, E>> {

@SuppressWarnings("rawtypes")
EIORuntime INSTANCE = new EIORuntime() {};

@Override
default <T> T run(Kind<Kind<EIO_, E>, T> value) {
return value.fix(toEIO()).safeRunSync().getRight();
}

@Override
default <T> Sequence<T> run(Sequence<Kind<Kind<EIO_, E>, T>> values) {
return run(EIO.traverse(values.map(EIOOf::<E, T>narrowK)));
Expand All @@ -235,7 +235,7 @@ default <T> Sequence<T> run(Sequence<Kind<Kind<EIO_, E>, T>> values) {
default <T> Future<T> parRun(Kind<Kind<EIO_, E>, T> value, Executor executor) {
return value.fix(toEIO()).runAsync().map(Either::get);
}

@Override
default <T> Future<Sequence<T>> parRun(Sequence<Kind<Kind<EIO_, E>, T>> values, Executor executor) {
return parRun(EIO.traverse(values.map(EIOOf::<E, T>narrowK)), executor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public String toString() {
return String.format("Ref(%s)", value.get());
}

@SuppressWarnings("NullAway")
private A safeGet() {
return Option.of(value.get()).getOrElseThrow();
return value.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public String toString() {
return String.format("Reference(%s)", value.get());
}

@SuppressWarnings("NullAway")
private A safeGet() {
return Option.of(value.get()).getOrElseThrow();
return value.get();
}
}

0 comments on commit 537d72e

Please sign in to comment.