Make Registrar load methods return Optionals instead of Nullables

This makes the code more understandable from callsites, and also forces
users of this function to deal with the situation where the registrar
with a given client ID might not be present (it was previously silently
NPEing from some of the callsites).

This also adds a test helper method loadRegistrar(clientId) that retains
the old functionality for terseness in tests. It also fixes some instances
of using the load method with the wrong cachedness -- some uses in high-
traffic situations (WHOIS) that should have caching, but also low-traffic
reporting that don't benefit from caching so might as well always be
current.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=162990468
This commit is contained in:
mcilwain 2017-07-24 14:43:20 -07:00 committed by Ben McIlwain
parent 84fdeebc2f
commit d536cef20f
81 changed files with 707 additions and 602 deletions

View file

@ -36,8 +36,10 @@ import static google.registry.util.X509Utils.getCertificateHash;
import static google.registry.util.X509Utils.loadCertificate;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Strings;
import com.google.common.base.Supplier;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
@ -901,15 +903,16 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
return CACHE_BY_CLIENT_ID.get().values();
}
/** Load a registrar entity by its client id directly from Datastore. */
@Nullable
public static Registrar loadByClientId(String clientId) {
return ofy().load().type(Registrar.class).parent(getCrossTldKey()).id(clientId).now();
/** Loads and returns a registrar entity by its client id directly from Datastore. */
public static Optional<Registrar> loadByClientId(String clientId) {
checkArgument(!Strings.isNullOrEmpty(clientId), "clientId must be specified");
return Optional.fromNullable(
ofy().load().type(Registrar.class).parent(getCrossTldKey()).id(clientId).now());
}
/** Load a registrar entity by its client id using an in-memory cache. */
@Nullable
public static Registrar loadByClientIdCached(String clientId) {
return CACHE_BY_CLIENT_ID.get().get(clientId);
/** Loads and returns a registrar entity by its client id using an in-memory cache. */
public static Optional<Registrar> loadByClientIdCached(String clientId) {
checkArgument(!Strings.isNullOrEmpty(clientId), "clientId must be specified");
return Optional.fromNullable(CACHE_BY_CLIENT_ID.get().get(clientId));
}
}