ListSupport 추가

master
mjkhan21 1 year ago
parent 99ab158aa8
commit 97df00b484

@ -0,0 +1,75 @@
package cokr.xit.foundation.util;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.StreamSupport;
/**List
* @param <T> List
* @author mjkhan
*/
public class ListSupport<T> {
private int workSize = 1;
private List<T> list;
/** list
* @param workSize
* @return ListSupport
*/
public ListSupport<T> setWorkSize(int workSize) {
if (workSize < 1)
throw new IllegalArgumentException("workSize < 1");
this.workSize = workSize;
return this;
}
/**List Collection .
* @param iterable Collection
* @return ListSupport
*/
public ListSupport<T> setIterable(Iterable<T> iterable) {
this.list = iterable instanceof List<T> ?
(List<T>)iterable :
StreamSupport.stream(iterable.spliterator(), false).toList();
return this;
}
/** function List .
* @param consumer List function
*/
public void consume(Consumer<List<T>> consumer) {
int size = list == null ? 0 : list.size();
if (size < 1) return;
int from = 0;
while (from <= size - 1) {
int to = from + workSize;
List<T> subList = list.subList(from, Math.min(to, size));
consumer.accept(subList);
from = to;
}
}
/** Collection workSize List .
* @return List
*/
public List<List<T>> split() {
ArrayList<List<T>> result = new ArrayList<>();
consume(result::add);
return result;
}
/*
public static void main(String[] args) {
List<String> list = List.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k");
ListSupport<String> support = new ListSupport<String>()
.setWorkSize(3)
.setIterable(list);
for (List<String> subList: support.split())
System.out.println(subList);
support.consume(System.out::println);;
}
*/
}
Loading…
Cancel
Save