Make ImmutableMap Stream collect()ion nicer (#654)

This adds an entriesToImmutableMap() collector that can be used in place of
toImmutableMap(Map.Entry::getkey, Map.Entry::getValue()).

It also fixes up some existing calls that use toImmutableMap() when terser
alternatives exist.
This commit is contained in:
Ben McIlwain 2020-06-26 11:57:26 -04:00 committed by GitHub
parent fba6804d3b
commit 74b2de5c35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 49 additions and 54 deletions

View file

@ -34,6 +34,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Maps;
import com.google.common.net.InternetDomainName;
import com.googlecode.objectify.Key;
import google.registry.flows.EppException;
@ -48,7 +49,6 @@ import google.registry.model.registry.Registry;
import google.registry.model.reporting.HistoryEntry;
import google.registry.testing.AppEngineRule;
import google.registry.testing.ShardableTestCase;
import java.util.function.Function;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Rule;
@ -324,11 +324,7 @@ public class AllocationTokenFlowUtilsTest extends ShardableTestCase {
AllocationToken tokenEntity,
String clientId,
DateTime now) {
return domainNames.stream()
.collect(
ImmutableMap.toImmutableMap(
Function.identity(),
domainName -> domainName.toString().contains("bunny") ? "fufu" : ""));
return Maps.toMap(domainNames, domain -> domain.toString().contains("bunny") ? "fufu" : "");
}
}
}

View file

@ -15,11 +15,11 @@
package google.registry.testing;
import static com.google.common.truth.Truth.assertWithMessage;
import static google.registry.util.CollectionUtils.entriesToImmutableMap;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder;
import com.google.common.collect.Multimaps;
@ -136,7 +136,7 @@ public class AppEngineRuleTest {
Map<String, Collection<Class<?>>> conflictingKinds =
kindToEntityMultiMap.asMap().entrySet().stream()
.filter(e -> e.getValue().size() > 1)
.collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
.collect(entriesToImmutableMap());
assertWithMessage(
"Conflicting Ofy kinds found. Tests will break if they are registered with "
+ " AppEngineRule in the same test executor.")

View file

@ -34,6 +34,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.googlecode.objectify.Key;
import google.registry.model.domain.token.AllocationToken;
@ -47,7 +48,6 @@ import google.registry.util.Retrier;
import google.registry.util.StringGenerator.Alphabets;
import java.io.File;
import java.util.Collection;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.joda.time.DateTime;
import org.junit.Before;
@ -311,8 +311,7 @@ public class GenerateAllocationTokensCommandTest
// Using ImmutableObject comparison here is tricky because the creation/updated timestamps are
// neither easy nor valuable to test here.
ImmutableMap<String, AllocationToken> actualTokens =
ofy().load().type(AllocationToken.class).list().stream()
.collect(ImmutableMap.toImmutableMap(AllocationToken::getToken, Function.identity()));
Maps.uniqueIndex(ofy().load().type(AllocationToken.class), AllocationToken::getToken);
assertThat(actualTokens).hasSize(expectedTokens.length);
for (AllocationToken expectedToken : expectedTokens) {
AllocationToken match = actualTokens.get(expectedToken.getToken());