Skip to content

Commit e3cdfea

Browse files
committed
#map: adding MappingIterable, ImmutableIterator, and MonadicFunction; creating IterableMatcher to help with JUnit assertions
1 parent 6bdf8ad commit e3cdfea

6 files changed

Lines changed: 171 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.jnape.palatable.lambda;
2+
3+
public abstract class MonadicFunction<Input, Output> {
4+
public abstract Output apply(Input input);
5+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.jnape.palatable.lambda.iterables;
2+
3+
import com.jnape.palatable.lambda.MonadicFunction;
4+
import com.jnape.palatable.lambda.iterators.ImmutableIterator;
5+
6+
import java.util.Iterator;
7+
8+
public class MappingIterable<Input, Output> implements Iterable<Output> {
9+
10+
private final MonadicFunction<? super Input, ? extends Output> function;
11+
private final Iterable<Input> xs;
12+
13+
public MappingIterable(MonadicFunction<? super Input, ? extends Output> function, Iterable<Input> xs) {
14+
this.xs = xs;
15+
this.function = function;
16+
}
17+
18+
@Override
19+
public Iterator<Output> iterator() {
20+
final Iterator<Input> xsIterator = xs.iterator();
21+
22+
return new ImmutableIterator<Output>() {
23+
@Override
24+
public boolean hasNext() {
25+
return xsIterator.hasNext();
26+
}
27+
28+
@Override
29+
public Output next() {
30+
return function.apply(xsIterator.next());
31+
}
32+
};
33+
}
34+
35+
public static <Input, Output> MappingIterable<Input, Output> map(
36+
MonadicFunction<? super Input, ? extends Output> function,
37+
Iterable<Input> xs) {
38+
return new MappingIterable<Input, Output>(function, xs);
39+
}
40+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.jnape.palatable.lambda.iterators;
2+
3+
import java.util.Iterator;
4+
5+
public abstract class ImmutableIterator<Element> implements Iterator<Element> {
6+
7+
@Override
8+
public final void remove() {
9+
throw new UnsupportedOperationException("Iterator is immutable.");
10+
}
11+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.jnape.palatable.lambda.iterables;
2+
3+
import com.jnape.palatable.lambda.MonadicFunction;
4+
import org.junit.Test;
5+
6+
import static com.jnape.palatable.lambda.iterables.MappingIterable.map;
7+
import static java.util.Arrays.asList;
8+
import static org.junit.Assert.assertThat;
9+
import static testsupport.matchers.IterableMatcher.iterates;
10+
11+
public class MappingIterableTest {
12+
13+
@Test
14+
public void mapsInputIterableIntoOutputIterable() {
15+
Iterable<String> strings = asList("one", "two", "three");
16+
MappingIterable<String, Integer> stringsToLengths = map(new MonadicFunction<String, Integer>() {
17+
@Override
18+
public Integer apply(String string) {
19+
return string.length();
20+
}
21+
}, strings);
22+
23+
assertThat(stringsToLengths, iterates(3, 3, 5));
24+
}
25+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.jnape.palatable.lambda.iterators;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
public class ImmutableIteratorTest {
7+
8+
private ImmutableIterator immutableIterator;
9+
10+
@Before
11+
public void setUp() {
12+
immutableIterator = new ImmutableIterator() {
13+
@Override
14+
public boolean hasNext() {
15+
return false;
16+
}
17+
18+
@Override
19+
public Object next() {
20+
return null;
21+
}
22+
};
23+
}
24+
25+
@Test(expected = UnsupportedOperationException.class)
26+
public void doesNotSupportRemove() {
27+
immutableIterator.remove();
28+
}
29+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package testsupport.matchers;
2+
3+
import org.hamcrest.BaseMatcher;
4+
import org.hamcrest.Description;
5+
6+
import java.util.Iterator;
7+
8+
import static java.util.Arrays.asList;
9+
10+
public class IterableMatcher<Element> extends BaseMatcher<Iterable<Element>> {
11+
12+
private final Iterable<Element> expected;
13+
14+
public IterableMatcher(Iterable<Element> expected) {
15+
this.expected = expected;
16+
}
17+
18+
@Override
19+
public boolean matches(Object actual) {
20+
return actual instanceof Iterable && iteratesExpectedElementsInOrder((Iterable) actual);
21+
}
22+
23+
@Override
24+
public void describeTo(Description description) {
25+
description.appendValue(expected);
26+
}
27+
28+
@Override
29+
public void describeMismatch(Object item, Description description) {
30+
if (item instanceof Iterable)
31+
description.appendText("was ").appendText(stringify((Iterable) item));
32+
else
33+
super.describeMismatch(item, description);
34+
}
35+
36+
private boolean iteratesExpectedElementsInOrder(Iterable actual) {
37+
Iterator actualIterator = actual.iterator();
38+
Iterator expectedIterator = expected.iterator();
39+
40+
while (actualIterator.hasNext() && expectedIterator.hasNext())
41+
if (!actualIterator.next().equals(expectedIterator.next()))
42+
return false;
43+
44+
return actualIterator.hasNext() == expectedIterator.hasNext();
45+
}
46+
47+
private String stringify(Iterable iterable) {
48+
StringBuilder stringBuilder = new StringBuilder().append("<[");
49+
Iterator iterator = iterable.iterator();
50+
while (iterator.hasNext()) {
51+
stringBuilder.append(iterator.next());
52+
if (iterator.hasNext())
53+
stringBuilder.append(", ");
54+
}
55+
return stringBuilder.append("]>").toString();
56+
}
57+
58+
public static <Element> IterableMatcher<Element> iterates(Element... elements) {
59+
return new IterableMatcher<Element>(asList(elements));
60+
}
61+
}

0 commit comments

Comments
 (0)