package com.yami.shop.wx.utils;

import com.google.common.collect.Maps;
import com.yami.shop.common.exception.GlobalException;

import java.util.Map;

/**
 * @author Cullen
 * @date 2015-12-12 00:00:00
 */
public class CullenUtils {

    /**
     * 校验参数是否满足表达式并抛出异常
     *
     * @param exp     表达式
     * @param message 异常信息
     */
    public static void validateDataThrowException(Boolean exp, String message) {
        if (exp) {
            throw new GlobalException(message);
        }
    }


    /**
     * 校验参数是否满足表达式并抛出异常
     *
     * @param exp1    表达式
     * @param exp2    表达式
     * @param message 异常信息
     */
    public static void validateDataThrowException(Boolean exp1, Boolean exp2, String message) {
        if (exp1) {
            if (exp2) {
                throw new GlobalException(message);
            }
        }
    }

    /**
     * 例如：{auth=Kaur, enable=true, domain=www.hjx.org}
     *
     * @param input 文本
     * @return map
     */
    public static Map<String, String> textToMap(String input) {
        Map<String, String> map = Maps.newHashMap();
        input = input.substring(1, input.length() - 1);
        String[] keyValuePairs = input.split(", ");
        for (String pair : keyValuePairs) {
            String[] entry = pair.split("=");
            String key = entry[0].trim();
            String value = entry[1].trim();
            if (value.startsWith("'") && value.endsWith("'")) {
                value = value.substring(1, value.length() - 1);
            }
            map.put(key, value);
        }
        return map;
    }

    /**
     * 判断是否是数字
     *
     * @param isNumeric isNumeric
     * @return boolean
     */
    public static boolean isNumeric(String isNumeric) {
        return isNumeric.matches("\\d+");
    }

}
