Skip to content

Commit

Permalink
Merge pull request #32826 from vespa-engine/hakonhall/add-uncheck-of-…
Browse files Browse the repository at this point in the history
…supplier-throwing-interruptedexception

Add uncheck of supplier throwing InterruptedException
  • Loading branch information
freva authored Nov 10, 2024
2 parents edce103 + f5bbbe0 commit c527f3e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 32 deletions.
14 changes: 14 additions & 0 deletions vespajlib/abi-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -3803,6 +3803,19 @@
],
"fields" : [ ]
},
"com.yahoo.yolean.Exceptions$SupplierThrowingInterruptedException" : {
"superClass" : "java.lang.Object",
"interfaces" : [ ],
"attributes" : [
"public",
"interface",
"abstract"
],
"methods" : [
"public abstract java.lang.Object get()"
],
"fields" : [ ]
},
"com.yahoo.yolean.Exceptions" : {
"superClass" : "java.lang.Object",
"interfaces" : [ ],
Expand All @@ -3822,6 +3835,7 @@
"public static java.lang.Object uncheck(com.yahoo.yolean.Exceptions$SupplierThrowingIOException)",
"public static varargs java.lang.Object uncheck(com.yahoo.yolean.Exceptions$SupplierThrowingIOException, java.lang.String, java.lang.String[])",
"public static java.lang.Object uncheckAndIgnore(com.yahoo.yolean.Exceptions$SupplierThrowingIOException, java.lang.Class)",
"public static java.lang.Object uncheckInterrupted(com.yahoo.yolean.Exceptions$SupplierThrowingInterruptedException)",
"public static java.lang.RuntimeException throwUnchecked(java.lang.Throwable)"
],
"fields" : [ ]
Expand Down
79 changes: 47 additions & 32 deletions vespajlib/src/main/java/com/yahoo/yolean/Exceptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public static <T extends Throwable> Optional<T> findCause(Throwable t, Class<T>
return Optional.empty();
}

@FunctionalInterface
public interface RunnableThrowingIOException {
void run() throws IOException;
}

/**
* Wraps any IOException thrown from a runnable in an UncheckedIOException.
*/
Expand All @@ -72,22 +77,6 @@ public static void uncheck(RunnableThrowingIOException runnable) {
}
}

public static void uncheckInterrupted(RunnableThrowingInterruptedException runnable) {
try {
runnable.run();
} catch (InterruptedException e) {
throw new UncheckedInterruptedException(e, false);
}
}

public static void uncheckInterruptedAndRestoreFlag(RunnableThrowingInterruptedException runnable) {
try {
runnable.run();
} catch (InterruptedException e) {
throw new UncheckedInterruptedException(e, true);
}
}

/**
* Wraps any IOException thrown from a runnable in an UncheckedIOException w/message.
*/
Expand Down Expand Up @@ -124,23 +113,21 @@ public static <T extends IOException> void uncheckAndIgnore(RunnableThrowingIOEx
}

@FunctionalInterface
public interface RunnableThrowingIOException {
void run() throws IOException;
public interface FunctionThrowingIOException<T, R> {
R map(T t) throws IOException;
}

@FunctionalInterface public interface RunnableThrowingInterruptedException { void run() throws InterruptedException; }

/**
* Wraps any IOException thrown from a function in an UncheckedIOException.
*/
/** Wraps any IOException thrown from a function in an UncheckedIOException. */
public static <T, R> Function<T, R> uncheck(FunctionThrowingIOException<T, R> function) {
return t -> uncheck(() -> function.map(t));
}
@FunctionalInterface public interface FunctionThrowingIOException<T, R> { R map(T t) throws IOException; }

/**
* Wraps any IOException thrown from a supplier in an UncheckedIOException.
*/
@FunctionalInterface
public interface SupplierThrowingIOException<T> {
T get() throws IOException;
}

/** Wraps any IOException thrown from a supplier in an UncheckedIOException. */
public static <T> T uncheck(SupplierThrowingIOException<T> supplier) {
try {
return supplier.get();
Expand All @@ -149,9 +136,7 @@ public static <T> T uncheck(SupplierThrowingIOException<T> supplier) {
}
}

/**
* Wraps any IOException thrown from a supplier in an UncheckedIOException w/message.
*/
/** Wraps any IOException thrown from a supplier in an UncheckedIOException w/message. */
public static <T> T uncheck(SupplierThrowingIOException<T> supplier, String format, String... args) {
try {
return supplier.get();
Expand Down Expand Up @@ -185,8 +170,38 @@ public static <R, T extends IOException> R uncheckAndIgnore(SupplierThrowingIOEx
}

@FunctionalInterface
public interface SupplierThrowingIOException<T> {
T get() throws IOException;
public interface RunnableThrowingInterruptedException {
void run() throws InterruptedException;
}

@FunctionalInterface
public interface SupplierThrowingInterruptedException<T> {
T get() throws InterruptedException;
}

public static void uncheckInterrupted(RunnableThrowingInterruptedException runnable) {
try {
runnable.run();
} catch (InterruptedException e) {
throw new UncheckedInterruptedException(e, false);
}
}

public static void uncheckInterruptedAndRestoreFlag(RunnableThrowingInterruptedException runnable) {
try {
runnable.run();
} catch (InterruptedException e) {
throw new UncheckedInterruptedException(e, true);
}
}

/** Wraps an InterruptedException in an UncheckedInterruptedException. */
public static <T> T uncheckInterrupted(SupplierThrowingInterruptedException<T> supplier) {
try {
return supplier.get();
} catch (InterruptedException e) {
throw new UncheckedInterruptedException(e);
}
}

/**
Expand Down

0 comments on commit c527f3e

Please sign in to comment.