File tree Expand file tree Collapse file tree 1 file changed +13
-2
lines changed
arclight-common/src/main/java/io/izzel/arclight/common/util Expand file tree Collapse file tree 1 file changed +13
-2
lines changed Original file line number Diff line number Diff line change 11package io .izzel .arclight .common .util ;
22
33import java .util .Iterator ;
4+ import java .util .NoSuchElementException ;
45import java .util .function .Predicate ;
56
67public class IteratorUtil {
@@ -14,30 +15,40 @@ private static class FilterIterator<T> implements Iterator<T> {
1415 private final Iterator <T > iterator ;
1516 private final Predicate <T > predicate ;
1617
18+ private boolean nextComputed = false ;
1719 private boolean hasNext = false ;
1820 private T next ;
1921
2022 private FilterIterator (Iterator <T > iterator , Predicate <T > predicate ) {
2123 this .iterator = iterator ;
2224 this .predicate = predicate ;
23- this .computeNext ();
2425 }
2526
2627 @ Override
2728 public boolean hasNext () {
29+ if (!nextComputed ) {
30+ this .computeNext ();
31+ }
2832 return hasNext ;
2933 }
3034
3135 @ Override
3236 public T next () {
37+ if (!nextComputed ) {
38+ this .computeNext ();
39+ }
40+ if (!hasNext ) {
41+ throw new NoSuchElementException ();
42+ }
3343 try {
3444 return this .next ;
3545 } finally {
36- computeNext () ;
46+ nextComputed = false ;
3747 }
3848 }
3949
4050 private void computeNext () {
51+ nextComputed = true ;
4152 while (iterator .hasNext ()) {
4253 T next = iterator .next ();
4354 if (predicate .test (next )) {
You can’t perform that action at this time.
0 commit comments