Skip to content

Commit 204324b

Browse files
committed
InGroupsOf: fixing a bug where the iterator cursor doesn't advance on next()
IterableMatcher: adding iteration check to ensure iterable can be finitely iterated to help catch previously mentioned InGroupsOf bug pom: adding dependency on loan-shark
1 parent 142a2bf commit 204324b

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

‎pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
<properties>
4747
<commons-lang3.version>3.1</commons-lang3.version>
48+
<loan-shark.version>1.0</loan-shark.version>
4849
</properties>
4950

5051
<dependencies>
@@ -56,6 +57,11 @@
5657
<groupId>org.mockito</groupId>
5758
<artifactId>mockito-all</artifactId>
5859
</dependency>
60+
<dependency>
61+
<groupId>com.jnape.loan-shark</groupId>
62+
<artifactId>loan-shark</artifactId>
63+
<version>${loan-shark.version}</version>
64+
</dependency>
5965
<dependency>
6066
<groupId>org.apache.commons</groupId>
6167
<artifactId>commons-lang3</artifactId>

‎src/main/java/com/jnape/palatable/lambda/functions/InGroupsOf.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import com.jnape.palatable.lambda.iterators.ImmutableIterator;
44

5+
import java.util.ArrayList;
56
import java.util.Iterator;
7+
import java.util.List;
68

79
import static com.jnape.palatable.lambda.functions.Take.take;
810
import static com.jnape.palatable.lambda.staticfactory.IterableFactory.iterable;
@@ -22,7 +24,10 @@ public boolean hasNext() {
2224

2325
@Override
2426
public Iterable<A> next() {
25-
return take(k, iterable(asIterator));
27+
List<A> group = new ArrayList<A>();
28+
for (A a : take(k, iterable(asIterator)))
29+
group.add(a);
30+
return group;
2631
}
2732
};
2833
}

‎src/test/java/testsupport/matchers/IterableMatcher.java‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package testsupport.matchers;
22

3+
import com.jnape.loanshark.annotation.Todo;
34
import org.hamcrest.BaseMatcher;
45
import org.hamcrest.Description;
56
import org.hamcrest.Matcher;
@@ -12,15 +13,17 @@
1213

1314
public class IterableMatcher<Element> extends BaseMatcher<Iterable<Element>> {
1415

16+
private final int length;
1517
private final Iterable<Element> expected;
1618

17-
public IterableMatcher(Iterable<Element> expected) {
19+
public IterableMatcher(int length, Iterable<Element> expected) {
20+
this.length = length;
1821
this.expected = expected;
1922
}
2023

2124
@Override
2225
public boolean matches(Object actual) {
23-
return actual instanceof Iterable && iterablesIterateSameElementsInOrder((Iterable) expected, (Iterable) actual);
26+
return actual instanceof Iterable && iterablesIterateSameElementsInOrder((Iterable) expected, (Iterable) actual) && finitelyIterable((Iterable) actual);
2427
}
2528

2629
@Override
@@ -36,6 +39,7 @@ public void describeMismatch(Object item, Description description) {
3639
super.describeMismatch(item, description);
3740
}
3841

42+
@Todo(created = "2/17/2014", author = "jnape", description = "This is a disaster. So many methods in one")
3943
private boolean iterablesIterateSameElementsInOrder(Iterable expected, Iterable actual) {
4044
Iterator actualIterator = actual.iterator();
4145
Iterator expectedIterator = expected.iterator();
@@ -54,6 +58,18 @@ private boolean iterablesIterateSameElementsInOrder(Iterable expected, Iterable
5458
return actualIterator.hasNext() == expectedIterator.hasNext();
5559
}
5660

61+
private boolean finitelyIterable(Iterable iterable) {
62+
Iterator iterator = iterable.iterator();
63+
int i = 0;
64+
while (iterator.hasNext()) {
65+
iterator.next();
66+
if (i++ > length)
67+
return false;
68+
}
69+
return true;
70+
}
71+
72+
@Todo(created = "02/17/2014", author = "jnape", description = "Pull this out into a separate class")
5773
private String stringify(Iterable iterable) {
5874
StringBuilder stringBuilder = new StringBuilder().append("[");
5975
Iterator iterator = iterable.iterator();
@@ -70,10 +86,10 @@ private String stringify(Iterable iterable) {
7086
}
7187

7288
public static <Element> IterableMatcher<Element> iterates(Element... elements) {
73-
return new IterableMatcher<Element>(asList(elements));
89+
return new IterableMatcher<Element>(elements.length, asList(elements));
7490
}
7591

7692
public static <A> Matcher<Iterable<A>> isEmpty() {
77-
return new IterableMatcher<A>(new ArrayList<A>());
93+
return new IterableMatcher<A>(0, new ArrayList<A>());
7894
}
7995
}

0 commit comments

Comments
 (0)