com.codepoetics.protonpack.Indexed Java Examples

The following examples show how to use com.codepoetics.protonpack.Indexed. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StatefulTraversalTest.java    From protonpack with MIT License 5 votes vote down vote up
@Test
public void indexed() {
  assertThat(Statefully.index(Stream.of("a", "b", "c", "d", "e")).collect(Collectors.toList()),
      contains(
          Indexed.index(0, "a"),
          Indexed.index(1, "b"),
          Indexed.index(2, "c"),
          Indexed.index(3, "d"),
          Indexed.index(4, "e")
      ));
}
 
Example #2
Source File: StreamUtilsUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenStream_whenZipWithIndex_shouldReturnZippedStreamWithIndex() {
    Stream<String> source = Stream.of("Foo", "Bar", "Baz");

    List<Indexed<String>> zipped = StreamUtils.zipWithIndex(source).collect(Collectors.toList());

    assertThat(zipped, contains(Indexed.index(0, "Foo"), Indexed.index(1, "Bar"), Indexed.index(2, "Baz")));
}
 
Example #3
Source File: StreamIndicesUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenList_whenCalled_thenReturnListOfEvenIndexedStrings() {
    List<String> names = Arrays.asList("Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim");
    List<Indexed<String>> expectedResult = Arrays
      .asList(Indexed.index(0, "Afrim"), Indexed.index(2, "Besim"), Indexed
        .index(4, "Durim"));
    List<Indexed<String>> actualResult = StreamIndices.getEvenIndexedStrings(names);

    assertEquals(expectedResult, actualResult);
}
 
Example #4
Source File: StreamIndicesUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenList_whenCalled_thenReturnListOfOddIndexedStrings() {
    List<String> names = Arrays.asList("Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim");
    List<Indexed<String>> expectedResult = Arrays
      .asList(Indexed.index(1, "Bashkim"), Indexed.index(3, "Lulzim"), Indexed
        .index(5, "Shpetim"));
    List<Indexed<String>> actualResult = StreamIndices.getOddIndexedStrings(names);

    assertEquals(expectedResult, actualResult);
}
 
Example #5
Source File: ProtonpackUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenZippedWithIndex_thenZippedWithIndex() {
    Stream<String> streamOfClubs = Stream.of("Juventus", "Barcelona", "Liverpool");
    Set<Indexed<String>> zipsWithIndex = StreamUtils.zipWithIndex(streamOfClubs).collect(Collectors.toSet());
    assertThat(zipsWithIndex).contains(Indexed.index(0, "Juventus"), Indexed.index(1, "Barcelona"),
      Indexed.index(2, "Liverpool"));
}
 
Example #6
Source File: Statefully.java    From protonpack with MIT License 4 votes vote down vote up
public static <T> Stream<Indexed<T>> index(Stream<T> input) {
  return Statefully.transform(
      input,
      StateMachine.create(() -> 0L, (index, item) -> Transition.to(index + 1, Indexed.index(index, item)))
  );
}
 
Example #7
Source File: StreamUtilsExample.java    From tutorials with MIT License 4 votes vote down vote up
public void zipAStreamWithIndex() {
    Stream<String> source = Stream.of("Foo", "Bar", "Baz");
    List<Indexed<String>> zipped = StreamUtils.zipWithIndex(source).collect(Collectors.toList());
}
 
Example #8
Source File: StreamIndices.java    From tutorials with MIT License 4 votes vote down vote up
public static List<Indexed<String>> getEvenIndexedStrings(List<String> names) {
    List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
        .filter(i -> i.getIndex() % 2 == 0)
        .collect(Collectors.toList());
    return list;
}
 
Example #9
Source File: StreamIndices.java    From tutorials with MIT License 4 votes vote down vote up
public static List<Indexed<String>> getOddIndexedStrings(List<String> names) {
    List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
        .filter(i -> i.getIndex() % 2 == 1)
        .collect(Collectors.toList());
    return list;
}