diff --git a/java/google/registry/model/registry/label/BaseDomainLabelList.java b/java/google/registry/model/registry/label/BaseDomainLabelList.java index b471a86a0..144880b34 100644 --- a/java/google/registry/model/registry/label/BaseDomainLabelList.java +++ b/java/google/registry/model/registry/label/BaseDomainLabelList.java @@ -16,6 +16,7 @@ package google.registry.model.registry.label; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Strings.isNullOrEmpty; +import static com.google.common.collect.ImmutableSet.toImmutableSet; import static google.registry.model.common.EntityGroupRoot.getCrossTldKey; import static google.registry.model.registry.Registries.getTlds; @@ -136,14 +137,11 @@ public abstract class BaseDomainLabelList, R extends Dom /** Gets the names of the tlds that reference this list. */ public final ImmutableSet getReferencingTlds() { - ImmutableSet.Builder builder = new ImmutableSet.Builder<>(); Key> key = Key.create(this); - for (String tld : getTlds()) { - if (refersToKey(Registry.get(tld), key)) { - builder.add(tld); - } - } - return builder.build(); + return getTlds() + .stream() + .filter((tld) -> refersToKey(Registry.get(tld), key)) + .collect(toImmutableSet()); } protected abstract boolean refersToKey( diff --git a/java/google/registry/model/registry/label/PremiumList.java b/java/google/registry/model/registry/label/PremiumList.java index 7f4d2d878..1002f0b70 100644 --- a/java/google/registry/model/registry/label/PremiumList.java +++ b/java/google/registry/model/registry/label/PremiumList.java @@ -33,7 +33,6 @@ import com.google.common.cache.LoadingCache; import com.google.common.hash.BloomFilter; import com.google.common.util.concurrent.UncheckedExecutionException; import com.googlecode.objectify.Key; -import com.googlecode.objectify.Work; import com.googlecode.objectify.annotation.Entity; import com.googlecode.objectify.annotation.Id; import com.googlecode.objectify.annotation.Parent; @@ -113,13 +112,12 @@ public final class PremiumList extends BaseDomainLabelList cachePremiumLists = CacheBuilder.newBuilder() .expireAfterWrite(getDomainLabelListCacheDuration().getMillis(), MILLISECONDS) - .build(new CacheLoader() { - @Override - public PremiumList load(final String listName) { - return ofy().doTransactionless(new Work() { + .build( + new CacheLoader() { @Override - public PremiumList run() { - return ofy().load() - .type(PremiumList.class) - .parent(getCrossTldKey()) - .id(listName) - .now(); - }}); - }}); + public PremiumList load(final String listName) { + return ofy() + .doTransactionless( + () -> + ofy() + .load() + .type(PremiumList.class) + .parent(getCrossTldKey()) + .id(listName) + .now()); + } + }); /** * In-memory cache for {@link PremiumListRevision}s, used for retrieving Bloom filters quickly. * - *

This is cached for a long duration (essentially indefinitely) because a given - * {@link PremiumListRevision} is immutable and cannot ever be changed once created, so its cache - * need not ever expire. + *

This is cached for a long duration (essentially indefinitely) because a given {@link + * PremiumListRevision} is immutable and cannot ever be changed once created, so its cache need + * not ever expire. */ static final LoadingCache, PremiumListRevision> cachePremiumListRevisions = @@ -166,14 +166,9 @@ public final class PremiumList extends BaseDomainLabelList, PremiumListRevision>() { @Override public PremiumListRevision load(final Key revisionKey) { - return ofy() - .doTransactionless( - new Work() { - @Override - public PremiumListRevision run() { - return ofy().load().key(revisionKey).now(); - }}); - }}); + return ofy().doTransactionless(() -> ofy().load().key(revisionKey).now()); + } + }); /** * In-memory cache for {@link PremiumListEntry}s for a given label and {@link PremiumListRevision} @@ -206,10 +201,7 @@ public final class PremiumList extends BaseDomainLabelList load(final Key entryKey) { return ofy() - .doTransactionless( - () -> { - return Optional.ofNullable(ofy().load().key(entryKey).now()); - }); + .doTransactionless(() -> Optional.ofNullable(ofy().load().key(entryKey).now())); } }); } diff --git a/java/google/registry/model/registry/label/ReservedList.java b/java/google/registry/model/registry/label/ReservedList.java index 801d34026..52c929fb4 100644 --- a/java/google/registry/model/registry/label/ReservedList.java +++ b/java/google/registry/model/registry/label/ReservedList.java @@ -36,6 +36,7 @@ import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; import com.google.common.net.InternetDomainName; import com.google.common.util.concurrent.UncheckedExecutionException; import com.googlecode.objectify.Key; @@ -45,7 +46,6 @@ import com.googlecode.objectify.annotation.Mapify; import com.googlecode.objectify.mapper.Mapper; import google.registry.model.registry.Registry; import google.registry.model.registry.label.DomainLabelMetrics.MetricsReservedListMatch; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Optional; @@ -96,6 +96,7 @@ public final class ReservedList /** Mapper for use with @Mapify */ static class LabelMapper implements Mapper { + @Override public String getKey(ReservedListEntry entry) { return entry.getLabel(); @@ -130,7 +131,8 @@ public final class ReservedList entry.allowedNameservers = Joiner.on(',').join(allowedNameservers); } } else { - checkArgument(reservationType != RESERVED_FOR_ANCHOR_TENANT, + checkArgument( + reservationType != RESERVED_FOR_ANCHOR_TENANT, "Anchor tenant reservations must have an auth code configured"); checkArgument( reservationType != NAMESERVER_RESTRICTED, @@ -143,13 +145,13 @@ public final class ReservedList } private static void checkNameserversAreValid(Set nameservers) { - for (String nameserver : nameservers) { - // A domain name with fewer than two parts cannot be a hostname, as a nameserver should be. - checkArgument( - InternetDomainName.from(nameserver).parts().size() >= 3, - "%s is not a valid nameserver hostname", - nameserver); - } + // A domain name with fewer than two parts cannot be a hostname, as a nameserver should be. + nameservers.forEach( + (ns) -> + checkArgument( + InternetDomainName.from(ns).parts().size() >= 3, + "%s is not a valid nameserver hostname", + ns)); } @Override @@ -218,7 +220,7 @@ public final class ReservedList } return getReservedListEntries(label, tld) .stream() - .map((ReservedListEntry reservedListEntry) -> reservedListEntry.reservationType) + .map(ReservedListEntry::getValue) .collect(toImmutableSet()); } @@ -230,15 +232,13 @@ public final class ReservedList */ public static boolean matchesAnchorTenantReservation( InternetDomainName domainName, String authCode) { - ImmutableSet entries = - getReservedListEntries(domainName.parts().get(0), domainName.parent().toString()); - Set domainAuthCodes = new HashSet<>(); - for (ReservedListEntry entry : entries) { - if (entry.reservationType == RESERVED_FOR_ANCHOR_TENANT) { - domainAuthCodes.add(entry.getAuthCode()); - } - } + ImmutableSet domainAuthCodes = + getReservedListEntries(domainName.parts().get(0), domainName.parent().toString()) + .stream() + .filter((entry) -> entry.reservationType == RESERVED_FOR_ANCHOR_TENANT) + .map(ReservedListEntry::getAuthCode) + .collect(toImmutableSet()); checkState( domainAuthCodes.size() <= 1, "There are conflicting auth codes for domain: %s", domainName); @@ -253,38 +253,27 @@ public final class ReservedList * domain is not set with {@code NAMESERVER_RESTRICTED} reservation type. */ public static ImmutableSet getAllowedNameservers(InternetDomainName domainName) { - HashSet allowedNameservers = new HashSet<>(); - boolean foundFirstNameserverRestricted = false; - for (ReservedListEntry entry : - getReservedListEntries(domainName.parts().get(0), domainName.parent().toString())) { - if (entry.reservationType == NAMESERVER_RESTRICTED) { - if (foundFirstNameserverRestricted) { - allowedNameservers.retainAll(entry.getAllowedNameservers()); - } else { - allowedNameservers = new HashSet(entry.getAllowedNameservers()); - foundFirstNameserverRestricted = true; - } - } - } - return ImmutableSet.copyOf(allowedNameservers); + return getReservedListEntries(domainName.parts().get(0), domainName.parent().toString()) + .stream() + .filter((entry) -> entry.reservationType == NAMESERVER_RESTRICTED) + .map(ReservedListEntry::getAllowedNameservers) + .reduce((types1, types2) -> Sets.intersection(types1, types2).immutableCopy()) + .orElse(ImmutableSet.of()); } - /** * Helper function to retrieve the entries associated with this label and TLD, or an empty set if * no such entry exists. */ private static ImmutableSet getReservedListEntries(String label, String tld) { DateTime startTime = DateTime.now(UTC); - Registry registry = Registry.get(checkNotNull(tld, "tld")); - ImmutableSet> reservedLists = registry.getReservedLists(); - ImmutableSet lists = loadReservedLists(reservedLists); + Registry registry = Registry.get(checkNotNull(tld, "tld must not be null")); ImmutableSet.Builder entriesBuilder = new ImmutableSet.Builder<>(); ImmutableSet.Builder metricMatchesBuilder = new ImmutableSet.Builder<>(); // Loop through all reservation lists and add each of them. - for (ReservedList rl : lists) { + for (ReservedList rl : loadReservedLists(registry.getReservedLists())) { if (rl.getReservedListEntries().containsKey(label)) { ReservedListEntry entry = rl.getReservedListEntries().get(label); entriesBuilder.add(entry); @@ -300,17 +289,20 @@ public final class ReservedList private static ImmutableSet loadReservedLists( ImmutableSet> reservedListKeys) { - ImmutableSet.Builder builder = new ImmutableSet.Builder<>(); - for (Key listKey : reservedListKeys) { - try { - builder.add(cache.get(listKey.getName())); - } catch (ExecutionException e) { - throw new UncheckedExecutionException(String.format( - "Could not load the reserved list '%s' from the cache", listKey.getName()), e); - } - } - - return builder.build(); + return reservedListKeys + .stream() + .map( + (listKey) -> { + try { + return cache.get(listKey.getName()); + } catch (ExecutionException e) { + throw new UncheckedExecutionException( + String.format( + "Could not load the reserved list '%s' from the cache", listKey.getName()), + e); + } + }) + .collect(toImmutableSet()); } private static LoadingCache cache = diff --git a/javatests/google/registry/model/registry/label/PremiumListTest.java b/javatests/google/registry/model/registry/label/PremiumListTest.java index cc7e7669f..f97962209 100644 --- a/javatests/google/registry/model/registry/label/PremiumListTest.java +++ b/javatests/google/registry/model/registry/label/PremiumListTest.java @@ -16,18 +16,14 @@ package google.registry.model.registry.label; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; -import static com.google.common.truth.Truth8.assertThat; import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.testing.DatastoreHelper.createTld; import static google.registry.testing.DatastoreHelper.persistPremiumList; import static google.registry.testing.DatastoreHelper.persistReservedList; import static google.registry.testing.DatastoreHelper.persistResource; -import com.google.common.base.Function; import com.google.common.collect.ImmutableList; -import com.googlecode.objectify.Key; import google.registry.model.registry.Registry; -import google.registry.model.registry.label.PremiumList.PremiumListEntry; import google.registry.model.registry.label.PremiumList.PremiumListRevision; import google.registry.testing.AppEngineRule; import google.registry.testing.ExceptionRule; @@ -93,8 +89,4 @@ public class PremiumListTest { ImmutableList.of( "lol,USD 100", "rofl,USD 90", "paper,USD 80", "wood,USD 70", "lol,USD 200")); } - - /** Gets the label of a premium list entry. */ - public static final Function, String> GET_ENTRY_NAME_FUNCTION = - Key::getName; }