host.anzo.commons.collection.MapSplitter Maven / Gradle / Ivy
package host.anzo.commons.collection;
import java.util.*;
/***
* @author ANZO
* @since 6/22/2022
*/
public class MapSplitter {
private TreeMap map;
private List keys;
private int splitCount;
private int currentIndex = 0;
public MapSplitter(Map map, int splitCount) {
if (map != null) {
this.map = new TreeMap<>(map);
this.keys = new ArrayList<>(this.map.keySet());
this.splitCount = splitCount;
}
}
public MapSplitter(Map map, Comparator super K> comparator, int splitCount) {
if (map != null) {
this.map = new TreeMap<>(comparator);
this.map.putAll(map);
this.keys = new ArrayList<>(this.map.keySet());
this.splitCount = splitCount;
}
}
public Map getNext(int splitCount) {
this.splitCount = splitCount;
return getNext();
}
public Map getNext() {
final Map chunk;
if (currentIndex + splitCount < map.size()) {
chunk = map.subMap(keys.get(currentIndex), keys.get(currentIndex + splitCount));
}
else {
chunk = map.tailMap(keys.get(currentIndex));
}
currentIndex += splitCount;
return chunk;
}
public int size() {
return map.size();
}
public boolean isFirst() {
return currentIndex <= splitCount;
}
public boolean isLast() {
return currentIndex == map.size();
}
public boolean hasNext() {
return currentIndex < map.size();
}
}