
com.aliyun.dts.subscribe.clients.common.function.SwallowException Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dts-new-subscribe-sdk Show documentation
Show all versions of dts-new-subscribe-sdk Show documentation
The Aliyun new Subscribe SDK for Java used for accessing Data Transmission Service
The newest version!
package com.aliyun.dts.subscribe.clients.common.function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Closeable;
import java.util.function.Function;
//@SuppressFBWarnings
public final class SwallowException implements AutoCloseable {
private static final Logger LOGGER = LoggerFactory.getLogger(SwallowException.class);
private final T o;
private SwallowException(T oo) {
this.o = oo;
}
public interface CalleeFunctionWithoutReturnValue {
void call() throws Exception;
}
public interface CalleeFunctionWithReturnValue {
R call() throws Exception;
}
public interface ConsumerFunctionWithoutReturnValue {
void call(L value) throws Exception;
}
public static R callAndSwallowException(CalleeFunctionWithReturnValue callee) {
try {
return callee.call();
} catch (Exception e) {
LOGGER.info("call function {} failed, and swallow exception", callee.getClass().getName(), e);
}
return null;
}
public static void callAndSwallowException(CalleeFunctionWithoutReturnValue function) {
callAndSwallowException((CalleeFunctionWithReturnValue) () -> {
function.call();
return null;
});
}
public static R callAndThrowRuntimeException(CalleeFunctionWithReturnValue callee) {
try {
return callee.call();
} catch (Exception e) {
LOGGER.info("call function {} failed, and swallow exception", callee.getClass().getName(), e);
throw new RuntimeException(e.toString(), e);
}
}
public static void callAndThrowRuntimeException(CalleeFunctionWithoutReturnValue function) {
callAndThrowRuntimeException((CalleeFunctionWithReturnValue) () -> {
function.call();
return null;
});
}
public static R callAndThrowException(CalleeFunctionWithReturnValue callee,
Function exceptionSupplier) {
try {
return callee.call();
} catch (Exception e) {
LOGGER.info("call function {} failed", callee.getClass().getName(), e);
throw exceptionSupplier.apply(e);
}
}
public static void callAndThrowException(CalleeFunctionWithoutReturnValue function,
Function exceptionSupplier) {
callAndThrowException((CalleeFunctionWithReturnValue) () -> {
function.call();
return null;
}, exceptionSupplier);
}
public static SwallowException create(R oo) {
return new SwallowException<>(oo);
}
public T getObject() {
return o;
}
@Override
public void close() {
callAndSwallowException(() -> o.close());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy