package com.agileboot.codegen.util;

import java.util.Map;

/**
 * Map工具类
 */
public class MapUtil {

    /**
     * 从Map中获取String值
     *
     * @param map Map对象
     * @param key 键
     * @return String值
     */
    public static String getString(Map<String, Object> map, String key) {
        if (map == null || key == null) {
            return null;
        }
        Object value = map.get(key);
        return value != null ? value.toString() : null;
    }

    /**
     * 从Map中获取String值，带默认值
     *
     * @param map          Map对象
     * @param key          键
     * @param defaultValue 默认值
     * @return String值
     */
    public static String getString(Map<String, Object> map, String key, String defaultValue) {
        String value = getString(map, key);
        return value != null ? value : defaultValue;
    }

    /**
     * 从Map中获取Boolean值
     *
     * @param map Map对象
     * @param key 键
     * @return Boolean值
     */
    public static Boolean getBoolean(Map<String, Object> map, String key) {
        if (map == null || key == null) {
            return null;
        }
        Object value = map.get(key);
        if (value instanceof Boolean) {
            return (Boolean) value;
        }
        if (value instanceof String) {
            return Boolean.parseBoolean((String) value);
        }
        return null;
    }

    /**
     * 从Map中获取Boolean值，带默认值
     *
     * @param map          Map对象
     * @param key          键
     * @param defaultValue 默认值
     * @return Boolean值
     */
    public static Boolean getBoolean(Map<String, Object> map, String key, Boolean defaultValue) {
        Boolean value = getBoolean(map, key);
        return value != null ? value : defaultValue;
    }
}
