Skip to content

Commit

Permalink
Throw checked exceptions from side effects (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper authored Oct 30, 2023
1 parent 26105f1 commit 79461b9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io.grpc.MethodDescriptor;
import java.time.Duration;
import java.util.Optional;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.NotThreadSafe;

Expand Down Expand Up @@ -104,8 +103,8 @@ <T extends MessageLite> void oneWayCall(
<T extends MessageLite> void delayedCall(
MethodDescriptor<T, ? extends MessageLite> methodDescriptor, T parameter, Duration delay);

/** Shorthand for {@link #sideEffect(TypeTag, Supplier)}. */
default <T> T sideEffect(Class<T> clazz, Supplier<T> action) {
/** Shorthand for {@link #sideEffect(TypeTag, ThrowingSupplier)}. */
default <T> T sideEffect(Class<T> clazz, ThrowingSupplier<T> action) {
return sideEffect(TypeTag.ofClass(clazz), action);
}

Expand All @@ -120,10 +119,10 @@ default <T> T sideEffect(Class<T> clazz, Supplier<T> action) {
* @param <T> type of the return value.
* @return value of the side effect operation.
*/
<T> T sideEffect(TypeTag<T> typeTag, Supplier<T> action);
<T> T sideEffect(TypeTag<T> typeTag, ThrowingSupplier<T> action);

/** Like {@link #sideEffect(TypeTag, Supplier)}, but without returning a value. */
default void sideEffect(Runnable runnable) {
/** Like {@link #sideEffect(TypeTag, ThrowingSupplier)}, but without returning a value. */
default void sideEffect(ThrowingRunnable runnable) {
sideEffect(
TypeTag.VOID,
() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -72,7 +71,7 @@ public <T extends MessageLite> void delayedCall(
}

@Override
public <T> T sideEffect(TypeTag<T> typeTag, Supplier<T> action) {
public <T> T sideEffect(TypeTag<T> typeTag, ThrowingSupplier<T> action) {
CompletableFuture<CompletableFuture<T>> enterFut = new CompletableFuture<>();
syscalls.enterSideEffectBlock(
typeTag,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.restate.sdk.blocking;

/** Like {@link Runnable} but can throw checked exceptions. */
@FunctionalInterface
public interface ThrowingRunnable {

/** Run, potentially throwing an exception. */
void run() throws Throwable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.restate.sdk.blocking;

import java.util.function.Supplier;

/** Like {@link Supplier} but can throw checked exceptions. */
@FunctionalInterface
public interface ThrowingSupplier<T> {

/**
* Get a result, potentially throwing an exception.
*
* @return a result
*/
T get() throws Throwable;
}

0 comments on commit 79461b9

Please sign in to comment.