com.codepoetics.protonpack.StreamUtils Java Examples

The following examples show how to use com.codepoetics.protonpack.StreamUtils. 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: Pin.java    From BlockMap with MIT License 6 votes vote down vote up
@Override
protected PopOver initInfo() {
	PopOver info = super.initInfo();
	GridPane popContent = new GridPane();
	popContent.getStyleClass().add("grid");

	/* Image+Text for the popover */
	StreamUtils.zipWithIndex(pinCount.entrySet().stream()).forEach(e -> {
		ImageView img = new ImageView(e.getValue().getKey().image);
		img.setSmooth(false);
		img.setPreserveRatio(true);
		Label label1 = new Label(e.getValue().getKey().toString(), img);
		img.fitHeightProperty().bind(Bindings.createDoubleBinding(() -> label1.getFont().getSize() * 1.3, label1.fontProperty()));
		popContent.add(label1, 0, (int) e.getIndex());
		Label label2 = new Label(String.format("%dx", e.getValue().getValue()));
		popContent.add(label2, 1, (int) e.getIndex());
	});

	info.setContentNode(popContent);
	return info;
}
 
Example #2
Source File: ProtonpackUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenMultipleStream_whenZipped_thenZipped() {
    String[] clubs = {"Juventus", "Barcelona", "Liverpool", "PSG"};
    String[] players = {"Ronaldo", "Messi", "Salah"};
    Set<String> zippedFrom2Sources = StreamUtils
      .zip(stream(clubs), stream(players), (club, player) -> club + " " + player)
      .collect(Collectors.toSet());
    assertThat(zippedFrom2Sources).contains("Juventus Ronaldo", "Barcelona Messi", "Liverpool Salah");

    String[] leagues = {"Serie A", "La Liga", "Premier League"};
    Set<String> zippedFrom3Sources = StreamUtils.zip(stream(clubs), stream(players), stream(leagues),
      (club, player, league) -> club + " " + player + " " + league).collect(Collectors.toSet());
    assertThat(zippedFrom3Sources).contains("Juventus Ronaldo Serie A", "Barcelona Messi La Liga",
      "Liverpool Salah Premier League");
}
 
Example #3
Source File: StreamUtilsUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenTwoStreams_whenZip_shouldReturnZippedStream() {
    Stream<String> streamA = Stream.of("A", "B", "C");
    Stream<String> streamB = Stream.of("Apple", "Banana", "Carrot");

    List<String> zipped = StreamUtils.zip(streamA, streamB, (a, b) -> a + " is for " + b).collect(Collectors.toList());

    assertThat(zipped, contains("A is for Apple", "B is for Banana", "C is for Carrot"));
}
 
Example #4
Source File: StreamUtilsUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenThreeStreams_whenZip_shouldReturnZippedStream() {
    Stream<String> streamA = Stream.of("A", "B", "C");
    Stream<String> streamB = Stream.of("aggravating", "banausic", "complaisant");
    Stream<String> streamC = Stream.of("Apple", "Banana", "Carrot");

    List<String> zipped = StreamUtils.zip(streamA, streamB, streamC, (a, b, c) -> a + " is for " + b + " " + c).collect(Collectors.toList());

    assertThat(zipped, contains("A is for aggravating Apple", "B is for banausic Banana", "C is for complaisant Carrot"));
}
 
Example #5
Source File: StreamUtilsUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
// givenThreeStreams_whenMerge_shouldReturnMergedStream
public void givenThreeStreams_whenMerge_shouldReturnMergedStream() {
    Stream<String> streamA = Stream.of("A", "B", "C");
    Stream<String> streamB = Stream.of("apple", "banana", "carrot", "date");
    Stream<String> streamC = Stream.of("fritter", "split", "cake", "roll", "pastry");

    Stream<List<String>> merged = StreamUtils.mergeToList(streamA, streamB, streamC);

    assertThat(merged.collect(toList()), contains(asList("A", "apple", "fritter"), asList("B", "banana", "split"), asList("C", "carrot", "cake"), asList("date", "roll"), asList("pastry")));
}
 
Example #6
Source File: Pin.java    From BlockMap with MIT License 5 votes vote down vote up
@Override
protected PopOver initInfo() {
	PopOver info = super.initInfo();

	GridPane popContent = new GridPane();
	popContent.getStyleClass().add("grid");
	info.setContentNode(popContent);

	StreamUtils.zipWithIndex(chunkGeneration.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet().stream())
			.forEach(index -> {
				popContent.add(new Label(index.getValue().getKey() + ":"), 0, (int) index.getIndex());
				popContent.add(new Label(index.getValue().getValue() + " chunks"), 1, (int) index.getIndex());
			});
	return info;
}
 
Example #7
Source File: StreamUtilsUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
// givenInfiniteStream_whenTakeWhile10_shouldReturnStreamOfSize10
public void givenInfiniteStream_whenTakeWhile10_shouldReturnStream() {
    Stream<Integer> infiniteInts = Stream.iterate(0, i -> i + 1);
    Stream<Integer> finiteInts = StreamUtils.takeWhile(infiniteInts, i -> i < 10);

    assertThat(finiteInts.collect(Collectors.toList()), hasSize(10));
}
 
Example #8
Source File: StreamUtilsUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenInfiniteStream_whenTakeUntil10_shouldReturnStreamUpto10() {
    Stream<Integer> infiniteInts = Stream.iterate(0, i -> i + 1);
    Stream<Integer> finiteInts = StreamUtils.takeUntil(infiniteInts, i -> i > 10);

    assertThat(finiteInts.collect(Collectors.toList()), hasSize(11));
}
 
Example #9
Source File: StreamUtilsUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenIntegerStreamOfTen_whenSkipWhileLessThanFour_shouldReturnStreamFromFourToTen() {
    Stream<Integer> ints = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    Stream<Integer> skipped = StreamUtils.skipWhile(ints, i -> i < 4);
    List<Integer> collected = skipped.collect(Collectors.toList());

    assertThat(collected, contains(4, 5, 6, 7, 8, 9, 10));
}
 
Example #10
Source File: StreamUtilsUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenIntegerStreamOfTen_whenSkipUntilFour_shouldReturnStreamFromFiveToTen() {
    Stream<Integer> ints = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    Stream<Integer> skipped = StreamUtils.skipUntil(ints, i -> i > 4);
    List<Integer> collected = skipped.collect(Collectors.toList());

    assertThat(collected, contains(5, 6, 7, 8, 9, 10));
}
 
Example #11
Source File: StreamUtilsUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void giveIntegerStream_whenGroupRuns_shouldReturnListGroupItems() {
    Stream<Integer> integerStream = Stream.of(1, 1, 2, 2, 3, 4, 5);
    List<List<Integer>> runs = StreamUtils.groupRuns(integerStream).collect(toList());

    assertThat(runs, contains(asList(1, 1), asList(2, 2), asList(3), asList(4), asList(5)));
}
 
Example #12
Source File: StreamUtilsUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenIntegerStream_whenWindowed_shouldReturnListOfListOfItemsOfWindowSize() {
    Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);

    List<List<Integer>> windows = StreamUtils.windowed(integerStream, 2).collect(toList());

    assertThat(windows, contains(asList(1, 2), asList(2, 3), asList(3, 4), asList(4, 5)));
}
 
Example #13
Source File: StreamUtilsUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
// givenIntegerStream_whenWindowedWithWindowSizeAndSkip_shouldReturnListOfListOfWindowSizeAddingASkip
public void givenIntegerStream_whenWindowedWithWindowSizeAndSkip_shouldReturnListOfListOfWindowSizeAddingASkip() {
    Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);

    List<List<Integer>> windows = StreamUtils.windowed(integerStream, 3, 2).collect(toList());

    assertThat(windows, contains(asList(1, 2, 3), asList(3, 4, 5)));
}
 
Example #14
Source File: StreamUtilsUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenIntegerStream_whenWindowedWithWindowSizeAndSkipAndAllowLesserSize_shouldReturnListOfListOfInteger() {
    Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);

    List<List<Integer>> windows = StreamUtils.windowed(integerStream, 2, 2, true).collect(toList());

    assertThat(windows, contains(asList(1, 2), asList(3, 4), asList(5)));
}
 
Example #15
Source File: StreamUtilsUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
// givenThreeStreams_whenInterleave_shouldReturnRoundRobinInterleavingStream
public void givenThreeStreams_whenInterleave_shouldReturnRoundRobinInterleavingStream() {
    Stream<String> streamA = Stream.of("Peter", "Paul", "Mary");
    Stream<String> streamB = Stream.of("A", "B", "C", "D", "E");
    Stream<String> streamC = Stream.of("foo", "bar", "baz", "xyzzy");

    Stream<String> interleaved = StreamUtils.interleave(Selectors.roundRobin(), streamA, streamB, streamC);

    assertThat(interleaved.collect(Collectors.toList()), contains("Peter", "A", "foo", "Paul", "B", "bar", "Mary", "C", "baz", "D", "xyzzy", "E"));
}
 
Example #16
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 #17
Source File: ProtonpackUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenMultipleStream_whenMerged_thenMerged() {
    Stream<String> streamOfClubs = Stream.of("Juventus", "Barcelona", "Liverpool", "PSG");
    Stream<String> streamOfPlayers = Stream.of("Ronaldo", "Messi", "Salah");
    Stream<String> streamOfLeagues = Stream.of("Serie A", "La Liga", "Premier League");

    Set<String> merged = StreamUtils.merge(() -> "", (valOne, valTwo) -> valOne + " " + valTwo, streamOfClubs,
      streamOfPlayers, streamOfLeagues).collect(Collectors.toSet());

    assertThat(merged)
      .contains(" Juventus Ronaldo Serie A", " Barcelona Messi La Liga", " Liverpool Salah Premier League",
        " PSG");
}
 
Example #18
Source File: ProtonpackUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenMultipleStream_whenMergedToList_thenMergedToList() {
    Stream<String> streamOfClubs = Stream.of("Juventus", "Barcelona", "PSG");
    Stream<String> streamOfPlayers = Stream.of("Ronaldo", "Messi");

    List<List<String>> mergedListOfList = StreamUtils.mergeToList(streamOfClubs, streamOfPlayers)
      .collect(Collectors.toList());
    assertThat(mergedListOfList.get(0)).isInstanceOf(List.class);
    assertThat(mergedListOfList.get(0)).containsExactly("Juventus", "Ronaldo");
    assertThat(mergedListOfList.get(1)).containsExactly("Barcelona", "Messi");
    assertThat(mergedListOfList.get(2)).containsExactly("PSG");
}
 
Example #19
Source File: ProtonpackUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenMultipleStream_whenInterleaved_thenInterleaved() {
    Stream<String> streamOfClubs = Stream.of("Juventus", "Barcelona", "Liverpool");
    Stream<String> streamOfPlayers = Stream.of("Ronaldo", "Messi");
    Stream<String> streamOfLeagues = Stream.of("Serie A", "La Liga");

    List<String> interleavedList = StreamUtils
      .interleave(Selectors.roundRobin(), streamOfClubs, streamOfPlayers, streamOfLeagues)
      .collect(Collectors.toList());

    assertThat(interleavedList)
      .hasSize(7)
      .containsExactly("Juventus", "Ronaldo", "Serie A", "Barcelona", "Messi", "La Liga", "Liverpool");
}
 
Example #20
Source File: ProtonpackUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenSkippedUntil_thenSkippedUntil() {
    Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    List<Integer> skippedUntilGreaterThan5 = StreamUtils.skipUntil(stream(numbers), i -> i > 5)
      .collect(Collectors.toList());
    assertThat(skippedUntilGreaterThan5).containsExactly(6, 7, 8, 9, 10);

    List<Integer> skippedUntilLessThanEquals5 = StreamUtils.skipUntil(stream(numbers), i -> i <= 5)
      .collect(Collectors.toList());
    assertThat(skippedUntilLessThanEquals5).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
}
 
Example #21
Source File: ProtonpackUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenSkippedWhile_thenSkippedWhile() {
    Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    List<Integer> skippedWhileLessThanEquals5 = StreamUtils.skipWhile(stream(numbers), i -> i <= 5)
      .collect(Collectors.toList());
    assertThat(skippedWhileLessThanEquals5).containsExactly(6, 7, 8, 9, 10);

    List<Integer> skippedWhileGreaterThan5 = StreamUtils.skipWhile(stream(numbers), i -> i > 5)
      .collect(Collectors.toList());
    assertThat(skippedWhileGreaterThan5).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
}
 
Example #22
Source File: ProtonpackUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenWindowed_thenWindowed() {
    Integer[] numbers = {1, 2, 3, 4, 5, 6, 7};

    List<List<Integer>> windowedWithSkip1 = StreamUtils.windowed(stream(numbers), 3, 1)
      .collect(Collectors.toList());
    assertThat(windowedWithSkip1)
      .containsExactly(asList(1, 2, 3), asList(2, 3, 4), asList(3, 4, 5), asList(4, 5, 6),
        asList(5, 6, 7));

    List<List<Integer>> windowedWithSkip2 = StreamUtils.windowed(stream(numbers), 3, 2)
      .collect(Collectors.toList());
    assertThat(windowedWithSkip2).containsExactly(asList(1, 2, 3), asList(3, 4, 5), asList(5, 6, 7));
}
 
Example #23
Source File: ProtonpackUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenAggregated_thenAggregated() {
    Integer[] numbers = {1, 2, 2, 3, 4, 4, 4, 5};
    List<List<Integer>> aggregated = StreamUtils
      .aggregate(stream(numbers), (int1, int2) -> int1.compareTo(int2) == 0)
      .collect(Collectors.toList());
    assertThat(aggregated).containsExactly(asList(1), asList(2, 2), asList(3), asList(4, 4, 4), asList(5));

    List<List<Integer>> aggregatedFixSize = StreamUtils.aggregate(stream(numbers), 5).collect(Collectors.toList());
    assertThat(aggregatedFixSize).containsExactly(asList(1, 2, 2, 3, 4), asList(4, 4, 5));
}
 
Example #24
Source File: ProtonpackUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenGroupedRun_thenGroupedRun() {
    Integer[] numbers = {1, 1, 2, 3, 4, 4, 5};
    List<List<Integer>> grouped = StreamUtils.groupRuns(stream(numbers)).collect(Collectors.toList());
    assertThat(grouped).containsExactly(asList(1, 1), asList(2), asList(3), asList(4, 4), asList(5));

    Integer[] numbers2 = {1, 2, 3, 1};
    List<List<Integer>> grouped2 = StreamUtils.groupRuns(stream(numbers2)).collect(Collectors.toList());
    assertThat(grouped2).containsExactly(asList(1), asList(2), asList(3), asList(1));
}
 
Example #25
Source File: ProtonpackUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenAggregatedOnListCondition_thenAggregatedOnListCondition() {
    Integer[] numbers = {1, 1, 2, 3, 4, 4, 5};
    Stream<List<Integer>> aggregated = StreamUtils.aggregateOnListCondition(stream(numbers),
      (currentList, nextInt) -> currentList.stream().mapToInt(Integer::intValue).sum() + nextInt <= 5);
    assertThat(aggregated).containsExactly(asList(1, 1, 2), asList(3), asList(4), asList(4), asList(5));
}
 
Example #26
Source File: StreamUtilsExample.java    From tutorials with MIT License 5 votes vote down vote up
public void windowedStream() {
    Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);

    List<List<Integer>> windows = StreamUtils.windowed(integerStream, 2).collect(toList());
    List<List<Integer>> windowsWithSkipIndex = StreamUtils.windowed(integerStream, 3, 2).collect(toList());
    List<List<Integer>> windowsWithSkipIndexAndAllowLowerSize = StreamUtils.windowed(integerStream, 2, 2, true).collect(toList());

}
 
Example #27
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 #28
Source File: StreamUtilsExample.java    From tutorials with MIT License 5 votes vote down vote up
public void interleavingStreamsUsingRoundRobin() {
    Stream<String> streamA = Stream.of("Peter", "Paul", "Mary");
    Stream<String> streamB = Stream.of("A", "B", "C", "D", "E");
    Stream<String> streamC = Stream.of("foo", "bar", "baz", "xyzzy");

    Stream<String> interleaved = StreamUtils.interleave(Selectors.roundRobin(), streamA, streamB, streamC);
}
 
Example #29
Source File: StreamUtilsExample.java    From tutorials with MIT License 5 votes vote down vote up
public void mergeThreeStreams() {
    Stream<String> streamA = Stream.of("A", "B", "C");
    Stream<String> streamB = Stream.of("apple", "banana", "carrot", "date");
    Stream<String> streamC = Stream.of("fritter", "split", "cake", "roll", "pastry");

    Stream<List<String>> merged = StreamUtils.mergeToList(streamA, streamB, streamC);
}
 
Example #30
Source File: StreamUtilsExample.java    From tutorials with MIT License 5 votes vote down vote up
public void zipThreeStreams() {
    Stream<String> streamA = Stream.of("A", "B", "C");
    Stream<String> streamB = Stream.of("aggravating", "banausic", "complaisant");
    Stream<String> streamC = Stream.of("Apple", "Banana", "Carrot");

    List<String> zipped = StreamUtils.zip(streamA, streamB, streamC, (a, b, c) -> a + " is for " + b + " " + c).collect(Collectors.toList());
}