com.github.shepherdviolet.glacimon.java.misc.StreamingBuilder Maven / Gradle / Ivy
/*
* Copyright (C) 2022-2023 S.Violet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project GitHub: https://github.com/shepherdviolet/glacimon
* Email: [email protected]
*/
package com.github.shepherdviolet.glacimon.java.misc;
import java.util.*;
/**
* [非线程安全|NOT THREAD SAFE] 流式创建Map/Set/Object/List
*
*
*
*
*
* @param key type
* @param value type
*
* @author shepherdviolet
*/
public class StreamingBuilder {
/**
* 流式创建一个HashMap
* @param key type
* @param value type
*/
public static MapKeySetter hashMap() {
return new MapBuilder<>(new HashMap<>());
}
/**
* 流式创建一个LinkedHashMap
* @param key type
* @param value type
*/
public static MapKeySetter linkedHashMap() {
return new MapBuilder<>(new LinkedHashMap<>());
}
/**
* 流式创建一个ArrayList
* @param element type
*/
public static ListElementAdder arrayList() {
return new ListBuilder<>(new ArrayList<>());
}
/**
* 流式创建一个LinkedList
* @param element type
*/
public static ListElementAdder linkedList() {
return new ListBuilder<>(new LinkedList<>());
}
/**
* 流式创建一个Set
* @param element type
*/
public static SetElementAdder hashSet() {
return new SetBuilder<>(new HashSet<>());
}
// MapBuilder ////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* 流式创建一个Map
* @param key type
* @param value type
*/
public static class MapBuilder implements MapKeySetter, MapValueSetter {
private Map map;
private K key;
public MapBuilder(Map map) {
if (map == null) {
map = new LinkedHashMap<>();
}
this.map = map;
}
@Override
public MapValueSetter key(K key) {
if (this.map == null) {
throw new IllegalStateException("Each StreamingBuilder can only create one map object, do not call the key method after build!");
}
this.key = key;
return this;
}
@Override
public MapKeySetter value(V value) {
if (this.map == null) {
throw new IllegalStateException("Each StreamingBuilder can only create one map object, do not call the value method after build!");
}
map.put(key, value);
return this;
}
@Override
@SuppressWarnings("unchecked")
public Map build() {
if (this.map == null) {
throw new IllegalStateException("Each StreamingBuilder can only create one map object, do not call the build method twice!");
}
Map map = (Map) this.map;
this.map = null;
this.key = null;
return map;
}
}
/**
* 流式创建一个Map
* @param key type
* @param value type
*/
public interface MapKeySetter {
/**
* 设置key
*/
MapValueSetter key(K key);
/**
* 创建Map, 这个方法只能被调用一次, 否则会抛出异常
* @throws IllegalStateException 每个MapBuilder只能创建一个Map对象, 不要调用build方法两次
*/
Map build();
}
/**
* 流式创建一个Map
* @param key type
* @param value type
*/
public interface MapValueSetter {
/**
* 设置value
*/
MapKeySetter value(V value);
}
// ListBuilder ////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* 流式创建一个List
* @param element type
*/
public static class ListBuilder implements ListElementAdder {
private List collection;
public ListBuilder(List collection) {
if (collection == null) {
collection = new ArrayList<>();
}
this.collection = collection;
}
@Override
public ListElementAdder add(E element) {
collection.add(element);
return this;
}
@Override
@SuppressWarnings("unchecked")
public List build() {
if (this.collection == null) {
throw new IllegalStateException("Each StreamingBuilder can only create one collection object, do not call the build method twice!");
}
List collection = (List) this.collection;
this.collection = null;
return collection;
}
}
/**
* 流式创建一个List
* @param element type
*/
public interface ListElementAdder {
/**
* 添加一个元素
*/
ListElementAdder add(E element);
/**
* 创建List, 这个方法只能被调用一次, 否则会抛出异常
* @throws IllegalStateException 每个ListBuilder只能创建一个Collection对象, 不要调用build方法两次
*/
List build();
}
// SetBuilder ////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* 流式创建一个Set
* @param element type
*/
public static class SetBuilder implements SetElementAdder {
private Set collection;
public SetBuilder(Set collection) {
if (collection == null) {
collection = new HashSet<>();
}
this.collection = collection;
}
@Override
public SetElementAdder add(E element) {
collection.add(element);
return this;
}
@Override
@SuppressWarnings("unchecked")
public Set build() {
if (this.collection == null) {
throw new IllegalStateException("Each StreamingBuilder can only create one collection object, do not call the build method twice!");
}
Set collection = (Set) this.collection;
this.collection = null;
return collection;
}
}
/**
* 流式创建一个Set
* @param element type
*/
public interface SetElementAdder {
/**
* 添加一个元素
*/
SetElementAdder add(E element);
/**
* 创建Set, 这个方法只能被调用一次, 否则会抛出异常
* @throws IllegalStateException 每个SetBuilder只能创建一个Collection对象, 不要调用build方法两次
*/
Set build();
}
}