Skip to content

Commit

Permalink
Cleanup: prefer unmodifiable collections, and several toList stragglers
Browse files Browse the repository at this point in the history
* Change collectors to use unmodifiable Set, Map, etc. where appropriate
* Fix a few stragglers to use toList instead of collect(toList())
  • Loading branch information
sleberknight committed Jun 3, 2024
1 parent e4d91ae commit 3c52b9b
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/kiwiproject/beans/BeanConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.isNull;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.toSet;
import static java.util.stream.Collectors.toUnmodifiableSet;

import com.google.common.collect.Sets;
import lombok.Getter;
Expand Down Expand Up @@ -106,7 +106,7 @@ protected Set<String> getPropertySet(T input, BeanWrapper inputWrapper) {
// remove exclusions
return propertyNames.stream()
.filter(not(prop -> exclusions.contains(prop)))
.collect(toSet());
.collect(toUnmodifiableSet());
}

@SuppressWarnings({"unchecked", "rawtypes"})
Expand All @@ -117,7 +117,7 @@ private static <T> Set<String> getPropertyNamesAsSet(T input, BeanWrapper inputW

return Stream.of(inputWrapper.getPropertyDescriptors())
.map(PropertyDescriptor::getName)
.collect(toSet());
.collect(toUnmodifiableSet());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static com.google.common.base.Verify.verify;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static java.util.stream.Collectors.toSet;
import static java.util.stream.Collectors.toUnmodifiableSet;
import static org.kiwiproject.base.KiwiStrings.format;
import static org.kiwiproject.collect.KiwiLists.first;
import static org.kiwiproject.collect.KiwiLists.hasOneElement;
Expand Down Expand Up @@ -254,7 +254,7 @@ public int getRollUpStatus() {
}

verify(errors.size() > 1, "Expecting more than one error at this point");
var uniqueStatusCodes = errors.stream().map(ErrorMessage::getCode).collect(toSet());
var uniqueStatusCodes = errors.stream().map(ErrorMessage::getCode).collect(toUnmodifiableSet());
if (KiwiSets.hasOneElement(uniqueStatusCodes)) {
return uniqueStatusCodes.iterator().next();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/kiwiproject/retry/RetryResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.nonNull;
import static java.util.stream.Collectors.toSet;
import static java.util.stream.Collectors.toUnmodifiableSet;
import static org.kiwiproject.collect.KiwiLists.isNullOrEmpty;
import static org.kiwiproject.collect.KiwiLists.last;

Expand Down Expand Up @@ -172,6 +172,6 @@ public Optional<Exception> getLastErrorIfPresent() {
public Set<String> getUniqueErrorTypes() {
return errors.stream()
.map(e -> e.getClass().getName())
.collect(toSet());
.collect(toUnmodifiableSet());
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/kiwiproject/retry/SimpleRetries.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public static <T> RetryResult<T> tryGetObjectCollectingErrors(int maxAttempts,
*
* return StreamEx.of(resultStream)
* .takeWhileInclusive(result -> isNull(result.getLeft())
* .collect(toList());
* .toList();
* </pre>
*/
private static <T> List<Pair<T, Exception>> collectResults(int maxAttempts,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.kiwiproject.spring.context;

import static java.util.stream.Collectors.toSet;
import static java.util.stream.Collectors.toUnmodifiableSet;
import static org.kiwiproject.spring.data.KiwiMongoConverters.addCustomConverters;
import static org.kiwiproject.spring.data.KiwiMongoConverters.newBsonUndefinedToNullObjectConverter;

Expand Down Expand Up @@ -95,7 +95,7 @@ public void attachListeners(ApplicationListener<?>... listeners) {
Set<String> registeredListenerClassNames = springContext.getApplicationListeners()
.stream()
.map(listener -> listener.getClass().getName())
.collect(toSet());
.collect(toUnmodifiableSet());

Stream.of(listeners).forEach(listener -> {
var listenerClassName = listener.getClass().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toUnmodifiableMap;
import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotNull;
import static org.kiwiproject.collect.KiwiSets.isNotNullOrEmpty;
import static org.kiwiproject.collect.KiwiSets.isNullOrEmpty;
Expand Down Expand Up @@ -274,7 +274,7 @@ public static <T> Map<String, String> combineErrorMessagesIntoMap(Set<Constraint
}

return violations.stream()
.collect(toMap(
.collect(toUnmodifiableMap(
violation -> pathTransformer.apply(violation.getPropertyPath()),
ConstraintViolation::getMessage,
(accumulatedMessage, newErrorMessage) -> accumulatedMessage + ", " + newErrorMessage));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.kiwiproject.ansible.vault;

import static java.util.stream.Collectors.toSet;
import static java.util.stream.Collectors.toUnmodifiableSet;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

Expand Down Expand Up @@ -109,7 +109,7 @@ void shouldRequireProperties() {
var invalidProperties = violations.stream()
.map(ConstraintViolation::getPropertyPath)
.map(Object::toString)
.collect(toSet());
.collect(toUnmodifiableSet());

assertThat(invalidProperties).containsExactlyInAnyOrder(
"ansibleVaultPath",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.kiwiproject.collect;

import static java.util.Map.entry;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand Down Expand Up @@ -49,7 +48,7 @@ void shouldCombineFromParallelStream() {
}

private void assertListContents(ImmutableList<Integer> list) {
var expectedList = firstFiftyEvenIntegers().collect(toList());
var expectedList = firstFiftyEvenIntegers().toList();

assertThat(list).isEqualTo(expectedList);
}
Expand Down
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 org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -30,7 +29,7 @@ void shouldEvictLeastRecentItems() {
IntStream.rangeClosed(1, 150).forEach(queue::add);

var queueItems = new ArrayList<>(queue);
var expectedItems = IntStream.rangeClosed(51, 150).boxed().collect(toList());
var expectedItems = IntStream.rangeClosed(51, 150).boxed().toList();
assertThat(queueItems).containsExactlyElementsOf(expectedItems);
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/org/kiwiproject/collect/KiwiStreamsTest.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.assertThatIllegalArgumentException;

Expand Down Expand Up @@ -85,8 +84,8 @@ private Stream<? extends Number> newNumberStream() {
return Stream.of(24, 42L, 64, 84.0, 96L, 256.0);
}

private List<Number> newNumberList() {
return newNumberStream().collect(toList());
private List<? extends Number> newNumberList() {
return newNumberStream().toList();
}

@Value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.kiwiproject.json;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toUnmodifiableList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.entry;
Expand Down Expand Up @@ -179,7 +179,7 @@ public List<BeanPropertyWriter> changeProperties(SerializationConfig config,
return beanProperties.stream()
.map(beanPropertyWriter ->
new PropertyMaskingSafePropertyWriter(beanPropertyWriter, maskedFields))
.collect(toList());
.collect(toUnmodifiableList());
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.kiwiproject.spring.context;

import static java.util.stream.Collectors.toSet;
import static java.util.stream.Collectors.toUnmodifiableSet;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;

Expand Down Expand Up @@ -98,7 +98,7 @@ void shouldBeAbleToUseRepository() {
var people = quoteDocRepository.findAll()
.stream()
.map(SampleQuoteDoc::getName)
.collect(toSet());
.collect(toUnmodifiableSet());

assertThat(people).containsExactlyInAnyOrder("Adam Smith", "Blaise Pascal");
}
Expand Down

0 comments on commit 3c52b9b

Please sign in to comment.