Merge pull request #5311 from eugenp/proton
Refactor ProtonPack samples
This commit is contained in:
		
						commit
						54c1fcb4a9
					
				| @ -4,10 +4,9 @@ import com.codepoetics.protonpack.Indexed; | ||||
| import com.codepoetics.protonpack.StreamUtils; | ||||
| import com.codepoetics.protonpack.collectors.CollectorUtils; | ||||
| import com.codepoetics.protonpack.collectors.NonUniqueValueException; | ||||
| import com.codepoetics.protonpack.selectors.Selector; | ||||
| import com.codepoetics.protonpack.selectors.Selectors; | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import java.util.Arrays; | ||||
| import java.util.List; | ||||
| import java.util.Optional; | ||||
| import java.util.Set; | ||||
| @ -21,7 +20,6 @@ import static java.util.Arrays.stream; | ||||
| import static org.assertj.core.api.Assertions.assertThat; | ||||
| import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||||
| 
 | ||||
| @SuppressWarnings("unchecked") | ||||
| public class ProtonpackUnitTest { | ||||
|     @Test | ||||
|     public void whenTakeWhile_thenTakenWhile() { | ||||
| @ -41,7 +39,8 @@ public class ProtonpackUnitTest { | ||||
|     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) | ||||
|         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"); | ||||
| 
 | ||||
| @ -69,7 +68,8 @@ public class ProtonpackUnitTest { | ||||
|         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", | ||||
|         assertThat(merged) | ||||
|           .contains(" Juventus Ronaldo Serie A", " Barcelona Messi La Liga", " Liverpool Salah Premier League", | ||||
|             " PSG"); | ||||
|     } | ||||
| 
 | ||||
| @ -92,26 +92,20 @@ public class ProtonpackUnitTest { | ||||
|         Stream<String> streamOfPlayers = Stream.of("Ronaldo", "Messi"); | ||||
|         Stream<String> streamOfLeagues = Stream.of("Serie A", "La Liga"); | ||||
| 
 | ||||
|         AtomicInteger counter = new AtomicInteger(0); | ||||
|         Selector roundRobinSelector = (o) -> { | ||||
|             Object[] vals = (Object[]) o; | ||||
|             while (counter.get() >= vals.length || vals[counter.get()] == null) { | ||||
|                 if (counter.incrementAndGet() >= vals.length) | ||||
|                     counter.set(0); | ||||
|             } | ||||
|             return counter.getAndIncrement(); | ||||
|         }; | ||||
|         Stream<String> interleavedStream = StreamUtils.interleave(roundRobinSelector, streamOfClubs, streamOfPlayers, | ||||
|             streamOfLeagues); | ||||
|         List<String> interleavedList = interleavedStream.collect(Collectors.toList()); | ||||
|         assertThat(interleavedList).containsExactly("Juventus", "Ronaldo", "Serie A", "Barcelona", "Messi", "La Liga", | ||||
|             "Liverpool"); | ||||
|         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"); | ||||
|     } | ||||
| 
 | ||||
|     @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()); | ||||
|         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) | ||||
| @ -126,37 +120,38 @@ public class ProtonpackUnitTest { | ||||
|           .collect(Collectors.toList()); | ||||
|         assertThat(skippedWhileLessThanEquals5).containsExactly(6, 7, 8, 9, 10); | ||||
| 
 | ||||
|         List<Integer> skippedWhileGreaterThan5 = StreamUtils.skipWhile(stream(numbers), i -> i > 5).collect(Collectors.toList()); | ||||
|         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); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenFibonacciGenerator_whenUnfolded_thenUnfolded() { | ||||
|         AtomicInteger lastValue = new AtomicInteger(0); | ||||
|         Function<Integer, Optional<Integer>> fibonacciGenerator = (i) -> (i < 10) ? | ||||
|             Optional.of(i + lastValue.getAndSet(i)) : | ||||
|             Optional.empty(); | ||||
|         Stream<Integer> unfolded = StreamUtils.unfold(2, i -> (i < 100) ? Optional.of(i * i) : Optional.empty()); | ||||
| 
 | ||||
|         List<Integer> fib = StreamUtils.unfold(1, fibonacciGenerator).collect(Collectors.toList()); | ||||
|         assertThat(fib).containsExactly(1, 1, 2, 3, 5, 8, 13); | ||||
|         assertThat(unfolded.collect(Collectors.toList())).containsExactly(2, 4, 16, 256); | ||||
|     } | ||||
| 
 | ||||
|     @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), | ||||
|         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()); | ||||
|         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)); | ||||
|     } | ||||
| 
 | ||||
|     @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) | ||||
|         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)); | ||||
| 
 | ||||
| @ -187,15 +182,14 @@ public class ProtonpackUnitTest { | ||||
|     public void givenProjectionFunction_whenMaxedBy_thenMaxedBy() { | ||||
|         Stream<String> clubs = Stream.of("Juventus", "Barcelona", "PSG"); | ||||
|         Optional<String> longestName = clubs.collect(CollectorUtils.maxBy(String::length)); | ||||
|         assertThat(longestName.get()).isEqualTo("Barcelona"); | ||||
|         assertThat(longestName).contains("Barcelona"); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenStreamOfMultipleElem_whenUniqueCollector_thenValueReturned() { | ||||
|         Stream<Integer> singleElement = Stream.of(1); | ||||
|         Optional<Integer> unique = singleElement.collect(CollectorUtils.unique()); | ||||
|         assertThat(unique.get()).isEqualTo(1); | ||||
| 
 | ||||
|         assertThat(unique).contains(1); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
| @ -205,5 +199,4 @@ public class ProtonpackUnitTest { | ||||
|             multipleElement.collect(CollectorUtils.unique()); | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user