package com.ruoyi.generator.domain.front.dto.block;

public class ChartQueryParameter
{
    private String name;
    private Object value;

    public ChartQueryParameter()
    {
    }

    public ChartQueryParameter(String name, Object value)
    {
        this.name = name;
        this.value = value;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public Object getValue()
    {
        return value;
    }

    public void setValue(Object value)
    {
        this.value = value;
    }

    public String getJavaLiteral()
    {
        return toJavaLiteral(value);
    }

    public static String toJavaLiteral(Object value)
    {
        if (value == null)
        {
            return "null";
        }
        if (value instanceof Number || value instanceof Boolean)
        {
            return String.valueOf(value);
        }
        return "\"" + escapeJava(String.valueOf(value)) + "\"";
    }

    private static String escapeJava(String value)
    {
        StringBuilder escaped = new StringBuilder();
        for (int index = 0; index < value.length(); index++)
        {
            char ch = value.charAt(index);
            switch (ch)
            {
                case '\\': escaped.append("\\\\"); break;
                case '"': escaped.append("\\\""); break;
                case '\n': escaped.append("\\n"); break;
                case '\r': escaped.append("\\r"); break;
                case '\t': escaped.append("\\t"); break;
                case '\b': escaped.append("\\b"); break;
                case '\f': escaped.append("\\f"); break;
                default: escaped.append(ch);
            }
        }
        return escaped.toString();
    }
}
