Skip to content

Commit e3db7b5

Browse files
committed
Remove the hasAdvancer() approach(). Now first() and limit() of Traverser use the upstream tryAdvance(). And takeWhile() still use shortCircuit().
1 parent a836f55 commit e3db7b5

28 files changed

+105
-122
lines changed

src/main/java/org/jayield/Query.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ public Query(Advancer<T> adv, Traverser<T> trav) {
8686
this.trav = trav;
8787
}
8888

89-
public boolean hasAdvancer() {
90-
return adv != null;
91-
}
92-
9389
/**
9490
* Yields elements sequentially in the current thread,
9591
* until all elements have been processed or an
@@ -242,8 +238,8 @@ public final Query<T> limit(int n){
242238
* {@link Object#equals(Object)}) of this query.
243239
*/
244240
public final Query<T> distinct(){
245-
AdvancerDistinct<T> adv = new AdvancerDistinct<>(this);
246-
return new Query<>(adv, adv);
241+
AdvancerDistinct<T> dis = new AdvancerDistinct<>(this);
242+
return new Query<>(dis, dis);
247243
}
248244

249245
/**
@@ -252,8 +248,8 @@ public final Query<T> distinct(){
252248
* the provided mapping function to each element.
253249
*/
254250
public final <R> Query<R> flatMap(Function<? super T,? extends Query<? extends R>> mapper){
255-
AdvancerFlatMap<T, R> adv = new AdvancerFlatMap<>(this, mapper);
256-
return new Query<>(adv, adv);
251+
AdvancerFlatMap<T, R> map = new AdvancerFlatMap<>(this, mapper);
252+
return new Query<>(map, map);
257253
}
258254

259255
/**
@@ -271,8 +267,8 @@ public final Query<T> peek(Consumer<? super T> action) {
271267
* this query that match the given predicate.
272268
*/
273269
public final Query<T> takeWhile(Predicate<? super T> predicate){
274-
AdvancerTakeWhile<T> adv = new AdvancerTakeWhile<>(this, predicate);
275-
return new Query<>(adv, adv);
270+
AdvancerTakeWhile<T> take = new AdvancerTakeWhile<>(this, predicate);
271+
return new Query<>(take, take);
276272
}
277273

278274
/**
@@ -293,10 +289,10 @@ public final <R> Query<R> then(Function<Query<T>, Advancer<R>> nextAdv, Function
293289
* {@code Traverser} object that is encapsulated in the resulting query.
294290
*/
295291
public final <R> Query<R> then(Function<Query<T>, Traverser<R>> next) {
296-
Advancer<R> adv = item -> { throw new UnsupportedOperationException(
292+
Advancer<R> nextAdv = item -> { throw new UnsupportedOperationException(
297293
"Missing tryAdvance() implementation! Use the overloaded then() providing both Advancer and Traverser!");
298294
};
299-
return new Query<>(adv, next.apply(this));
295+
return new Query<>(nextAdv, next.apply(this));
300296
}
301297

302298
/**
@@ -336,10 +332,7 @@ public void forEachRemaining(Consumer<? super T> action) {
336332
*/
337333
public final Optional<T> findFirst(){
338334
Box<T> box = new Box<>();
339-
this.shortCircuit(item -> {
340-
box.turnPresent(item);
341-
Yield.bye();
342-
});
335+
this.tryAdvance(box::turnPresent);
343336
return box.isPresent()
344337
? Optional.of(box.getValue())
345338
: Optional.empty();

src/main/java/org/jayield/advs/AdvancerDropWhile.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ public boolean tryAdvance(Yield<? super T> yield) {
3535
if (dropped) {
3636
return upstream.tryAdvance(yield);
3737
} else {
38-
Yield<T> takeWhile = item -> {
38+
while(!dropped && upstream.tryAdvance(item -> {
3939
if(!predicate.test(item)){
4040
dropped = true;
4141
yield.ret(item);
4242
}
43-
};
44-
while(upstream.tryAdvance(takeWhile) && !dropped) { }
43+
})) {
44+
// Intentionally empty. Action specified on yield statement of tryAdvance().
45+
}
4546
return dropped;
4647
}
4748
}

src/main/java/org/jayield/advs/AdvancerFlatMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void traverse(Yield<? super R> yield) {
4444
@Override
4545
public boolean tryAdvance(Yield<? super R> yield) {
4646
while (!src.tryAdvance(yield)) {
47-
if(!upstream.tryAdvance((t) -> src = mapper.apply(t)))
47+
if(!upstream.tryAdvance(t -> src = mapper.apply(t)))
4848
return false;
4949
}
5050
return true;

src/main/java/org/jayield/advs/AdvancerIterate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public AdvancerIterate(U seed, UnaryOperator<U> f) {
3333

3434
@Override
3535
public void traverse(Yield<? super U> yield) {
36-
for(U curr = prev; true; prev = f.apply(prev))
36+
for(U curr = prev; true; curr = f.apply(curr))
3737
yield.ret(curr);
3838
}
3939

src/main/java/org/jayield/advs/AdvancerLimit.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,8 @@ public boolean tryAdvance(Yield<? super T> yield) {
4444
public void traverse(Yield<? super T> yield) {
4545
if(count >= n)
4646
throw new IllegalStateException("Traverser has already been operated on or closed!");
47-
if(upstream.hasAdvancer())
48-
while(this.tryAdvance(yield)) { }
49-
else
50-
upstream.shortCircuit(item -> {
51-
if(count >= n) Yield.bye();
52-
count++;
53-
yield.ret(item);
54-
});
47+
while(this.tryAdvance(yield)) {
48+
// Intentionally empty. Action specified on yield statement of tryAdvance().
49+
}
5550
}
5651
}

src/main/java/org/jayield/advs/AdvancerTakeWhile.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,9 @@ public boolean tryAdvance(Yield<? super T> yield) {
4949

5050
@Override
5151
public void traverse(Yield<? super T> yield) {
52-
if(!hasNext)
53-
throw new IllegalStateException("Traverser has already been operated on or closed!");
54-
while(hasNext && upstream.tryAdvance(item -> {
55-
if(!predicate.test(item))
56-
hasNext = false;
57-
else
58-
yield.ret(item);
59-
})) {}
52+
upstream.shortCircuit(item -> {
53+
if(!predicate.test(item)) Yield.bye();
54+
yield.ret(item);
55+
});
6056
}
6157
}

src/main/java/org/jayield/primitives/dbl/DoubleQuery.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ public DoubleQuery(DoubleAdvancer adv, DoubleTraverser trav) {
7979
this.trav = trav;
8080
}
8181

82-
public boolean hasAdvancer() {
83-
return adv != null;
84-
}
85-
8682
/**
8783
* Returns a sequential ordered {@code DoubleQuery} with elements
8884
* from the provided {@link DoubleStream} data.
@@ -262,8 +258,6 @@ public DoubleQuery skip(int n) {
262258
* if a reduction can be made, using the provided accumulator.
263259
*/
264260
public OptionalDouble reduce(DoubleBinaryOperator accumulator) {
265-
if(!hasAdvancer())
266-
throw new UnsupportedOperationException("Missing Advancer on then() or provide an identity instead!");
267261
DoubleBox box = new DoubleBox();
268262
if(this.tryAdvance(box::setValue)) {
269263
return OptionalDouble.of(this.reduce(box.getValue(), accumulator));
@@ -472,10 +466,7 @@ public OptionalDouble findAny() {
472466
*/
473467
public OptionalDouble findFirst() {
474468
DoubleBox box = new DoubleBox();
475-
this.shortCircuit(item -> {
476-
box.turnPresent(item);
477-
Yield.bye();
478-
});
469+
this.tryAdvance(box::turnPresent);
479470
return box.isPresent()
480471
? OptionalDouble.of(box.getValue())
481472
: OptionalDouble.empty();
@@ -567,10 +558,10 @@ public final DoubleQuery then(
567558
* {@code DoubleTraverser} object that is encapsulated in the resulting {@code DoubleQuery}.
568559
*/
569560
public final DoubleQuery then(Function<DoubleQuery, DoubleTraverser> next) {
570-
DoubleAdvancer adv = item -> { throw new UnsupportedOperationException(
561+
DoubleAdvancer nextAdv = item -> { throw new UnsupportedOperationException(
571562
"Missing tryAdvance() implementation! Use the overloaded then() providing both Advancer and Traverser!");
572563
};
573-
return new DoubleQuery(adv, next.apply(this));
564+
return new DoubleQuery(nextAdv, next.apply(this));
574565
}
575566

576567
/**

src/main/java/org/jayield/primitives/dbl/advs/DoubleAdvancerDropWhile.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public boolean tryAdvance(DoubleYield yield) {
4242
yield.ret(item);
4343
}
4444
};
45-
while(upstream.tryAdvance(takeWhile) && !dropped) { }
45+
while(upstream.tryAdvance(takeWhile) && !dropped) {
46+
// Intentionally empty. Action specified on yield statement of tryAdvance().
47+
}
4648
return dropped;
4749
}
4850

src/main/java/org/jayield/primitives/dbl/advs/DoubleAdvancerFlatMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void traverse(DoubleYield yield) {
4343
@Override
4444
public boolean tryAdvance(DoubleYield yield) {
4545
while (!src.tryAdvance(yield)) {
46-
if(!upstream.tryAdvance((t) -> src = mapper.apply(t)))
46+
if(!upstream.tryAdvance(t -> src = mapper.apply(t)))
4747
return false;
4848
}
4949
return true;

src/main/java/org/jayield/primitives/dbl/advs/DoubleAdvancerLimit.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.jayield.primitives.dbl.advs;
1818

19-
import org.jayield.Yield;
2019
import org.jayield.primitives.dbl.DoubleAdvancer;
2120
import org.jayield.primitives.dbl.DoubleQuery;
2221
import org.jayield.primitives.dbl.DoubleTraverser;
@@ -35,13 +34,11 @@ public DoubleAdvancerLimit(DoubleQuery upstream, int n) {
3534

3635
@Override
3736
public void traverse(DoubleYield yield) {
38-
upstream.shortCircuit(item -> {
39-
if (count >= n) {
40-
Yield.bye();
41-
}
42-
count++;
43-
yield.ret(item);
44-
});
37+
if(count >= n)
38+
throw new IllegalStateException("Traverser has already been operated on or closed!");
39+
while(this.tryAdvance(yield)) {
40+
// Intentionally empty. Action specified on yield statement of tryAdvance().
41+
}
4542
}
4643

4744
@Override

0 commit comments

Comments
 (0)