BAEL - 6715 (#15024)

* BAEL-6715: Fixed Indentation

* BAEL-6715: Changed Lists to Streams

* BAEL-6715: Inlined skip methods in the tests
This commit is contained in:
Eugene Kovko 2023-10-19 19:30:55 +02:00 committed by GitHub
parent 3178d8cb59
commit e3e95fbc82
3 changed files with 62 additions and 90 deletions

View File

@ -6,19 +6,23 @@ import java.util.function.BinaryOperator;
import java.util.stream.Collector; import java.util.stream.Collector;
class SkippingCollector { class SkippingCollector {
private static final BinaryOperator<SkippingCollector> IGNORE_COMBINE = (a, b) -> a; private static final BinaryOperator<SkippingCollector> IGNORE_COMBINE = (a, b) -> a;
private final int skip; private final int skip;
private final List<String> list = new ArrayList<>(); private final List<String> list = new ArrayList<>();
private int currentIndex = 0; private int currentIndex = 0;
private SkippingCollector(int skip) { private SkippingCollector(int skip) {
this.skip = skip; this.skip = skip;
} }
private void accept(String item) { private void accept(String item) {
final int index = ++currentIndex % skip; final int index = ++currentIndex % skip;
if (index == 0) if (index == 0) {
list.add(item); list.add(item);
}
} }
private List<String> getResult() { private List<String> getResult() {
return list; return list;
} }

View File

@ -1,65 +0,0 @@
package com.baeldung.skippingelements;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class SkippingElements {
private SkippingElements() {
}
public static List<String> skipNthElementInListWithFilter(List<String> sourceList, int n) {
return IntStream.range(0, sourceList.size())
.filter(s -> (s + 1) % n == 0)
.mapToObj(sourceList::get)
.collect(Collectors.toList());
}
public static List<String> skipNthElementInListWithIterate(List<String> sourceList, int n) {
int limit = sourceList.size() / n;
return IntStream.iterate(n - 1, i -> (i + n))
.limit(limit)
.mapToObj(sourceList::get)
.collect(Collectors.toList());
}
public static List<String> skipNthElementInListWithSublist(List<String> sourceList, int n) {
int limit = sourceList.size() / n;
return Stream.iterate(sourceList, s -> s.subList(n, s.size()))
.limit(limit)
.map(s -> s.get(n - 1))
.collect(Collectors.toList());
}
public static List<String> skipNthElementInListWithFor(List<String> sourceList, int n) {
List<String> result = new ArrayList<>();
for (int i = n - 1; i < sourceList.size(); i += n) {
result.add(sourceList.get(i));
}
return result;
}
public static List<String> skipNthElementInListWithIterator(Stream<String> sourceStream, int n) {
List<String> result = new ArrayList<>();
final Iterator<String> iterator = sourceStream.iterator();
int count = 0;
while (iterator.hasNext()) {
if (count % n == n - 1) {
result.add(iterator.next());
} else {
iterator.next();
}
++count;
}
return result;
}
public static List<String> skipNthElementInStreamWithCollector(Stream<String> sourceStream, int n) {
return sourceStream.collect(SkippingCollector.collector(n));
}
}

View File

@ -3,7 +3,11 @@ package com.baeldung.skippingelements;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.Arguments;
@ -14,21 +18,22 @@ class SkippingElementsUnitTest {
private static Stream<Arguments> testSource() { private static Stream<Arguments> testSource() {
return Stream.of( return Stream.of(
Arguments.of( Arguments.of(
List.of("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", Stream.of("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twenty One", "Twenty Two", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twenty One", "Twenty Two",
"Twenty Three", "Twenty Four", "Twenty Five", "Twenty Six", "Twenty Seven", "Twenty Eight", "Twenty Nine", "Thirty", "Twenty Three", "Twenty Four", "Twenty Five", "Twenty Six", "Twenty Seven", "Twenty Eight", "Twenty Nine", "Thirty",
"Thirty One", "Thirty Two", "Thirty Three"), "Thirty One", "Thirty Two", "Thirty Three"),
List.of("Three", "Six", "Nine", "Twelve", "Fifteen", "Eighteen", "Twenty One", "Twenty Four", "Twenty Seven", "Thirty", "Thirty Three"), List.of("Three", "Six", "Nine", "Twelve", "Fifteen", "Eighteen", "Twenty One", "Twenty Four", "Twenty Seven", "Thirty",
"Thirty Three"),
3), 3),
Arguments.of( Arguments.of(
List.of("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", Stream.of("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twenty One", "Twenty Two", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twenty One", "Twenty Two",
"Twenty Three", "Twenty Four", "Twenty Five", "Twenty Six", "Twenty Seven", "Twenty Eight", "Twenty Nine", "Thirty", "Twenty Three", "Twenty Four", "Twenty Five", "Twenty Six", "Twenty Seven", "Twenty Eight", "Twenty Nine", "Thirty",
"Thirty One", "Thirty Two", "Thirty Three"), "Thirty One", "Thirty Two", "Thirty Three"),
List.of("Five", "Ten", "Fifteen", "Twenty", "Twenty Five", "Thirty"), List.of("Five", "Ten", "Fifteen", "Twenty", "Twenty Five", "Thirty"),
5), 5),
Arguments.of( Arguments.of(
List.of("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", Stream.of("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twenty One", "Twenty Two", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twenty One", "Twenty Two",
"Twenty Three", "Twenty Four", "Twenty Five", "Twenty Six", "Twenty Seven", "Twenty Eight", "Twenty Nine", "Thirty", "Twenty Three", "Twenty Four", "Twenty Five", "Twenty Six", "Twenty Seven", "Twenty Eight", "Twenty Nine", "Thirty",
"Thirty One", "Thirty Two", "Thirty Three"), "Thirty One", "Thirty Two", "Thirty Three"),
@ -38,29 +43,29 @@ class SkippingElementsUnitTest {
"Thirty One", "Thirty Two", "Thirty Three"), "Thirty One", "Thirty Two", "Thirty Three"),
1), 1),
Arguments.of( Arguments.of(
List.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"), Stream.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"),
List.of("Wednesday", "Saturday"), List.of("Wednesday", "Saturday"),
3), 3),
Arguments.of( Arguments.of(
List.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"), Stream.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"),
List.of("Friday"), List.of("Friday"),
5), 5),
Arguments.of( Arguments.of(
List.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"), Stream.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"),
List.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"), List.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"),
1), 1),
Arguments.of( Arguments.of(
List.of("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", Stream.of("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",
"December"), "December"),
List.of("March", "June", "September", "December"), List.of("March", "June", "September", "December"),
3), 3),
Arguments.of( Arguments.of(
List.of("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", Stream.of("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",
"December"), "December"),
List.of("May", "October"), List.of("May", "October"),
5), 5),
Arguments.of( Arguments.of(
List.of("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", Stream.of("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",
"December"), "December"),
List.of("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", List.of("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",
"December"), "December"),
@ -70,45 +75,73 @@ class SkippingElementsUnitTest {
@ParameterizedTest @ParameterizedTest
@MethodSource("testSource") @MethodSource("testSource")
void givenListSkipNthElementInListWithFilterTestShouldFilterNthElement(List<String> input, List<String> expected, int n) { void givenListSkipNthElementInListWithFilterTestShouldFilterNthElement(Stream<String> input, List<String> expected, int n) {
final List<String> actual = SkippingElements.skipNthElementInListWithFilter(input, n); final List<String> sourceList = input.collect(Collectors.toList());
final List<String> actual = IntStream.range(0, sourceList.size())
.filter(s -> (s + 1) % n == 0)
.mapToObj(sourceList::get)
.collect(Collectors.toList());
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("testSource") @MethodSource("testSource")
void givenListSkipNthElementInListWithIterateTestShouldFilterNthElement(List<String> input, List<String> expected, int n) { void givenListSkipNthElementInListWithIterateTestShouldFilterNthElement(Stream<String> input, List<String> expected, int n) {
final List<String> actual = SkippingElements.skipNthElementInListWithIterate(input, n); final List<String> sourceList = input.collect(Collectors.toList());
int limit = sourceList.size() / n;
final List<String> actual = IntStream.iterate(n - 1, i -> (i + n))
.limit(limit)
.mapToObj(sourceList::get)
.collect(Collectors.toList());
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("testSource") @MethodSource("testSource")
void givenListSkipNthElementInListWithSublistTestShouldFilterNthElement(List<String> input, List<String> expected, int n) { void givenListSkipNthElementInListWithSublistTestShouldFilterNthElement(Stream<String> input, List<String> expected, int n) {
final List<String> actual = SkippingElements.skipNthElementInListWithSublist(input, n); final List<String> sourceList = input.collect(Collectors.toList());
int limit = sourceList.size() / n;
final List<String> actual = Stream.iterate(sourceList, s -> s.subList(n, s.size()))
.limit(limit)
.map(s -> s.get(n - 1))
.collect(Collectors.toList());
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("testSource") @MethodSource("testSource")
void givenListSkipNthElementInListWithForTestShouldFilterNthElement(List<String> input, List<String> expected, int n) { void givenListSkipNthElementInListWithForTestShouldFilterNthElement(Stream<String> input, List<String> expected, int n) {
final List<String> actual = SkippingElements.skipNthElementInListWithFor(input, n); final List<String> sourceList = input.collect(Collectors.toList());
List<String> result = new ArrayList<>();
for (int i = n - 1; i < sourceList.size(); i += n) {
result.add(sourceList.get(i));
}
final List<String> actual = result;
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("testSource") @MethodSource("testSource")
void givenListSkipNthElementInStreamWithIteratorTestShouldFilterNthElement(List<String> input, List<String> expected, int n) { void givenListSkipNthElementInStreamWithIteratorTestShouldFilterNthElement(Stream<String> input, List<String> expected, int n) {
final Stream<String> inputStream = input.stream(); List<String> result = new ArrayList<>();
final List<String> actual = SkippingElements.skipNthElementInListWithIterator(inputStream, n); final Iterator<String> iterator = input.iterator();
int count = 0;
while (iterator.hasNext()) {
if (count % n == n - 1) {
result.add(iterator.next());
} else {
iterator.next();
}
++count;
}
final List<String> actual = result;
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("testSource") @MethodSource("testSource")
void givenListSkipNthElementInStreamWithCollectorShouldFilterNthElement(List<String> input, List<String> expected, int n) { void givenListSkipNthElementInStreamWithCollectorShouldFilterNthElement(Stream<String> input, List<String> expected, int n) {
final Stream<String> inputStream = input.stream(); final List<String> actual = input.collect(SkippingCollector.collector(n));
final List<String> actual = SkippingElements.skipNthElementInStreamWithCollector(inputStream, n);
assertEquals(expected, actual); assertEquals(expected, actual);
} }
} }