map/jsonobject/set/list链式添加工具类工具类
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author jingshiyu
* @date 2019/10/25 13:47:53
* @desc
*/
public class CollectionBuilder {
/**
* map的build方法
*
* @param map
* @return
* @author jingshiyu
* @date 2019/10/25 14:36
*/
public static <K, V> MapBuilder<K, V> builder(Map<K, V> map) {
return new MapBuilder<>(map);
}
/**
* JsonObject的build方法
*
* @param jSONObject
* @return
* @author jingshiyu
* @date 2019/10/25 14:36
*/
public static JSONObjectBuilder builder(JSONObject jSONObject) {
return new JSONObjectBuilder(jSONObject);
}
/**
* list的build方法
*
* @param list
* @return
* @author jingshiyu
* @date 2019/10/25 14:36
*/
public static <V> ListBuilder<V> builder(List<V> list) {
return new ListBuilder<>(list);
}
/**
* set的build方法
*
* @param set
* @return
* @author jingshiyu
* @date 2019/10/25 14:36
*/
public static <V> SetBuilder<V> builder(Set<V> set) {
return new SetBuilder<>(set);
}
public static class MapBuilder<K, V> {
private Map<K, V> map;
private boolean underConstruction;
private MapBuilder(Map<K, V> map) {
this.map = map;
underConstruction = true;
}
public MapBuilder<K, V> put(K k, V v) {
if (!underConstruction) {
throw new IllegalStateException("Underlying map has already been built");
}
map.put(k, v);
return this;
}
public Map<K, V> build() {
if (!underConstruction) {
throw new IllegalStateException("Underlying map has already been built");
}
underConstruction = false;
return map;
}
}
public static class JSONObjectBuilder {
private JSONObject jSONObject;
private boolean underConstruction;
private JSONObjectBuilder(JSONObject jSONObject) {
this.jSONObject = jSONObject;
underConstruction = true;
}
public JSONObjectBuilder put(String k, Object v) {
if (!underConstruction) {
throw new IllegalStateException("Underlying jSONObject has already been built");
}
jSONObject.put(k, v);
return this;
}
public JSONObject build() {
if (!underConstruction) {
throw new IllegalStateException("Underlying jSONObject has already been built");
}
underConstruction = false;
return jSONObject;
}
}
public static class ListBuilder<V> {
private List<V> list;
private boolean underConstruction;
private ListBuilder(List<V> list) {
this.list = list;
underConstruction = true;
}
public ListBuilder<V> add(V v) {
if (!underConstruction) {
throw new IllegalStateException("Underlying list has already been built");
}
list.add(v);
return this;
}
public List<V> build() {
if (!underConstruction) {
throw new IllegalStateException("Underlying list has already been built");
}
underConstruction = false;
return list;
}
}
public static class SetBuilder<V> {
private Set<V> set;
private boolean underConstruction;
private SetBuilder(Set<V> set) {
this.set = set;
underConstruction = true;
}
public SetBuilder<V> add(V v) {
if (!underConstruction) {
throw new IllegalStateException("Underlying set has already been built");
}
set.add(v);
return this;
}
public Set<V> build() {
if (!underConstruction) {
throw new IllegalStateException("Underlying set has already been built");
}
underConstruction = false;
return set;
}
}
}
使用案例
//调用工具类的builder方法,传入需要链式处理的对象即可
List<String> put = CollectionBuilder.builder(new ArrayList<String>())
.add("234").add("4324").add("43243")
.build();
Map<String, String> aaa = CollectionBuilder.builder(new HashMap<String, String>())
.put("111", "222").put("121","123" )
.build();
Map<String, String> bbb = CollectionBuilder.builder(new LinkedHashMap<String, String>())
.put("111", "222").put("aaa","bbb" )
.build();
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author jingshiyu
* @date 2019/10/25 13:47:53
* @desc
*/
public class CollectionBuilder {
/**
* map的build方法
*
* @param map
* @return
* @author jingshiyu
* @date 2019/10/25 14:36
*/
public static <K, V> MapBuilder<K, V> builder(Map<K, V> map) {
return new MapBuilder<>(map);
}
/**
* JsonObject的build方法
*
* @param jSONObject
* @return
* @author jingshiyu
* @date 2019/10/25 14:36
*/
public static JSONObjectBuilder builder(JSONObject jSONObject) {
return new JSONObjectBuilder(jSONObject);
}
/**
* list的build方法
*
* @param list
* @return
* @author jingshiyu
* @date 2019/10/25 14:36
*/
public static <V> ListBuilder<V> builder(List<V> list) {
return new ListBuilder<>(list);
}
/**
* set的build方法
*
* @param set
* @return
* @author jingshiyu
* @date 2019/10/25 14:36
*/
public static <V> SetBuilder<V> builder(Set<V> set) {
return new SetBuilder<>(set);
}
public static class MapBuilder<K, V> {
private Map<K, V> map;
private boolean underConstruction;
private MapBuilder(Map<K, V> map) {
this.map = map;
underConstruction = true;
}
public MapBuilder<K, V> put(K k, V v) {
if (!underConstruction) {
throw new IllegalStateException("Underlying map has already been built");
}
map.put(k, v);
return this;
}
public Map<K, V> build() {
if (!underConstruction) {
throw new IllegalStateException("Underlying map has already been built");
}
underConstruction = false;
return map;
}
}
public static class JSONObjectBuilder {
private JSONObject jSONObject;
private boolean underConstruction;
private JSONObjectBuilder(JSONObject jSONObject) {
this.jSONObject = jSONObject;
underConstruction = true;
}
public JSONObjectBuilder put(String k, Object v) {
if (!underConstruction) {
throw new IllegalStateException("Underlying jSONObject has already been built");
}
jSONObject.put(k, v);
return this;
}
public JSONObject build() {
if (!underConstruction) {
throw new IllegalStateException("Underlying jSONObject has already been built");
}
underConstruction = false;
return jSONObject;
}
}
public static class ListBuilder<V> {
private List<V> list;
private boolean underConstruction;
private ListBuilder(List<V> list) {
this.list = list;
underConstruction = true;
}
public ListBuilder<V> add(V v) {
if (!underConstruction) {
throw new IllegalStateException("Underlying list has already been built");
}
list.add(v);
return this;
}
public List<V> build() {
if (!underConstruction) {
throw new IllegalStateException("Underlying list has already been built");
}
underConstruction = false;
return list;
}
}
public static class SetBuilder<V> {
private Set<V> set;
private boolean underConstruction;
private SetBuilder(Set<V> set) {
this.set = set;
underConstruction = true;
}
public SetBuilder<V> add(V v) {
if (!underConstruction) {
throw new IllegalStateException("Underlying set has already been built");
}
set.add(v);
return this;
}
public Set<V> build() {
if (!underConstruction) {
throw new IllegalStateException("Underlying set has already been built");
}
underConstruction = false;
return set;
}
}
}
//调用工具类的builder方法,传入需要链式处理的对象即可
List<String> put = CollectionBuilder.builder(new ArrayList<String>())
.add("234").add("4324").add("43243")
.build();
Map<String, String> aaa = CollectionBuilder.builder(new HashMap<String, String>())
.put("111", "222").put("121","123" )
.build();
Map<String, String> bbb = CollectionBuilder.builder(new LinkedHashMap<String, String>())
.put("111", "222").put("aaa","bbb" )
.build();