package com.sonic.common.exception;

import java.util.Collection;
import java.util.Objects;
import java.util.function.Supplier;

import com.sonic.common.rpc.ApiResultCode;
import com.sonic.common.rpc.GlobalResultCode;

import lombok.experimental.UtilityClass;

/**
 * @Description:
 * @version: 1.0.0
 */
@UtilityClass
public class SysExceptionUtils {

	public static void check(boolean expect, String code, String msg) {
		if (expect) {
			throw new SysException(code, msg);
		}
	}

	public static void check(boolean expect, ApiResultCode resultCode) {
		if (expect) {
			throw new SysException(resultCode);
		}
	}

    public static <T extends RuntimeException> void check(boolean expect, Supplier<? extends T> supplier) throws T {
        if (expect) {
            throw supplier.get();
        }
    }

    public static void checkEquals(Object source, Object target, String msg) {
        if (!Objects.equals(source, target)) {
            throw new SysException(GlobalResultCode.SYSTEM_EXCEPTION.getErrorCode(), msg);
        }
    }

    public static void checkNonNull(Object target, String msg) {
        if (Objects.isNull(target)) {
            throw new SysException(GlobalResultCode.SYSTEM_EXCEPTION.getErrorCode(), msg);
        }
    }

    public static void checkNotEmpty(Collection coll, String msg) {
        if (coll == null || coll.isEmpty()) {
            throw new BizException(GlobalResultCode.SYSTEM_EXCEPTION.getErrorCode(), msg);
        }
    }
}
