View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    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.Assert;
    27  import org.junit.Test;
    28
    29  public class IteratorTransformTest {
    30
    31      @Test
    32      public void testIteration() {
    33          // arrange
    34          final List<Integer> input = Arrays.asList(1, 2, 3, 4, 12, 13);
    35
    36          // act
    37          final List<String> result = toList(new EvenCharIterator(input.iterator()));
    38
    39          // assert
    40          Assert.assertEquals(Arrays.asList("2", "4", "1", "2"), result);
    41      }
    42
    43      @Test(expected = NoSuchElementException.class)
    44      public void testThrowsNoSuchElement() {
    45          // arrange
    46          final List<Integer> input = Collections.emptyList();
    47          final EvenCharIterator it = new EvenCharIterator(input.iterator());
    48
    49          // act/assert
    50          Assert.assertFalse(it.hasNext());
    51          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          /** {@inheritDoc} */
    70          @Override
    71          protected void acceptInput(final Integer input) {
    72              // filter out odd integers
    73              final int value = input.intValue();
    74              if (value % 2 == 0) {
    75                  final char[] chars = (value + "").toCharArray();
    76
    77                  if (chars.length > 1) {
    78                      final List<String> strs = new ArrayList<>();
    79                      for (final char ch : chars) {
    80                          strs.add(ch + "");
    81                      }
    82
    83                      addAllOutput(strs);
    84                  } else if (chars.length == 1) {
    85                      addOutput(chars[0] + "");
    86                  }
    87              }
    88          }
    89      }
    90  }