package com.zcloud.common.utils;

import java.util.HashMap;
import java.util.Map;

public class CamelCaseUtil {

    public static <V> Map<String, V> camelToUnderline(Map<String, V> sourceMap) {
        Map<String, V> targetMap = new HashMap<>();
        for (Map.Entry<String, V> entry : sourceMap.entrySet()) {
            String key = entry.getKey();
            String newKey = camelToUnderline(key);
            targetMap.put(newKey, entry.getValue());
        }
        return targetMap;
    }

    private static String camelToUnderline(String camelCase) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < camelCase.length(); i++) {
            char c = camelCase.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append("_").append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Map<String, String> sourceMap = new HashMap<>();
        sourceMap.put("helloWorld", "value1");
        sourceMap.put("exampleKey", "value2");
        Map<String, String> targetMap = camelToUnderline(sourceMap);
        System.out.println(targetMap);  // 输出: {hello_world=value1, example_key=value2}
    }
}
