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.StreamUtils; | ||||||
| import com.codepoetics.protonpack.collectors.CollectorUtils; | import com.codepoetics.protonpack.collectors.CollectorUtils; | ||||||
| import com.codepoetics.protonpack.collectors.NonUniqueValueException; | import com.codepoetics.protonpack.collectors.NonUniqueValueException; | ||||||
| import com.codepoetics.protonpack.selectors.Selector; | import com.codepoetics.protonpack.selectors.Selectors; | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| 
 | 
 | ||||||
| import java.util.Arrays; |  | ||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.Optional; | import java.util.Optional; | ||||||
| import java.util.Set; | 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.assertThat; | ||||||
| import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||||||
| 
 | 
 | ||||||
| @SuppressWarnings("unchecked") |  | ||||||
| public class ProtonpackUnitTest { | public class ProtonpackUnitTest { | ||||||
|     @Test |     @Test | ||||||
|     public void whenTakeWhile_thenTakenWhile() { |     public void whenTakeWhile_thenTakenWhile() { | ||||||
| @ -41,7 +39,8 @@ public class ProtonpackUnitTest { | |||||||
|     public void givenMultipleStream_whenZipped_thenZipped() { |     public void givenMultipleStream_whenZipped_thenZipped() { | ||||||
|         String[] clubs = {"Juventus", "Barcelona", "Liverpool", "PSG"}; |         String[] clubs = {"Juventus", "Barcelona", "Liverpool", "PSG"}; | ||||||
|         String[] players = {"Ronaldo", "Messi", "Salah"}; |         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()); |           .collect(Collectors.toSet()); | ||||||
|         assertThat(zippedFrom2Sources).contains("Juventus Ronaldo", "Barcelona Messi", "Liverpool Salah"); |         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, |         Set<String> merged = StreamUtils.merge(() -> "", (valOne, valTwo) -> valOne + " " + valTwo, streamOfClubs, | ||||||
|           streamOfPlayers, streamOfLeagues).collect(Collectors.toSet()); |           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"); |             " PSG"); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -92,26 +92,20 @@ public class ProtonpackUnitTest { | |||||||
|         Stream<String> streamOfPlayers = Stream.of("Ronaldo", "Messi"); |         Stream<String> streamOfPlayers = Stream.of("Ronaldo", "Messi"); | ||||||
|         Stream<String> streamOfLeagues = Stream.of("Serie A", "La Liga"); |         Stream<String> streamOfLeagues = Stream.of("Serie A", "La Liga"); | ||||||
| 
 | 
 | ||||||
|         AtomicInteger counter = new AtomicInteger(0); |         List<String> interleavedList = StreamUtils | ||||||
|         Selector roundRobinSelector = (o) -> { |           .interleave(Selectors.roundRobin(), streamOfClubs, streamOfPlayers, streamOfLeagues) | ||||||
|             Object[] vals = (Object[]) o; |           .collect(Collectors.toList()); | ||||||
|             while (counter.get() >= vals.length || vals[counter.get()] == null) { | 
 | ||||||
|                 if (counter.incrementAndGet() >= vals.length) |         assertThat(interleavedList) | ||||||
|                     counter.set(0); |           .hasSize(7) | ||||||
|             } |           .containsExactly("Juventus", "Ronaldo", "Serie A", "Barcelona", "Messi", "La Liga", "Liverpool"); | ||||||
|             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"); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void whenSkippedUntil_thenSkippedUntil() { |     public void whenSkippedUntil_thenSkippedUntil() { | ||||||
|         Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |         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); |         assertThat(skippedUntilGreaterThan5).containsExactly(6, 7, 8, 9, 10); | ||||||
| 
 | 
 | ||||||
|         List<Integer> skippedUntilLessThanEquals5 = StreamUtils.skipUntil(stream(numbers), i -> i <= 5) |         List<Integer> skippedUntilLessThanEquals5 = StreamUtils.skipUntil(stream(numbers), i -> i <= 5) | ||||||
| @ -126,37 +120,38 @@ public class ProtonpackUnitTest { | |||||||
|           .collect(Collectors.toList()); |           .collect(Collectors.toList()); | ||||||
|         assertThat(skippedWhileLessThanEquals5).containsExactly(6, 7, 8, 9, 10); |         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); |         assertThat(skippedWhileGreaterThan5).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void givenFibonacciGenerator_whenUnfolded_thenUnfolded() { |     public void givenFibonacciGenerator_whenUnfolded_thenUnfolded() { | ||||||
|         AtomicInteger lastValue = new AtomicInteger(0); |         Stream<Integer> unfolded = StreamUtils.unfold(2, i -> (i < 100) ? Optional.of(i * i) : Optional.empty()); | ||||||
|         Function<Integer, Optional<Integer>> fibonacciGenerator = (i) -> (i < 10) ? |  | ||||||
|             Optional.of(i + lastValue.getAndSet(i)) : |  | ||||||
|             Optional.empty(); |  | ||||||
| 
 | 
 | ||||||
|         List<Integer> fib = StreamUtils.unfold(1, fibonacciGenerator).collect(Collectors.toList()); |         assertThat(unfolded.collect(Collectors.toList())).containsExactly(2, 4, 16, 256); | ||||||
|         assertThat(fib).containsExactly(1, 1, 2, 3, 5, 8, 13); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void whenWindowed_thenWindowed() { |     public void whenWindowed_thenWindowed() { | ||||||
|         Integer[] numbers = {1, 2, 3, 4, 5, 6, 7}; |         Integer[] numbers = {1, 2, 3, 4, 5, 6, 7}; | ||||||
| 
 | 
 | ||||||
|         List<List<Integer>> windowedWithSkip1 = StreamUtils.windowed(stream(numbers), 3, 1).collect(Collectors.toList()); |         List<List<Integer>> windowedWithSkip1 = StreamUtils.windowed(stream(numbers), 3, 1) | ||||||
|         assertThat(windowedWithSkip1).containsExactly(asList(1, 2, 3), asList(2, 3, 4), asList(3, 4, 5), asList(4, 5, 6), |           .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)); |             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)); |         assertThat(windowedWithSkip2).containsExactly(asList(1, 2, 3), asList(3, 4, 5), asList(5, 6, 7)); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void whenAggregated_thenAggregated() { |     public void whenAggregated_thenAggregated() { | ||||||
|         Integer[] numbers = {1, 2, 2, 3, 4, 4, 4, 5}; |         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()); |           .collect(Collectors.toList()); | ||||||
|         assertThat(aggregated).containsExactly(asList(1), asList(2, 2), asList(3), asList(4, 4, 4), asList(5)); |         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() { |     public void givenProjectionFunction_whenMaxedBy_thenMaxedBy() { | ||||||
|         Stream<String> clubs = Stream.of("Juventus", "Barcelona", "PSG"); |         Stream<String> clubs = Stream.of("Juventus", "Barcelona", "PSG"); | ||||||
|         Optional<String> longestName = clubs.collect(CollectorUtils.maxBy(String::length)); |         Optional<String> longestName = clubs.collect(CollectorUtils.maxBy(String::length)); | ||||||
|         assertThat(longestName.get()).isEqualTo("Barcelona"); |         assertThat(longestName).contains("Barcelona"); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void givenStreamOfMultipleElem_whenUniqueCollector_thenValueReturned() { |     public void givenStreamOfMultipleElem_whenUniqueCollector_thenValueReturned() { | ||||||
|         Stream<Integer> singleElement = Stream.of(1); |         Stream<Integer> singleElement = Stream.of(1); | ||||||
|         Optional<Integer> unique = singleElement.collect(CollectorUtils.unique()); |         Optional<Integer> unique = singleElement.collect(CollectorUtils.unique()); | ||||||
|         assertThat(unique.get()).isEqualTo(1); |         assertThat(unique).contains(1); | ||||||
| 
 |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
| @ -205,5 +199,4 @@ public class ProtonpackUnitTest { | |||||||
|             multipleElement.collect(CollectorUtils.unique()); |             multipleElement.collect(CollectorUtils.unique()); | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user