|
|
@@ -10,11 +10,13 @@ import java.util.Iterator;
|
|
|
import java.util.ListIterator;
|
|
|
import java.util.NoSuchElementException;
|
|
|
import java.util.Spliterator;
|
|
|
-import java.util.function.*;
|
|
|
+import java.util.function.Consumer;
|
|
|
+import java.util.function.IntConsumer;
|
|
|
+import java.util.function.IntFunction;
|
|
|
+import java.util.function.IntPredicate;
|
|
|
+import java.util.function.IntUnaryOperator;
|
|
|
import java.util.stream.IntStream;
|
|
|
import java.util.stream.StreamSupport;
|
|
|
-import java.util.OptionalInt;
|
|
|
-
|
|
|
import net.ranides.assira.collection.IntCollection;
|
|
|
import net.ranides.assira.collection.IntComparator;
|
|
|
import net.ranides.assira.functional.IntBiPredicate;
|
|
|
@@ -29,46 +31,41 @@ public final class IntIteratorUtils {
|
|
|
/* utility class */
|
|
|
}
|
|
|
|
|
|
- public static OptionalInt first(IntIterator iterator) {
|
|
|
- return iterator.hasNext() ? OptionalInt.of(iterator.nextInt()) : OptionalInt.empty();
|
|
|
+ public static int first(IntIterator iterator) {
|
|
|
+ return iterator.next();
|
|
|
}
|
|
|
|
|
|
- public static OptionalInt first(IntIterator iterator, IntPredicate predicate) {
|
|
|
+ public static int first(IntIterator iterator, IntPredicate predicate) {
|
|
|
while(iterator.hasNext()) {
|
|
|
int v = iterator.nextInt();
|
|
|
if( predicate.test(v)) {
|
|
|
- return OptionalInt.of(v);
|
|
|
+ return v;
|
|
|
}
|
|
|
}
|
|
|
- return OptionalInt.empty();
|
|
|
+ throw new NoSuchElementException();
|
|
|
}
|
|
|
|
|
|
- public static OptionalInt last(IntIterator iterator) {
|
|
|
- if(!iterator.hasNext()) {
|
|
|
- return OptionalInt.empty();
|
|
|
- }
|
|
|
+ public static int last(IntIterator iterator) {
|
|
|
int result;
|
|
|
do {
|
|
|
result = iterator.nextInt();
|
|
|
} while(iterator.hasNext());
|
|
|
- return OptionalInt.of(result);
|
|
|
+ return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
- public static OptionalInt last(IntIterator iterator, IntPredicate predicate) {
|
|
|
- int result = 0;
|
|
|
- boolean found = false;
|
|
|
+ public static int last(IntIterator iterator, IntPredicate predicate) {
|
|
|
+ Integer result = null;
|
|
|
while(iterator.hasNext()) {
|
|
|
int v = iterator.nextInt();
|
|
|
if(predicate.test(v)) {
|
|
|
result = v;
|
|
|
- found = true;
|
|
|
}
|
|
|
}
|
|
|
- if(!found) {
|
|
|
- return OptionalInt.empty();
|
|
|
- }
|
|
|
- return OptionalInt.of(result);
|
|
|
+ if(null == result) {
|
|
|
+ throw new NoSuchElementException();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
public static int size(IntIterator iterator) {
|
|
|
@@ -215,7 +212,7 @@ public final class IntIteratorUtils {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
- };
|
|
|
+ }
|
|
|
|
|
|
|
|
|
private static final class Adapter<T> implements Iterator<T> {
|