import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public final class RequestBuilder {
private static final String PROCCOD = "PROCCOD";
private static final String INFBDY = "INFBDY";
private final Map<Object, Object> root;
private final List<Object> list;
private RequestBuilder(String code, String argName) {
root = newMap();
list = new ArrayList<Object>();
Map<Object, Object> tempMap = newMap();
root.put(PROCCOD, code);
root.put(INFBDY, tempMap);
tempMap.put(argName, list);
}
public ParamBuilder start() {
return new ParamBuilder(list);
}
public Map<Object, Object> build() {
return root;
}
private <K, V> Map<K, V> newMap() {
return new HashMap<K, V>();
}
public final class ParamBuilder {
private Map<Object, Object> paramMap = newMap();
private ParamBuilder(List<Object> list) {
list.add(paramMap);
}
public <K, V> ParamBuilder addParam(K key, V value) {
this.paramMap.put(key, value);
return this;
}
public RequestBuilder end() {
return RequestBuilder.this;
}
}
public static RequestBuilder create(String code, String argName) {
return new RequestBuilder(code, argName);
}
}
public class BuilderTest {
public static void main(String[] args) {
Map map = RequestBuilder.create("IMB1", "IMB1X1")
.start().addParam("hkp", "123").addParam("hello", 1).end()
.start().addParam("1", "xx1").addParam("2", "xx2").addParam("3", "xx3").end()
.build();
Gson gson = new Gson();
System.out.println(gson.toJson(map));
}
}