Skip to content

Commit

Permalink
Sonar: Fix java:S6204 (use Stream.toList) (#1149)
Browse files Browse the repository at this point in the history
  • Loading branch information
sleberknight authored Jun 3, 2024
1 parent 71537f8 commit e4d91ae
Show file tree
Hide file tree
Showing 19 changed files with 43 additions and 64 deletions.
5 changes: 2 additions & 3 deletions src/test/java/org/kiwiproject/collect/KiwiListsTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.kiwiproject.collect;

import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
Expand Down Expand Up @@ -121,7 +120,7 @@ void shouldThrow_WhenNullList() {

@Test
void shouldReturnSortedList_WhenHasItems() {
var someIntegers = IntStream.iterate(1, n -> n + 1).limit(100).boxed().collect(toList());
var someIntegers = IntStream.iterate(1, n -> n + 1).limit(100).boxed().toList();

var randomIntegers = newArrayList(someIntegers);
Collections.shuffle(randomIntegers);
Expand Down Expand Up @@ -159,7 +158,7 @@ void shouldThrow_WhenNullComparator() {

@Test
void shouldReturnSortedList_WhenHasItems() {
var someIntegers = IntStream.iterate(1, n -> n + 1).limit(100).boxed().collect(toList());
var someIntegers = IntStream.iterate(1, n -> n + 1).limit(100).boxed().toList();
var reverseIntegers = newArrayList(someIntegers);
Collections.reverse(reverseIntegers);

Expand Down
11 changes: 5 additions & 6 deletions src/test/java/org/kiwiproject/collect/KiwiMapsTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.kiwiproject.collect;

import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
Expand Down Expand Up @@ -100,7 +99,7 @@ void testNewLinkedHashMap() {
List<String> expectedKeys = Arrays.stream(items)
.filter(obj -> obj instanceof String)
.map(String.class::cast)
.collect(toList());
.toList();
assertThat(linkedHashMap.keySet()).containsExactlyElementsOf(expectedKeys);
}

Expand All @@ -127,7 +126,7 @@ void testNewTreeMap() {
.filter(obj -> obj instanceof String)
.map(String.class::cast)
.sorted()
.collect(toList());
.toList();
assertThat(treeMap.keySet()).containsExactlyElementsOf(expectedKeys);
}

Expand All @@ -147,7 +146,7 @@ void testNewTreeMap_StringKeys_To_ObjectValues() {
List<String> transformed = treeMap.entrySet()
.stream()
.map(entry -> entry.getKey() + entry.getValue())
.collect(toList());
.toList();

assertThat(transformed).containsExactly("bar84", "baz126", "foo42");
}
Expand Down Expand Up @@ -313,7 +312,7 @@ void shouldCreateUnmodifiableLinkedHashMap() {
List<String> expectedKeys = Arrays.stream(items)
.filter(obj -> obj instanceof String)
.map(String.class::cast)
.collect(toList());
.toList();
assertThat(unmodifiableLinkedHashMap.keySet()).containsExactlyElementsOf(expectedKeys);

//noinspection DataFlowIssue
Expand Down Expand Up @@ -343,7 +342,7 @@ void shouldCreateUnmodifiableTreeMap() {
.filter(obj -> obj instanceof String)
.map(String.class::cast)
.sorted()
.collect(toList());
.toList();
assertThat(unmodifiableTreeMap.keySet()).containsExactlyElementsOf(expectedKeys);

//noinspection DataFlowIssue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.kiwiproject.config;

import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
Expand Down Expand Up @@ -233,7 +232,7 @@ void shouldRoundRobinManyDomains() {

var domainNumbers = Stream.iterate(1, value -> value + 1)
.limit(numberOfDomains)
.collect(toList());
.toList();
var cyclingIterable = Iterables.cycle(domainNumbers);

StreamSupport.stream(cyclingIterable.spliterator(), /* parallel */ false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.kiwiproject.config;

import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.tuple;
Expand Down Expand Up @@ -330,7 +329,7 @@ private SecureEndpointsConfiguration newSecureEndpointsConfigurationWithEndpoint
}

private SecureEndpointsConfiguration newSecureEndpointsConfiguration(IntStream tagStream) {
var endpointConfigs = tagStream.mapToObj(this::newHttpEndpointConfiguration).collect(toList());
var endpointConfigs = tagStream.mapToObj(this::newHttpEndpointConfiguration).toList();

var config = newSecureEndpointsConfigurationWithNoEndpoints();
config.setEndpoints(endpointConfigs);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.kiwiproject.dropwizard.jdbi3;

import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.kiwiproject.collect.KiwiLists.first;
Expand Down Expand Up @@ -125,7 +124,7 @@ private void verifyAndAssertWhenBuildingManagedDataSource(String healthCheckName
.map(lifeCycle -> (JettyManaged) lifeCycle)
.map(JettyManaged::getManaged)
.map(Managed::getClass)
.collect(toList());
.toList();
assertThat(managedClasses).hasSize(1);
var firstManagedClass = first(managedClasses);
assertThat(firstManagedClass.getInterfaces()).contains(ManagedDataSource.class);
Expand Down Expand Up @@ -243,7 +242,7 @@ private void verifyAndAssertWhenGivenAManagedDataSource(ManagedDataSource manage
.stream()
.map(lifeCycle -> (JettyManaged) lifeCycle)
.map(JettyManaged::getManaged)
.collect(toList());
.toList();
assertThat(managedObjects).containsExactly(managedDataSource);

assertThat(healthCheckRegistry.getHealthCheck(healthCheckName)).isInstanceOf(JdbiHealthCheck.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.kiwiproject.io;

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand Down Expand Up @@ -333,7 +332,7 @@ void testUpdateFileDeletionMetadata_WhenExceedInMemoryDeleteErrorCapacity() {

var filesNotDeleted = IntStream.rangeClosed(1, 501)
.mapToObj(value -> newFailedFileDeleteResult(temporaryPath.resolve("folder" + value).toString()))
.collect(toList());
.toList();

cleaner.updateFileDeletionMetadata(600, filesNotDeleted);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.kiwiproject.io;

import static java.util.stream.Collectors.toList;

import org.apache.commons.io.FileUtils;

import java.io.File;
Expand Down Expand Up @@ -51,7 +49,7 @@ private void newFileInFolder(Path folder, int value) {

public List<File> filesInTempFolder() throws IOException {
try (var paths = Files.list(cleanerPath)) {
return paths.map(Path::toFile).collect(toList());
return paths.map(Path::toFile).toList();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.kiwiproject.jaxrs;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.kiwiproject.collect.KiwiLists.third;
Expand Down Expand Up @@ -112,7 +111,7 @@ void shouldSupportListOfStringObjectMaps() {

var sortedCars = cars.stream()
.sorted(comparing(car -> car.get("model").toString()))
.collect(toList());
.toList();

assertThat(sortedCars).hasSize(3);

Expand Down
7 changes: 3 additions & 4 deletions src/test/java/org/kiwiproject/regex/MatchSpliteratorTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.kiwiproject.regex;

import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.tuple;
Expand Down Expand Up @@ -49,7 +48,7 @@ void shouldCreateMatchResultStream() {
var matcher = Pattern.compile("the|(lazy cat)", Pattern.CASE_INSENSITIVE).matcher(input);
var matchSpliterator = new MatchSpliterator(matcher);

var matchResults = matchSpliterator.stream().collect(toList());
var matchResults = matchSpliterator.stream().toList();
assertThat(matchResults)
.extracting(MatchResult::group, MatchResult::start)
.containsExactly(
Expand All @@ -66,7 +65,7 @@ class StreamFromRegex {
@Test
void shouldCreateMatchResultStream_FromStringRegularExpression() {
var input = "The quick brown fox jumped over the fence and the lazy brown dog";
var matchResults = MatchSpliterator.stream("the", input).collect(toList());
var matchResults = MatchSpliterator.stream("the", input).toList();

assertThat(matchResults)
.extracting(MatchResult::group, MatchResult::start)
Expand All @@ -91,7 +90,7 @@ class StreamFromPattern {
void shouldCreateMatchResultStream_FromPattern() {
var input = "The quick brown fox jumped over the fence and the lazy brown dog and the lazy cat";
var pattern = Pattern.compile("the|(lazy cat)", Pattern.CASE_INSENSITIVE);
var matchResults = MatchSpliterator.stream(pattern, input).collect(toList());
var matchResults = MatchSpliterator.stream(pattern, input).toList();

assertThat(matchResults)
.extracting(MatchResult::group, MatchResult::start)
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/org/kiwiproject/retry/InMemoryAppender.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.kiwiproject.retry;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;

import ch.qos.logback.classic.spi.ILoggingEvent;
Expand Down Expand Up @@ -49,13 +48,13 @@ synchronized void clearEvents() {
}

List<ILoggingEvent> getOrderedEvents() {
return getOrderedEventStream().collect(toList());
return getOrderedEventStream().toList();
}

List<String> getOrderedEventMessages() {
return getOrderedEventStream()
.map(ILoggingEvent::getFormattedMessage)
.collect(toList());
.toList();
}

private Stream<ILoggingEvent> getOrderedEventStream() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static java.util.Comparator.comparing;
import static java.util.Comparator.comparingDouble;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.kiwiproject.base.KiwiStrings.f;
import static org.kiwiproject.spring.data.OrderTestData.ORDER_COLLECTION;
Expand Down Expand Up @@ -119,12 +118,12 @@ void shouldAggregatePage(SoftAssertions softly) {
.sorted(comparing(Order::getCustomerId))
.map(Order::getCustomerId)
.limit(limit)
.collect(toList());
.toList();

var aggregatedCustomerIds = aggregatePage.getContent()
.stream()
.map(Order::getCustomerId)
.collect(toList());
.toList();

softly.assertThat(aggregatedCustomerIds).isEqualTo(expectedCustomerIds);
}
Expand Down Expand Up @@ -202,7 +201,7 @@ void shouldAggregatePage_WithMatchOperation(SoftAssertions softly) {
.filter(orderFilter)
.sorted(comparing(Order::getCustomerId).thenComparing(comparingDouble(Order::getAmount).reversed()))
.limit(limit)
.collect(toList());
.toList();

softly.assertThat(aggregatePage.getContent()).isEqualTo(expectedOrders);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.kiwiproject.spring.data;

import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.kiwiproject.spring.util.MongoTestHelpers.newMongoTemplate;
Expand Down Expand Up @@ -69,7 +68,7 @@ private void assertIndices(Sort.Direction direction) {
var indexFields = indexInfos.stream()
.map(IndexInfo::getIndexFields)
.flatMap(Collection::stream)
.collect(toList());
.toList();

assertThat(indexFields)
.extracting("key", "direction")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.kiwiproject.spring.data;

import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand Down Expand Up @@ -337,7 +336,7 @@ private static List<Object> createTestSortDirectionFieldPairList() {
private static void assertSortChain(Sort sortChain) {
assertThat(sortChain).isNotNull();

var sortOrders = Streams.stream(sortChain.iterator()).collect(toList());
var sortOrders = Streams.stream(sortChain.iterator()).toList();

assertThat(sortOrders)
.extracting("property", "direction")
Expand Down
Loading

0 comments on commit e4d91ae

Please sign in to comment.