Skip to content

Commit

Permalink
Replace Stream.collect(Collectors.toList()) by Stream.toList()
Browse files Browse the repository at this point in the history
  • Loading branch information
groldan committed Mar 5, 2024
1 parent 5865e13 commit 7883f70
Show file tree
Hide file tree
Showing 31 changed files with 50 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
* <B>Note:</B> <TT>service</TT> and <TT>request</TT> params are usually set by the client, and by
Expand Down Expand Up @@ -76,7 +75,7 @@ private List<Rule> flatten(Map<String, List<Rule>> found) {
.flatMap(List::stream)
.sorted((r1, r2) -> Long.compare(r1.getPriority(), r2.getPriority()))
.distinct()
.collect(Collectors.toList());
.toList();
}

@Override
Expand Down Expand Up @@ -113,8 +112,7 @@ public AccessInfo getAccessInfo(AccessRequest request) {
}

log.debug("Returning {} for {}", ret, request);
List<String> matchingIds =
flatten(groupedRules).stream().map(Rule::getId).collect(Collectors.toList());
List<String> matchingIds = flatten(groupedRules).stream().map(Rule::getId).toList();
return ret.withMatchingRules(matchingIds);
}

Expand Down Expand Up @@ -500,8 +498,7 @@ protected Map<String, List<Rule>> getMatchingRulesByRole(AccessRequest request)
filter = filter.clone();
filter.getRole().setType(SpecialFilterType.DEFAULT);
}
List<Rule> found =
ruleService.getAll(RuleQuery.of(filter)).collect(Collectors.toList());
List<Rule> found = ruleService.getAll(RuleQuery.of(filter)).toList();
ret.put(null, found);
} else {
for (String role : finalRoleFilter) {
Expand All @@ -516,7 +513,7 @@ private List<Rule> getRulesByRole(RuleFilter filter, String role) {
filter = filter.clone();
filter.setRole(role);
filter.getRole().setIncludeDefault(true);
return ruleService.getAll(RuleQuery.of(filter)).collect(Collectors.toList());
return ruleService.getAll(RuleQuery.of(filter)).toList();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* {@link AuthorizationService} integration/conformance test
Expand Down Expand Up @@ -607,12 +606,9 @@ private void assertGetMatchingRules(
private void assertMatchingRules(AccessRequest request, Integer... expectedPriorities) {
List<Rule> rules = authorizationService.getMatchingRules(request);

List<Long> pri =
rules.stream().map(r -> r.getPriority()).sorted().collect(Collectors.toList());
List<Long> pri = rules.stream().map(r -> r.getPriority()).sorted().toList();
List<Long> exp =
Arrays.asList(expectedPriorities).stream()
.map(i -> i.longValue())
.collect(Collectors.toList());
Arrays.asList(expectedPriorities).stream().map(i -> i.longValue()).toList();
assertEquals(exp, pri, "Bad rule set selected for filter " + request);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ public UserDetails loadUserDetails(PreAuthenticatedAuthenticationToken token)
}

List<SimpleGrantedAuthority> authorities =
credentials.stream()
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toList());
credentials.stream().map(SimpleGrantedAuthority::new).toList();

return new User(principal, "", authorities);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Data
Expand Down Expand Up @@ -41,7 +40,7 @@ public boolean enabled() {
public List<SimpleGrantedAuthority> authorities() {
return Stream.of(admin ? "ROLE_ADMIN" : "ROLE_USER")
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toList());
.toList();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -182,7 +181,7 @@ public Stream<Rule> getAll(@NonNull RuleQuery<RuleFilter> query) {
@Override
public Optional<Rule> getRule(@NonNull RuleFilter filter) throws IllegalArgumentException {
RuleQuery<RuleFilter> query = RuleQuery.of(filter).setLimit(2);
List<Rule> found = ruleRepository.findAll(query).collect(Collectors.toList());
List<Rule> found = ruleRepository.findAll(query).toList();
if (found.size() > 1) {
throw new IllegalArgumentException(
"Unexpected rule count for filter " + filter + " : " + found.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.Optional;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public abstract class MemoryPriorityRepository<R> {
Expand Down Expand Up @@ -73,7 +72,7 @@ protected int shiftPrioritiesBetween(long min, long max, long offset) {
long p = getPriority(r);
return p >= min && p <= max;
})
.collect(Collectors.toList());
.toList();

rules.removeAll(matches);
matches.forEach(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class RuleAdminServiceIT {
Expand Down Expand Up @@ -236,7 +235,7 @@ void testGetAll() {
ruleAdminService.setLayerDetails(r2.getId(), details2);

List<Rule> expected = List.of(r1, r2);
List<Rule> actual = ruleAdminService.getAll().collect(Collectors.toList());
List<Rule> actual = ruleAdminService.getAll().toList();
assertThat(actual).isEqualTo(expected);
}

Expand All @@ -259,7 +258,7 @@ void testGetAll_and_GetList_paginated() {
String nextCursorId = null;
for (int page = 0; page < maxPages; page++) {
query.setNextId(nextCursorId);
List<Rule> result = ruleAdminService.getAll(query).collect(Collectors.toList());
List<Rule> result = ruleAdminService.getAll(query).toList();
if (result.size() > pageSize) {
assertThat(result.size()).isEqualTo(1 + pageSize);
nextCursorId = result.get(pageSize).getId();
Expand Down Expand Up @@ -589,7 +588,7 @@ void testUpdateRuleById_shifts_other_priorities_if_needed() {
r1 = r1.withPriority(4);
r4 = r4.withPriority(5);
assertThat(ruleAdminService.update(r3)).isEqualTo(r3);
List<Rule> collect = ruleAdminService.getAll().collect(Collectors.toList());
List<Rule> collect = ruleAdminService.getAll().toList();

assertGet(r2).as("r2 should have kept priority 2").isEqualTo(r2);

Expand Down Expand Up @@ -673,7 +672,7 @@ private void assertPriorities(List<Rule> expectedOrder, List<Integer> expectedPr
assertThat(expectedOrder.size())
.as("mismatch in expectations")
.isEqualTo(expectedPriorities.size());
List<Rule> all = ruleAdminService.getAll().collect(Collectors.toList());
List<Rule> all = ruleAdminService.getAll().toList();
assertThat(all.size()).isEqualTo(expectedPriorities.size());

for (int i = 0; i < expectedOrder.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

class RuleAdminServiceImplTest {
Expand Down Expand Up @@ -176,7 +175,7 @@ void getAll() {
List<Rule> expected = List.of(Rule.allow(), Rule.deny(), Rule.limit());
when(repository.findAll()).thenReturn(expected.stream());

List<Rule> actual = service.getAll().collect(Collectors.toList());
List<Rule> actual = service.getAll().toList();
verify(repository, times(1)).findAll();
verifyNoMoreInteractions(repository);
assertThat(actual).isEqualTo(expected);
Expand All @@ -197,7 +196,7 @@ void getList() {

when(repository.findAll(query)).thenReturn(expected.stream());

List<Rule> actual = service.getAll(query).collect(Collectors.toList());
List<Rule> actual = service.getAll(query).toList();
verify(repository, times(1)).findAll(eq(query));
verifyNoMoreInteractions(repository);
assertThat(actual).isEqualTo(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.geoserver.acl.domain.rules.Rule;

import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Slf4j
Expand Down Expand Up @@ -67,7 +66,7 @@ public List<Rule> getMatchingRules(org.geoserver.acl.authorization.AccessRequest
try {
apiRequest = mapper.toApi(request);
apiResponse = apiClient.getMatchingRules(apiRequest);
return apiResponse.stream().map(ruleMapper::toModel).collect(Collectors.toList());
return apiResponse.stream().map(ruleMapper::toModel).toList();
} catch (RuntimeException e) {
log.error("Error getting matching rules for {}", request, e);
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.springframework.http.ResponseEntity;

import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@IsAuthenticated
Expand Down Expand Up @@ -63,8 +62,7 @@ public ResponseEntity<List<Rule>> getMatchingRules(AccessRequest accessRequest)
modelResponse = service.getMatchingRules(modelRequest);

support.setPreferredGeometryEncoding();
List<Rule> apiResponse =
modelResponse.stream().map(support::toApi).collect(Collectors.toList());
List<Rule> apiResponse = modelResponse.stream().map(support::toApi).toList();
return ResponseEntity.ok(apiResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@IsAuthenticated
Expand Down Expand Up @@ -94,14 +93,14 @@ private ResponseEntity<List<Rule>> query(
query.setLimit(query.getLimit() + 1);
}
try {
list = service.getAll(query).collect(Collectors.toList());
list = service.getAll(query).toList();
query.setLimit(requestedLimit); // avoid side effect once the method returns
} catch (IllegalArgumentException e) {
return support.error(BAD_REQUEST, e.getMessage());
}

support.setPreferredGeometryEncoding();
List<Rule> body = list.stream().map(support::toApi).collect(Collectors.toList());
List<Rule> body = list.stream().map(support::toApi).toList();
String nextCursor;
if (requestedLimit != null && body.size() > requestedLimit) {
nextCursor = body.get(requestedLimit).getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@IsAuthenticated
Expand Down Expand Up @@ -114,13 +113,13 @@ private ResponseEntity<List<AdminRule>> query(
query.setLimit(query.getLimit() + 1);
}
try {
list = service.getAll(query).collect(Collectors.toList());
list = service.getAll(query).toList();
query.setLimit(requestedLimit); // avoid side effect once the method returns
} catch (IllegalArgumentException e) {
return support.error(BAD_REQUEST, e.getMessage());
}

List<AdminRule> body = list.stream().map(support::toApi).collect(Collectors.toList());
List<AdminRule> body = list.stream().map(support::toApi).toList();
String nextCursor;
if (requestedLimit != null && body.size() > requestedLimit) {
nextCursor = body.get(requestedLimit).getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

@SpringBootTest(classes = RulesApiConfiguration.class, properties = "spring.main.banner-mode=off")
class RulesApiImpTest {
Expand Down Expand Up @@ -119,7 +118,7 @@ private List<Rule> assertList(
ResponseEntity<List<org.geoserver.acl.api.model.Rule>> response = call.get();
assertThat(response.getStatusCode()).isEqualTo(status);
assertThat(response.getBody()).isNotNull();
return response.getBody().stream().map(support::toModel).collect(Collectors.toList());
return response.getBody().stream().map(support::toModel).toList();
}

@Disabled("Filter mapping not yet implemented")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public Stream<AdminRule> findAll(@NonNull RuleQuery<AdminRuleFilter> query) {
if (null != query.getLimit()) {
rules = rules.limit(query.getLimit());
}
return rules.collect(Collectors.toList()).stream();
return rules.toList().stream();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.Optional;
import java.util.OptionalLong;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
Expand Down Expand Up @@ -177,8 +176,7 @@ Predicate map(InSetPredicate<String> filter, StringPath propertyPath) {

if (includeDefault) {
return propertyPath.in(
Stream.concat(Stream.of("*"), values.stream())
.collect(Collectors.toList()));
Stream.concat(Stream.of("*"), values.stream()).toList());
}
return propertyPath.in(values);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public Stream<Rule> findAll(@NonNull RuleQuery<RuleFilter> query) {
if (null != pageSize) {
rules = rules.limit(query.getLimit());
}
return rules.collect(Collectors.toList()).stream();
return rules.toList().stream();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.springframework.test.context.ActiveProfiles;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@SpringBootTest(
Expand Down Expand Up @@ -112,15 +111,14 @@ void count() {
repo.findById(r1.getId());
repo.findById(r2.getId());

List<AdminRule> collect = repo.findAll().collect(Collectors.toList());
List<AdminRule> collect = repo.findAll().toList();
assertEquals(2, collect.size());
}

@Test
void streamAll() {
List<AdminRule> all =
IntStream.rangeClosed(1, 100).mapToObj(this::addFull).collect(Collectors.toList());
List<AdminRule> result = repo.findAll(RuleQuery.of()).collect(Collectors.toList());
List<AdminRule> all = IntStream.rangeClosed(1, 100).mapToObj(this::addFull).toList();
List<AdminRule> result = repo.findAll(RuleQuery.of()).toList();
assertThat(result).isEqualTo(all);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,22 @@ void count() {
repo.findById(r1.getId());
repo.findById(r2.getId());

List<Rule> collect = repo.findAll().collect(Collectors.toList());
List<Rule> collect = repo.findAll().toList();
assertEquals(2, collect.size());
}

@Test
void streamAll() {
List<Rule> limits =
IntStream.rangeClosed(1, 100)
.mapToObj(this::addLimitsRule)
.collect(Collectors.toList());
List<Rule> limits = IntStream.rangeClosed(1, 100).mapToObj(this::addLimitsRule).toList();
List<Rule> allows =
IntStream.rangeClosed(101, 200)
.mapToObj(this::addAllowRuleWithLayerDetails)
.collect(Collectors.toList());
.toList();

List<Rule> expected = new ArrayList<>(limits);
expected.addAll(allows);

List<Rule> result = repo.findAll(RuleQuery.of()).collect(Collectors.toList());
List<Rule> result = repo.findAll(RuleQuery.of()).toList();
assertThat(result).isEqualTo(expected);
}

Expand Down
Loading

0 comments on commit 7883f70

Please sign in to comment.