Skip to content

Commit

Permalink
0.9.52
Browse files Browse the repository at this point in the history
  • Loading branch information
landawn committed Feb 21, 2017
1 parent 56d09ef commit 96a3bf0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
Binary file modified lib/abacus-util-0.9.52.jar
Binary file not shown.
Binary file modified lib/abacus-util-all-0.9.52.jar
Binary file not shown.
67 changes: 43 additions & 24 deletions src/com/landawn/abacus/util/stream/AbstractStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -636,24 +636,30 @@ public Stream<T> collapse(final BiPredicate<? super T, ? super T> collapsible, f

return this.newStream(new ImmutableIterator<T>() {
private T pre = (T) NONE;
private T res = null;

@Override
public boolean hasNext() {
return iter.hasNext();
return pre != NONE || iter.hasNext();
}

@Override
public T next() {
T next = iter.next();
T res = pre == NONE ? (pre = iter.next()) : pre;

boolean hasMore = false;

if (pre == NONE || collapsible.test(pre, next) == false) {
res = next;
} else {
res = mergeFunction.apply(res, next);
while (iter.hasNext()) {
if (collapsible.test(pre, (pre = iter.next()))) {
res = mergeFunction.apply(res, pre);
} else {
hasMore = true;
break;
}
}

pre = next;
if (hasMore == false) {
pre = (T) NONE;
}

return res;
}
Expand All @@ -666,24 +672,30 @@ public <R> Stream<R> collapse(final R seed, final BiPredicate<? super T, ? super

return this.newStream(new ImmutableIterator<R>() {
private T pre = (T) NONE;
private R res = null;

@Override
public boolean hasNext() {
return iter.hasNext();
return pre != NONE || iter.hasNext();
}

@Override
public R next() {
T next = iter.next();
R res = mergeFunction.apply(seed, pre == NONE ? (pre = iter.next()) : pre);

boolean hasMore = false;

if (pre == NONE || collapsible.test(pre, next) == false) {
res = mergeFunction.apply(seed, next);
} else {
res = mergeFunction.apply(res, next);
while (iter.hasNext()) {
if (collapsible.test(pre, (pre = iter.next()))) {
res = mergeFunction.apply(res, pre);
} else {
hasMore = true;
break;
}
}

pre = next;
if (hasMore == false) {
pre = (T) NONE;
}

return res;
}
Expand All @@ -697,24 +709,31 @@ public <C> Stream<C> collapse(final Supplier<C> supplier, final BiPredicate<? su

return this.newStream(new ImmutableIterator<C>() {
private T pre = (T) NONE;
private C res = null;

@Override
public boolean hasNext() {
return iter.hasNext();
return pre != NONE || iter.hasNext();
}

@Override
public C next() {
T next = iter.next();
final C res = supplier.get();
mergeFunction.accept(res, pre == NONE ? (pre = iter.next()) : pre);

if (pre == NONE || collapsible.test(pre, next) == false) {
res = supplier.get();
}
boolean hasMore = false;

mergeFunction.accept(res, next);
while (iter.hasNext()) {
if (collapsible.test(pre, (pre = iter.next()))) {
mergeFunction.accept(res, pre);
} else {
hasMore = true;
break;
}
}

pre = next;
if (hasMore == false) {
pre = (T) NONE;
}

return res;
}
Expand Down

0 comments on commit 96a3bf0

Please sign in to comment.