package cn.axzo.msg.center.utils;

import cn.axzo.basics.common.exception.ServiceException;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author yanglin
 */
@Slf4j
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public class FutureWrapper<T> {

    private final CompletableFuture<T> future;

    public T get() {
        try {
            return future.get();
        } catch (InterruptedException | ExecutionException e) {
            log.error("执行异常", e);
            throw new ServiceException("执行异常");
        }
    }

    public T get(long timeout, TimeUnit unit) {
        try {
            return future.get(timeout, unit);
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            log.error("执行异常", e);
            throw new ServiceException("执行异常");
        }
    }
}