Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed a bug in WhereIterable

  • Loading branch information...
commit 0ceede563f3011c40cc31bc83d2c98daef26def4 1 parent af40cf9
@dolbz authored
View
18 src/main/java/com/dolbz/jenumerable/TakeWhileIterable.java
@@ -30,14 +30,15 @@ public TakeWhileIterable(final Iterable<TSource> source,
public boolean hasNext() {
if (onCheckedMatch) {
return true;
- }
- if (sourceIterator.hasNext()) {
- if (!onCheckedMatch) {
- currentItem = sourceIterator.next();
- }
- if (predicate.check(currentItem, index++)) {
- onCheckedMatch = true;
- return true;
+ } else {
+ if (sourceIterator.hasNext()) {
+ if (!onCheckedMatch) {
+ currentItem = sourceIterator.next();
+ }
+ if (predicate.check(currentItem, index++)) {
+ onCheckedMatch = true;
+ return true;
+ }
}
}
return false;
@@ -50,7 +51,6 @@ public TSource next() {
} else if (sourceIterator.hasNext()) {
currentItem = sourceIterator.next();
if (predicate.check(currentItem, index++)) {
- onCheckedMatch = false;
return currentItem;
}
}
View
31 src/main/java/com/dolbz/jenumerable/WhereIterable.java
@@ -41,29 +41,30 @@ public WhereIterable(final IndexPredicate<TSource> predicate,
private TSource currentItem;
public boolean hasNext() {
- // There is no concept of hasNext for IEnumerable. Have to jump
- // through hoops to ensure hasNext() and next() work nicely with the
- // deferred execution
- while (sourceIterator.hasNext()) {
- if (!onCheckedMatch) {
+ if (onCheckedMatch) {
+ return true;
+ } else {
+ while (sourceIterator.hasNext()) {
currentItem = sourceIterator.next();
- }
- if (predicate.check(currentItem, index++)) {
- onCheckedMatch = true;
- return true;
+ if (predicate.check(currentItem, index++)) {
+ onCheckedMatch = true;
+ return true;
+ }
}
}
return false;
}
public TSource next() {
- while (onCheckedMatch || sourceIterator.hasNext()) {
- if (!onCheckedMatch) {
+ if (onCheckedMatch) {
+ onCheckedMatch = false;
+ return currentItem;
+ } else {
+ while (sourceIterator.hasNext()) {
currentItem = sourceIterator.next();
- }
- if (predicate.check(currentItem, index++)) {
- onCheckedMatch = false;
- return currentItem;
+ if (predicate.check(currentItem, index++)) {
+ return currentItem;
+ }
}
}
throw new NoSuchElementException();
Please sign in to comment.
Something went wrong with that request. Please try again.