io.camunda.zeebe.scheduler.FutureUtil Maven / Gradle / Ivy
The newest version!
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under
* one or more contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright ownership.
* Licensed under the Camunda License 1.0. You may not use this file
* except in compliance with the Camunda License 1.0.
*/
package io.camunda.zeebe.scheduler;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.agrona.LangUtil;
public final class FutureUtil {
/**
* Invokes Future.get() returning the result of the invocation. Transforms checked exceptions into
* RuntimeExceptions to accommodate programmer laziness.
*/
public static T join(final Future f) {
try {
return f.get();
} catch (final Exception e) {
// NOTE: here we actually want to use rethrowUnchecked
LangUtil.rethrowUnchecked(e);
}
return null;
}
public static T join(final Future f, final long timeout, final TimeUnit timeUnit) {
try {
return f.get(timeout, timeUnit);
} catch (final Exception e) {
// NOTE: here we actually want to use rethrowUnchecked
LangUtil.rethrowUnchecked(e);
}
return null;
}
public static Runnable wrap(final Future> future) {
return () -> {
try {
future.get();
} catch (final Exception e) {
LangUtil.rethrowUnchecked(e);
}
};
}
}