package data; import java.util.List; import java.util.Spliterator; import java.util.function.Consumer; public class StudentSpliterator implements Spliterator { private final List studentList; private int endIndex; private int startIndex; public StudentSpliterator(List studentList) { this(studentList, 0, studentList.size()); } private StudentSpliterator(List studentList, int startIndex, int endIndex) { this.studentList = studentList; this.startIndex = startIndex; this.endIndex = endIndex; } @Override public boolean tryAdvance(Consumer action) { if (getSize() > 0) { action.accept(studentList.get(startIndex)); startIndex++; return true; } return false; } @Override public Spliterator trySplit() { if (getSize() == 1) { return null; } int newStartIndex; int newEndIndex = endIndex; if (getSize() % 2 == 0) { newStartIndex = startIndex + getSize() / 2; this.endIndex -= getSize() / 2; } else { newStartIndex = startIndex + getSize() / 2 + 1; this.endIndex -= getSize() / 2; } return new StudentSpliterator(studentList, newStartIndex, newEndIndex); } private int getSize() { return endIndex - startIndex; } @Override public long estimateSize() { return studentList.size(); } @Override public int characteristics() { return NONNULL; } }