예제 #1
0
  @Test
  public void shouldRemoveTheElementFromTheUnderlyingIterator() {
    // Given
    Collection<String> actualList = collectionWith("one", "two", "three", "four", "five");
    Collection<String> expectedList = collectionWith("three", "four", "five");
    Iterator<String> iterator = actualList.iterator();

    // When
    FilteredIterator<String> filteredIterator =
        new FilteredIterator<String>(
            iterator,
            new Predicate<String>() {
              public boolean evaluate(String item) {
                return item.contains("o");
              }
            });

    filteredIterator.next();
    filteredIterator.remove();
    filteredIterator.next();
    filteredIterator.remove();

    // Then
    assertThat(expectedList, is(actualList));
  }
예제 #2
0
  @Test
  public void shouldNotRemoveAnElementThatDoesNotMatchTheSuppliedPredicate() throws Exception {
    // Given
    Collection<String> initialElements = collectionWith("one", "three");
    Collection<String> expectedElements = collectionWith("three");

    // When
    FilteredIterator<String> filteredIterator =
        new FilteredIterator<String>(
            initialElements.iterator(),
            new Predicate<String>() {
              public boolean evaluate(String item) {
                return item.contains("o");
              }
            });

    // Then
    assertThat(filteredIterator.next(), is("one"));
    filteredIterator.remove();
    assertThat(filteredIterator.hasNext(), is(false));

    try {
      filteredIterator.remove();
      fail("Expected an IllegalStateException");
    } catch (IllegalStateException exception) {
      // continue
    }

    assertThat(initialElements, is(expectedElements));
  }
예제 #3
0
  @Test
  public void shouldAllowHasNextToBeCalledMultipleTimesWithoutProgressingTheIterator() {
    // Given
    Iterable<String> iterable = iterableWith("one", "two", "three", "four", "five");
    Predicate<String> predicate =
        new Predicate<String>() {
          public boolean evaluate(String item) {
            return item.contains("o");
          }
        };

    // When
    FilteredIterator<String> iterator =
        new FilteredIterator<String>(iterable.iterator(), predicate);

    // Then
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.next(), is("one"));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.next(), is("two"));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.next(), is("four"));
    assertThat(iterator.hasNext(), is(false));
    assertThat(iterator.hasNext(), is(false));
  }
예제 #4
0
  @Test(expected = NoSuchElementException.class)
  public void shouldThrowNoSuchElementExceptionIfDoesntHaveNext() {
    // Given
    Iterable<String> iterable = iterableWith("one", "two", "three", "four", "five");
    Predicate<String> predicate =
        new Predicate<String>() {
          public boolean evaluate(String item) {
            return item.contains("o");
          }
        };

    // When
    FilteredIterator<String> iterator =
        new FilteredIterator<String>(iterable.iterator(), predicate);
    iterator.next();
    iterator.next();
    iterator.next();
    iterator.next();

    // Then a NoSuchElementException is thrown
  }
예제 #5
0
  @Test(expected = IllegalStateException.class)
  public void shouldThrowAnIllegalStateExceptionIfNextHasNotBeenCalledBeforeRemove()
      throws Exception {
    // Given
    Iterator<String> iterator = iterableWith("one", "two", "three").iterator();

    // When
    FilteredIterator<String> filteredIterator =
        new FilteredIterator<String>(
            iterator,
            new Predicate<String>() {
              public boolean evaluate(String item) {
                return item.contains("o");
              }
            });

    filteredIterator.hasNext();
    filteredIterator.remove();

    // Then an IllegalStateException should be thrown
  }
예제 #6
0
  @Test
  public void shouldAllowNullValuesInTheIterator() throws Exception {
    // Given
    Iterator<Integer> delegateIterator = iterableWith(1, null, 10, 5).iterator();

    // When
    FilteredIterator<Integer> iterator =
        new FilteredIterator<Integer>(
            delegateIterator,
            new Predicate<Integer>() {
              public boolean evaluate(Integer item) {
                return item == null || item % 10 != 0;
              }
            });

    // Then
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.next(), is(1));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.next(), is(nullValue()));
    assertThat(iterator.hasNext(), is(true));
    assertThat(iterator.next(), is(5));
    assertThat(iterator.hasNext(), is(false));
  }