io.proximax.utils.TimeoutUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-chain-xipfs-sdk Show documentation
Show all versions of java-chain-xipfs-sdk Show documentation
Official ProximaX P2P Storage SDK Library in Java.
The newest version!
package io.proximax.utils;
import io.proximax.exceptions.RetrievalTimeoutException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;
/**
* The utility class for handling timeouts
*/
public class TimeoutUtils {
private TimeoutUtils() {}
/**
* Timeout retrieval
* @param supplier the supplier
* @param timeout timeout length
* @param timeUnit time unit of timeout
* @param the class the supplier provide
* @return the retrieved type
*/
public static T get(Supplier supplier, long timeout, TimeUnit timeUnit) {
try {
return CompletableFuture.supplyAsync(supplier).get(timeout, timeUnit);
} catch (TimeoutException e) {
throw new RetrievalTimeoutException("Retrieval timed out", e);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}