package com.ruoyi.iot.util;

import com.ruoyi.iot.constants.IotConstants;;

/**
 * 命令构造器
 * @author wjh
 * 2024/3/22
 */
public class CommandBuilder {

    private final StringBuilder command;

    private CommandBuilder() {
        this.command = new StringBuilder();
    }

    public static CommandBuilder builder() {
        return new CommandBuilder();
    }

    /**
     * 设置命令及其值
     * @param command 命令
     * @param value 值
     */
    public CommandBuilder set(String command, Object value) {
        this.command.append(command).append(value.toString()).append(IotConstants.COMMAND_SEPARATOR);
        return this;
    }

    /**
     * 设置命令，当其为空时，不设置
     * @param command 命令
     * @param value 值
     */
    public CommandBuilder setNotNull(String command, Object value) {
        if (value == null) {
            return this;
        }
        return set(command, value);
    }


    /**
     * 设置命令，当其为空时，设置为第二个值
     * @param command 命令
     * @param value 值
     * @param nullValue 当value为空时，设置为该值
     */
    public CommandBuilder setIfNull(String command, Object value, Object nullValue) {
        if (value == null) {
            return set(command, nullValue);
        }
        return set(command, value);
    }


    public String build() {
        return this.toString();
    }

    @Override
    public String toString() {
        return this.command.toString();
    }

}
