1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.core.internal;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.NoSuchElementException;
25
26 import org.junit.jupiter.api.Assertions;
27 import org.junit.jupiter.api.Test;
28
29 class IteratorTransformTest {
30
31 @Test
32 void testIteration() {
33
34 final List<Integer> input = Arrays.asList(1, 2, 3, 4, 12, 13);
35
36
37 final List<String> result = toList(new EvenCharIterator(input.iterator()));
38
39
40 Assertions.assertEquals(Arrays.asList("2", "4", "1", "2"), result);
41 }
42
43 @Test
44 void testThrowsNoSuchElement() {
45
46 final List<Integer> input = Collections.emptyList();
47 final EvenCharIterator it = new EvenCharIterator(input.iterator());
48
49
50 Assertions.assertFalse(it.hasNext());
51 Assertions.assertThrows(NoSuchElementException.class, it::next);
52 }
53
54 private static <T> List<T> toList(final Iterator<T> it) {
55 final List<T> result = new ArrayList<>();
56 while (it.hasNext()) {
57 result.add(it.next());
58 }
59
60 return result;
61 }
62
63 private static class EvenCharIterator extends IteratorTransform<Integer, String> {
64
65 EvenCharIterator(final Iterator<Integer> inputIterator) {
66 super(inputIterator);
67 }
68
69
70 @Override
71 protected void acceptInput(final Integer input) {
72
73 final int value = input;
74 if (value % 2 == 0) {
75 final char[] chars = (Integer.toString(value)).toCharArray();
76
77 if (chars.length > 1) {
78 final List<String> strs = new ArrayList<>();
79 for (final char ch : chars) {
80 strs.add(String.valueOf(ch));
81 }
82
83 addAllOutput(strs);
84 } else if (chars.length == 1) {
85 addOutput(String.valueOf(chars[0]));
86 }
87 }
88 }
89 }
90 }