package com.binance.connector.client.utils;

import com.binance.connector.client.exceptions.BinanceConnectorException;
import java.util.Arrays;
import java.util.Map;
import java.util.UUID;
import org.json.JSONObject;

/* loaded from: binance-connector-java-3.0.0rc3.jar:com/binance/connector/client/utils/ParameterChecker.class */
public final class ParameterChecker {
    private static final int countStart = 0;
    private static final int countFinal = 1;

    private ParameterChecker() {
    }

    public static void checkParameter(Map<String, Object> parameters, String parameter, Class<?> t) {
        checkRequiredParameter(parameters, parameter);
        checkParameterType(parameters.get(parameter), t, parameter);
    }

    public static void checkRequiredParameter(Map<String, Object> parameters, String parameter) {
        if (!parameters.containsKey(parameter)) {
            throw new BinanceConnectorException(String.format("\"%s\" is a mandatory parameter!", parameter));
        }
    }

    public static void checkParameterType(Object parameter, Class<?> t, String name) {
        if (!t.isInstance(parameter)) {
            throw new BinanceConnectorException(String.format("\"%s\" must be of %s type.", name, t));
        } else if ((parameter instanceof String) && ((String) parameter).isEmpty()) {
            throw new BinanceConnectorException(String.format("\"%s\" must not be empty.", name));
        }
    }

    public static Object processId(Object id, String name) {
        if (!(id instanceof Integer) && !(id instanceof String) && id != null) {
            throw new BinanceConnectorException(name + " must be of Int or String type.");
        } else if (id == null || ((id instanceof String) && ((String) id).isEmpty())) {
            return UUID.randomUUID().toString();
        } else {
            return id;
        }
    }

    public static void checkOnlyOneOfParameters(JSONObject params, String... parameters) {
        if (params != null) {
            int count = countStart;
            int length = parameters.length;
            for (int i = countStart; i < length; i += countFinal) {
                if (params.has(parameters[i])) {
                    count += countFinal;
                }
            }
            if (count > countFinal) {
                throw new BinanceConnectorException("Only one of the following parameters is allowed: " + Arrays.toString(parameters));
            }
        }
    }

    public static void checkOneOfParametersRequired(JSONObject params, String... parameters) {
        String exceptMsg = "One of the following parameters is required: " + Arrays.toString(parameters);
        if (params != null) {
            int count = countStart;
            int length = parameters.length;
            for (int i = countStart; i < length; i += countFinal) {
                if (!params.has(parameters[i])) {
                    count += countFinal;
                }
            }
            if (count == parameters.length) {
                throw new BinanceConnectorException(exceptMsg);
            }
            return;
        }
        throw new BinanceConnectorException(exceptMsg);
    }
}
