package com.chinaitop.depot.intelligent.factoryObject;

import com.chinaitop.depot.intelligent.common.ConstantClass;
import com.chinaitop.depot.intelligent.factoryObject.factoryInterface.StringFactory;
import org.springframework.stereotype.Component;

/**
 * @author qingsong.han
 * @description: 用于字符串连接简单工厂模式 类
 * @create 2021-09-08 10:46
 */
@Component
public class StringJoinFactory implements StringFactory {
    /**
     * 创建拼接字符串方法
     *
     * @param type  1: stringBuild,2: stringBuffer 默认 stringBuild
     * @param value 参数 按照传入的顺序拼接(例: a b c = abc)
     * @return string
     */
    @Override
    public String joinString(int type, String... value) {
        switch (type) {
            case ConstantClass.TYPE_ONE:
                return stringBuilderJoin(value);
            case ConstantClass.TYPE_TWO:
                return stringBufferJoin(value);
            default:
                return stringBuilderJoin(value);
        }
    }

    /**
     * 内部拼接方法
     */
    private String stringBuilderJoin(String... value) {
        StringBuilder builder = builderFactory();
        for (String s : value) {
            builder.append(s);
        }
        return builder.toString();
    }

    @SuppressWarnings("all")
    private String stringBufferJoin(String... value) {
        StringBuffer buffer = bufferFactory();
        for (String s : value) {
            buffer.append(s);
        }
        return buffer.toString();
    }

    @Override
    public StringBuilder builderFactory() {
        return new StringBuilder();
    }

    @Override
    public StringBuffer bufferFactory() {
        return new StringBuffer();
    }

}
