Add assertTldsExist(Iterable<String>) to check multiple TLDs at once

This is better than calling assertTldExists() inside a for loop because you can throw a single exception reporting all bad TLDs at once rather than only getting as far as the first failure.  And then it's also a one-liner instead of 3 lines.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=152412876
This commit is contained in:
nickfelt 2017-04-06 12:28:15 -07:00 committed by Ben McIlwain
parent 783033c261
commit 5081d780dc
8 changed files with 31 additions and 28 deletions

View file

@ -15,8 +15,9 @@
package google.registry.model.registry;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.equalTo;
import static com.google.common.base.Predicates.in;
import static com.google.common.base.Predicates.not;
import static com.google.common.base.Strings.emptyToNull;
import static com.google.common.collect.Maps.filterValues;
import static google.registry.model.CacheUtils.memoizeWithShortExpiration;
@ -24,8 +25,10 @@ import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Supplier;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.net.InternetDomainName;
@ -76,15 +79,25 @@ public final class Registries {
return ImmutableSet.copyOf(filterValues(cache.get(), equalTo(type)).keySet());
}
/** Shortcut to check whether a tld exists or else throw. If it exists, it is returned back. */
/** Pass-through check that the specified TLD exists, otherwise throw an IAE. */
public static String assertTldExists(String tld) {
checkArgument(
getTlds().contains(checkNotNull(emptyToNull(tld), "Null or empty TLD specified")),
getTlds().contains(checkArgumentNotNull(emptyToNull(tld), "Null or empty TLD specified")),
"TLD %s does not exist",
tld);
return tld;
}
/** Pass-through check that every TLD in the given iterable exists, otherwise throw an IAE. */
public static Iterable<String> assertTldsExist(Iterable<String> tlds) {
for (String tld : tlds) {
checkArgumentNotNull(emptyToNull(tld), "Null or empty TLD specified");
}
ImmutableSet<String> badTlds = FluentIterable.from(tlds).filter(not(in(getTlds()))).toSet();
checkArgument(badTlds.isEmpty(), "TLDs do not exist: %s", Joiner.on(", ").join(badTlds));
return tlds;
}
/**
* Returns TLD which the domain name or hostname falls under, no matter how many levels of
* sublabels there are.