Switch from Guava Optionals to Java 8 Optionals

This was a surprisingly involved change. Some of the difficulties included
java.util.Optional purposely not being Serializable (so I had to move a
few Optionals in mapreduce classes to @Nullable) and having to add the Truth
Java8 extension library for assertion support.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=171863777
This commit is contained in:
mcilwain 2017-10-11 13:09:26 -07:00 committed by jianglai
parent 184b2b56ac
commit c0f8da0c6e
581 changed files with 1325 additions and 932 deletions

View file

@ -18,10 +18,10 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static google.registry.model.ofy.ObjectifyService.ofy;
import com.google.common.base.Optional;
import google.registry.model.ofy.ObjectifyService;
import google.registry.util.TypeUtils.TypeInstantiator;
import java.lang.reflect.Field;
import java.util.Optional;
/** Interface for {@link ImmutableObject} subclasses that have a builder. */
public interface Buildable {
@ -64,8 +64,8 @@ public interface Buildable {
}
if (idField != null
&& !idField.getType().equals(String.class)
&& Optional.fromNullable((Long) ModelUtils.getFieldValue(instance, idField))
.or(0L) == 0) {
&& Optional.ofNullable((Long) ModelUtils.getFieldValue(instance, idField))
.orElse(0L) == 0) {
ModelUtils.setFieldValue(instance, idField, ObjectifyService.allocateId());
}
return instance;

View file

@ -23,7 +23,6 @@ import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import com.googlecode.objectify.Key;
@ -32,6 +31,7 @@ import com.googlecode.objectify.annotation.Index;
import google.registry.model.eppcommon.StatusValue;
import google.registry.model.ofy.CommitLogManifest;
import google.registry.model.transfer.TransferData;
import java.util.Optional;
import java.util.Set;
import org.joda.time.DateTime;
@ -296,7 +296,7 @@ public abstract class EppResource extends BackupGroupRoot implements Buildable {
addStatusValue(StatusValue.OK);
}
// If there is no deletion time, set it to END_OF_TIME.
setDeletionTime(Optional.fromNullable(getInstance().deletionTime).or(END_OF_TIME));
setDeletionTime(Optional.ofNullable(getInstance().deletionTime).orElse(END_OF_TIME));
return ImmutableObject.cloneEmptyToNull(super.build());
}
}

View file

@ -16,8 +16,8 @@ package google.registry.model;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import com.google.common.base.Optional;
import google.registry.model.translators.UpdateAutoTimestampTranslatorFactory;
import java.util.Optional;
import org.joda.time.DateTime;
/**
@ -31,7 +31,7 @@ public class UpdateAutoTimestamp extends ImmutableObject {
/** Returns the timestamp, or {@link #START_OF_TIME} if it's null. */
public DateTime getTimestamp() {
return Optional.fromNullable(timestamp).or(START_OF_TIME);
return Optional.ofNullable(timestamp).orElse(START_OF_TIME);
}
public static UpdateAutoTimestamp create(DateTime timestamp) {

View file

@ -24,7 +24,6 @@ import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
@ -44,6 +43,7 @@ import google.registry.model.domain.rgp.GracePeriodStatus;
import google.registry.model.reporting.HistoryEntry;
import google.registry.model.transfer.TransferData.TransferServerApproveEntity;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
import org.joda.money.Money;
@ -391,7 +391,7 @@ public abstract class BillingEvent extends ImmutableObject
checkNotNull(instance.reason);
instance.recurrenceTimeOfYear = TimeOfYear.fromDateTime(instance.eventTime);
instance.recurrenceEndTime =
Optional.fromNullable(instance.recurrenceEndTime).or(END_OF_TIME);
Optional.ofNullable(instance.recurrenceEndTime).orElse(END_OF_TIME);
return super.build();
}
}

View file

@ -21,7 +21,6 @@ import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.model.registry.Registries.assertTldExists;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Ordering;
@ -35,6 +34,7 @@ import google.registry.model.ImmutableObject;
import google.registry.model.annotations.ReportedOn;
import google.registry.model.registrar.Registrar;
import google.registry.model.registry.Registry;
import java.util.Optional;
import org.joda.money.CurrencyUnit;
import org.joda.time.DateTime;
@ -194,7 +194,7 @@ public final class RegistrarCredit extends ImmutableObject implements Buildable
Registry.get(instance.tld).getCurrency().equals(instance.currency),
"Credits must be in the currency of the assigned TLD");
instance.description =
Optional.fromNullable(instance.description).or(instance.getDefaultDescription());
Optional.ofNullable(instance.description).orElse(instance.getDefaultDescription());
return super.build();
}
}

View file

@ -20,7 +20,6 @@ import static com.google.common.base.Preconditions.checkState;
import static google.registry.model.ofy.ObjectifyService.ofy;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.collect.ForwardingNavigableMap;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Maps;
@ -34,6 +33,7 @@ import google.registry.model.ImmutableObject;
import google.registry.model.annotations.ReportedOn;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.joda.time.DateTime;
@ -171,8 +171,8 @@ public final class RegistrarCreditBalance extends ImmutableObject implements Bui
ofy().load().type(RegistrarCreditBalance.class).ancestor(registrarCredit)) {
// Create the submap at this key if it doesn't exist already.
Map<DateTime, Money> submap =
Optional.fromNullable(map.get(balance.effectiveTime))
.or(new HashMap<DateTime, Money>());
Optional.ofNullable(map.get(balance.effectiveTime))
.orElse(new HashMap<DateTime, Money>());
submap.put(balance.writtenTime, balance.amount);
map.put(balance.effectiveTime, submap);
}
@ -212,8 +212,8 @@ public final class RegistrarCreditBalance extends ImmutableObject implements Bui
private Optional<Money> getMostRecentlyWrittenBalance(
Map.Entry<DateTime, ImmutableSortedMap<DateTime, Money>> balancesAtEffectiveTime) {
return balancesAtEffectiveTime == null
? Optional.<Money>absent()
// Don't use Optional.fromNullable() here since it's an error if there's a empty submap.
? Optional.<Money>empty()
// Don't use Optional.ofNullable() here since it's an error if there's a empty submap.
: Optional.of(balancesAtEffectiveTime.getValue().lastEntry().getValue());
}

View file

@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static google.registry.model.EppResourceUtils.projectResourceOntoBuilderAtTime;
import com.google.common.base.Optional;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.googlecode.objectify.annotation.Entity;
@ -32,6 +31,7 @@ import google.registry.model.annotations.ExternalMessagingName;
import google.registry.model.annotations.ReportedOn;
import google.registry.model.contact.PostalInfo.Type;
import google.registry.model.transfer.TransferData;
import java.util.Optional;
import java.util.stream.Stream;
import javax.xml.bind.annotation.XmlElement;
import org.joda.time.DateTime;
@ -147,7 +147,7 @@ public class ContactResource extends EppResource implements
@Override
public final TransferData getTransferData() {
return Optional.fromNullable(transferData).or(TransferData.EMPTY);
return Optional.ofNullable(transferData).orElse(TransferData.EMPTY);
}
@Override

View file

@ -16,11 +16,11 @@ package google.registry.model.contact;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.base.Optional;
import com.googlecode.objectify.annotation.Embed;
import google.registry.model.Buildable;
import google.registry.model.Buildable.Overlayable;
import google.registry.model.ImmutableObject;
import java.util.Optional;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlEnumValue;
@ -77,10 +77,9 @@ public class PostalInfo extends ImmutableObject implements Overlayable<PostalInf
// Don't overlay the type field, as that should never change.
checkState(source.type == null || source.type == type);
return asBuilder()
.setName(Optional.fromNullable(source.getName()).or(Optional.fromNullable(name)).orNull())
.setOrg(Optional.fromNullable(source.getOrg()).or(Optional.fromNullable(org)).orNull())
.setAddress(
Optional.fromNullable(source.getAddress()).or(Optional.fromNullable(address)).orNull())
.setName(Optional.ofNullable(source.getName()).orElse(name))
.setOrg(Optional.ofNullable(source.getOrg()).orElse(org))
.setAddress(Optional.ofNullable(source.getAddress()).orElse(address))
.build();
}

View file

@ -24,7 +24,6 @@ import static google.registry.util.DateTimeUtils.earliestOf;
import static google.registry.util.DateTimeUtils.isBeforeOrAt;
import static google.registry.util.DateTimeUtils.leapSafeAddYears;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.EntitySubclass;
@ -41,6 +40,7 @@ import google.registry.model.registry.Registry;
import google.registry.model.transfer.TransferData;
import google.registry.model.transfer.TransferStatus;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
import org.joda.time.DateTime;
@ -174,7 +174,7 @@ public class DomainResource extends DomainBase
@Override
public final TransferData getTransferData() {
return Optional.fromNullable(transferData).or(TransferData.EMPTY);
return Optional.ofNullable(transferData).orElse(TransferData.EMPTY);
}
@Override
@ -328,7 +328,7 @@ public class DomainResource extends DomainBase
return earliestOf(
leapSafeAddYears(
currentExpirationTime,
Optional.fromNullable(extendedRegistrationYears).or(0)),
Optional.ofNullable(extendedRegistrationYears).orElse(0)),
leapSafeAddYears(now, MAX_REGISTRATION_YEARS));
}

View file

@ -14,9 +14,9 @@
package google.registry.model.domain.fee;
import com.google.common.base.Optional;
import google.registry.model.ImmutableObject;
import google.registry.model.domain.Period;
import java.util.Optional;
import javax.xml.bind.annotation.XmlTransient;
import org.joda.money.CurrencyUnit;
import org.joda.time.DateTime;
@ -69,6 +69,6 @@ public abstract class FeeQueryCommandExtensionItem extends ImmutableObject {
public abstract String getSubphase();
public Period getPeriod() {
return Optional.fromNullable(period).or(DEFAULT_PERIOD);
return Optional.ofNullable(period).orElse(DEFAULT_PERIOD);
}
}

View file

@ -14,9 +14,9 @@
package google.registry.model.domain.fee06;
import com.google.common.base.Optional;
import google.registry.model.domain.fee.FeeCheckCommandExtensionItem;
import google.registry.model.domain.fee.FeeExtensionCommandDescriptor;
import java.util.Optional;
import javax.xml.bind.annotation.XmlType;
import org.joda.money.CurrencyUnit;
import org.joda.time.DateTime;
@ -79,6 +79,6 @@ public class FeeCheckCommandExtensionItemV06 extends FeeCheckCommandExtensionIte
@Override
public Optional<DateTime> getEffectiveDate() {
return Optional.absent();
return Optional.empty();
}
}

View file

@ -14,10 +14,10 @@
package google.registry.model.domain.fee06;
import com.google.common.base.Optional;
import google.registry.model.domain.fee.FeeExtensionCommandDescriptor;
import google.registry.model.domain.fee.FeeQueryCommandExtensionItem;
import google.registry.model.eppinput.EppInput.CommandExtension;
import java.util.Optional;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.joda.money.CurrencyUnit;
@ -66,7 +66,7 @@ public class FeeInfoCommandExtensionV06
@Override
public Optional<DateTime> getEffectiveDate() {
return Optional.absent();
return Optional.empty();
}
}

View file

@ -16,7 +16,6 @@ package google.registry.model.domain.fee11;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import google.registry.model.ImmutableObject;
@ -26,6 +25,7 @@ import google.registry.model.domain.fee.FeeCheckCommandExtensionItem;
import google.registry.model.domain.fee.FeeCheckResponseExtensionItem;
import google.registry.model.domain.fee.FeeExtensionCommandDescriptor;
import google.registry.model.domain.fee11.FeeCheckCommandExtensionV11.FeeCheckCommandExtensionItemV11;
import java.util.Optional;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@ -115,7 +115,7 @@ public class FeeCheckCommandExtensionV11 extends ImmutableObject
@Override
public Period getPeriod() {
return Optional.fromNullable(period).or(DEFAULT_PERIOD);
return Optional.ofNullable(period).orElse(DEFAULT_PERIOD);
}
@Override
@ -140,7 +140,7 @@ public class FeeCheckCommandExtensionV11 extends ImmutableObject
@Override
public Optional<DateTime> getEffectiveDate() {
return Optional.absent();
return Optional.empty();
}
}
}

View file

@ -16,9 +16,9 @@ package google.registry.model.domain.fee12;
import com.google.common.base.Ascii;
import com.google.common.base.CharMatcher;
import com.google.common.base.Optional;
import google.registry.model.domain.Period;
import google.registry.model.domain.fee.FeeCheckCommandExtensionItem;
import java.util.Optional;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@ -110,6 +110,6 @@ public class FeeCheckCommandExtensionItemV12 extends FeeCheckCommandExtensionIte
@Override
public Optional<DateTime> getEffectiveDate() {
return Optional.fromNullable(feeDate);
return Optional.ofNullable(feeDate);
}
}

View file

@ -22,12 +22,12 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
import com.google.common.base.Ascii;
import com.google.common.base.CharMatcher;
import com.google.common.base.Optional;
import com.google.common.primitives.Ints;
import com.googlecode.objectify.annotation.Embed;
import com.googlecode.objectify.annotation.IgnoreSave;
import com.googlecode.objectify.condition.IfNull;
import google.registry.model.ImmutableObject;
import java.util.Optional;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@ -64,7 +64,7 @@ public class LaunchNotice extends ImmutableObject {
public String getValidatorId() {
// The default value is "tmch".
return Optional.fromNullable(validatorId).or("tmch");
return Optional.ofNullable(validatorId).orElse("tmch");
}
}
@ -78,7 +78,7 @@ public class LaunchNotice extends ImmutableObject {
DateTime acceptedTime;
public NoticeIdType getNoticeId() {
return Optional.fromNullable(noticeId).or(EMPTY_NOTICE_ID);
return Optional.ofNullable(noticeId).orElse(EMPTY_NOTICE_ID);
}
public DateTime getExpirationTime() {

View file

@ -18,7 +18,6 @@ import static google.registry.util.CollectionUtils.nullSafeImmutableCopy;
import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy;
import com.google.common.base.Ascii;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import google.registry.model.ImmutableObject;
@ -56,6 +55,7 @@ import google.registry.model.eppinput.ResourceCommand.ResourceCheck;
import google.registry.model.eppinput.ResourceCommand.SingleResourceCommand;
import google.registry.model.host.HostCommand;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
import javax.xml.bind.annotation.XmlAttribute;
@ -107,12 +107,12 @@ public class EppInput extends ImmutableObject {
return Optional.of(xmlSchemaAnnotation.xmlns()[0].prefix());
}
}
return Optional.absent();
return Optional.empty();
}
/** Returns whether this EppInput represents a command that operates on domain resources. */
public boolean isDomainResourceType() {
return getResourceType().or("").equals("domain");
return getResourceType().orElse("").equals("domain");
}
@Nullable
@ -131,7 +131,7 @@ public class EppInput extends ImmutableObject {
ResourceCommand resourceCommand = getResourceCommand();
return resourceCommand instanceof SingleResourceCommand
? Optional.of(((SingleResourceCommand) resourceCommand).getTargetId())
: Optional.<String>absent();
: Optional.<String>empty();
}
/**

View file

@ -17,7 +17,6 @@ package google.registry.model.eppoutput;
import static google.registry.util.CollectionUtils.forceEmptyToNull;
import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import google.registry.model.Buildable;
import google.registry.model.ImmutableObject;
@ -157,9 +156,7 @@ public class EppResponse extends ImmutableObject implements ResponseOrGreeting {
@Nullable
public ResponseExtension getFirstExtensionOfType(Class<? extends ResponseExtension> clazz) {
return Optional.fromJavaUtil(
extensions.stream().filter(clazz::isInstance).map(clazz::cast).findFirst())
.orNull();
return extensions.stream().filter(clazz::isInstance).map(clazz::cast).findFirst().orElse(null);
}
@Nullable

View file

@ -21,7 +21,6 @@ import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static google.registry.util.DomainNameUtils.canonicalizeDomainName;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
@ -35,6 +34,7 @@ import google.registry.model.annotations.ReportedOn;
import google.registry.model.domain.DomainResource;
import google.registry.model.transfer.TransferData;
import java.net.InetAddress;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
import org.joda.time.DateTime;
@ -146,9 +146,9 @@ public class HostResource extends EppResource implements ForeignKeyedEppResource
superordinateDomain != null
&& Key.create(superordinateDomain).equals(getSuperordinateDomain()));
DateTime lastSuperordinateChange =
Optional.fromNullable(getLastSuperordinateChange()).or(getCreationTime());
Optional.ofNullable(getLastSuperordinateChange()).orElse(getCreationTime());
DateTime lastTransferOfCurrentSuperordinate =
Optional.fromNullable(superordinateDomain.getLastTransferTime()).or(START_OF_TIME);
Optional.ofNullable(superordinateDomain.getLastTransferTime()).orElse(START_OF_TIME);
return (lastSuperordinateChange.isBefore(lastTransferOfCurrentSuperordinate))
? superordinateDomain.getLastTransferTime()
: getLastTransferTime();

View file

@ -22,7 +22,6 @@ import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Converter;
import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Streams;
@ -47,6 +46,7 @@ import google.registry.model.transfer.TransferData.TransferServerApproveEntity;
import google.registry.model.transfer.TransferResponse.ContactTransferResponse;
import google.registry.model.transfer.TransferResponse.DomainTransferResponse;
import java.util.List;
import java.util.Optional;
import org.joda.time.DateTime;
/**
@ -351,7 +351,7 @@ public abstract class PollMessage extends ImmutableObject
public Autorenew build() {
Autorenew instance = getInstance();
instance.autorenewEndTime =
Optional.fromNullable(instance.autorenewEndTime).or(END_OF_TIME);
Optional.ofNullable(instance.autorenewEndTime).orElse(END_OF_TIME);
return super.build();
}
}

View file

@ -14,7 +14,7 @@
package google.registry.model.pricing;
import com.google.common.base.Optional;
import java.util.Optional;
import org.joda.money.Money;
import org.joda.time.DateTime;

View file

@ -23,9 +23,9 @@ import static google.registry.model.registry.label.ReservedList.getReservationTy
import static google.registry.util.DomainNameUtils.getTldFromDomainName;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.net.InternetDomainName;
import google.registry.model.registry.Registry;
import java.util.Optional;
import javax.inject.Inject;
import org.joda.money.Money;
import org.joda.time.DateTime;
@ -52,8 +52,8 @@ public final class StaticPremiumListPricingEngine implements PremiumPricingEngin
isNameCollisionInSunrise ? "collision" : null));
return DomainPrices.create(
premiumPrice.isPresent(),
premiumPrice.or(registry.getStandardCreateCost()),
premiumPrice.or(registry.getStandardRenewCost(priceTime)),
Optional.<String>fromNullable(feeClass));
premiumPrice.orElse(registry.getStandardCreateCost()),
premiumPrice.orElse(registry.getStandardRenewCost(priceTime)),
Optional.<String>ofNullable(feeClass));
}
}

View file

@ -39,7 +39,6 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Comparator.comparing;
import static java.util.function.Predicate.isEqual;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
@ -80,6 +79,7 @@ import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import javax.annotation.Nullable;
@ -896,13 +896,13 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
/** 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(
return Optional.ofNullable(
ofy().load().type(Registrar.class).parent(getCrossTldKey()).id(clientId).now());
}
/** 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));
return Optional.ofNullable(CACHE_BY_CLIENT_ID.get().get(clientId));
}
}

View file

@ -27,7 +27,6 @@ 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.ImmutableMap;
import com.google.common.collect.ImmutableSet;
@ -35,6 +34,7 @@ import com.google.common.collect.Streams;
import com.google.common.net.InternetDomainName;
import com.googlecode.objectify.Work;
import google.registry.model.registry.Registry.TldType;
import java.util.Optional;
/** Utilities for finding and listing {@link Registry} entities. */
public final class Registries {
@ -123,7 +123,7 @@ public final class Registries {
return Optional.of(domainName);
}
}
return Optional.absent();
return Optional.empty();
}
/**
@ -132,7 +132,7 @@ public final class Registries {
*/
public static InternetDomainName findTldForNameOrThrow(InternetDomainName domainName) {
return checkArgumentNotNull(
findTldForName(domainName).orNull(),
findTldForName(domainName).orElse(null),
"Domain name is not under a recognized TLD: %s", domainName.toString());
}
}

View file

@ -30,7 +30,6 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.joda.money.CurrencyUnit.USD;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
@ -67,6 +66,7 @@ import google.registry.model.registry.label.ReservedList.ReservedListEntry;
import google.registry.util.Idn;
import java.util.Collection;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
import org.joda.money.CurrencyUnit;
@ -213,7 +213,7 @@ public class Registry extends ImmutableObject implements Buildable {
/** Returns the registry for a given TLD, throwing if none exists. */
public static Registry get(String tld) {
Registry registry = CACHE.getUnchecked(tld).orNull();
Registry registry = CACHE.getUnchecked(tld).orElse(null);
if (registry == null) {
throw new RegistryNotFoundException(tld);
}
@ -241,7 +241,7 @@ public class Registry extends ImmutableObject implements Buildable {
public Optional<Registry> load(final String tld) {
// Enter a transactionless context briefly; we don't want to enroll every TLD in a
// transaction that might be wrapping this call.
return Optional.fromNullable(
return Optional.ofNullable(
ofy()
.doTransactionless(
new Work<Registry>() {

View file

@ -19,7 +19,6 @@ import static com.google.common.base.Strings.isNullOrEmpty;
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
import static google.registry.model.registry.Registries.getTlds;
import com.google.common.base.Optional;
import com.google.common.cache.CacheLoader.InvalidCacheLoadException;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.HashMultiset;
@ -39,6 +38,7 @@ import google.registry.model.registry.label.ReservedList.ReservedListEntry;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import javax.annotation.Nullable;
import org.joda.time.DateTime;
@ -153,7 +153,7 @@ public abstract class BaseDomainLabelList<T extends Comparable<?>, R extends Dom
try {
return Optional.of(cache.get(listName));
} catch (InvalidCacheLoadException e) {
return Optional.absent();
return Optional.empty();
} catch (ExecutionException e) {
throw new UncheckedExecutionException("Could not retrieve list named " + listName, e);
}

View file

@ -13,6 +13,7 @@
// limitations under the License.
package google.registry.model.registry.label;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.hash.Funnels.unencodedCharsFunnel;
import static google.registry.config.RegistryConfig.getDomainLabelListCacheDuration;
@ -24,7 +25,6 @@ import static google.registry.model.ofy.ObjectifyService.ofy;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.base.Splitter;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
@ -46,6 +46,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import javax.annotation.Nullable;
@ -206,12 +207,9 @@ public final class PremiumList extends BaseDomainLabelList<Money, PremiumList.Pr
public Optional<PremiumListEntry> load(final Key<PremiumListEntry> entryKey) {
return ofy()
.doTransactionless(
new Work<Optional<PremiumListEntry>>() {
@Override
public Optional<PremiumListEntry> run() {
return Optional.fromNullable(ofy().load().key(entryKey).now());
}
});
() -> {
return Optional.ofNullable(ofy().load().key(entryKey).now());
});
}
});
}
@ -226,7 +224,7 @@ public final class PremiumList extends BaseDomainLabelList<Money, PremiumList.Pr
try {
return Optional.of(cachePremiumLists.get(name));
} catch (InvalidCacheLoadException e) {
return Optional.<PremiumList> absent();
return Optional.empty();
} catch (ExecutionException e) {
throw new UncheckedExecutionException("Could not retrieve premium list named " + name, e);
}

View file

@ -31,7 +31,6 @@ import static org.joda.time.DateTimeZone.UTC;
import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.cache.CacheLoader.InvalidCacheLoadException;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
@ -45,6 +44,7 @@ import google.registry.model.registry.label.PremiumList.PremiumListEntry;
import google.registry.model.registry.label.PremiumList.PremiumListRevision;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import org.joda.money.Money;
import org.joda.time.DateTime;
@ -73,7 +73,7 @@ public final class PremiumListUtils {
public static Optional<Money> getPremiumPrice(String label, Registry registry) {
// If the registry has no configured premium list, then no labels are premium.
if (registry.getPremiumList() == null) {
return Optional.<Money>absent();
return Optional.<Money>empty();
}
DateTime startTime = DateTime.now(UTC);
String listName = registry.getPremiumList().getName();
@ -103,7 +103,7 @@ public final class PremiumListUtils {
private static CheckResults checkStatus(PremiumListRevision premiumListRevision, String label) {
if (!premiumListRevision.getProbablePremiumLabels().mightContain(label)) {
return CheckResults.create(BLOOM_FILTER_NEGATIVE, Optional.<Money>absent());
return CheckResults.create(BLOOM_FILTER_NEGATIVE, Optional.<Money>empty());
}
Key<PremiumListEntry> entryKey =
@ -115,7 +115,7 @@ public final class PremiumListUtils {
if (entry.isPresent()) {
return CheckResults.create(CACHED_POSITIVE, Optional.of(entry.get().getValue()));
} else {
return CheckResults.create(CACHED_NEGATIVE, Optional.<Money>absent());
return CheckResults.create(CACHED_NEGATIVE, Optional.<Money>empty());
}
}
@ -123,7 +123,7 @@ public final class PremiumListUtils {
if (entry.isPresent()) {
return CheckResults.create(UNCACHED_POSITIVE, Optional.of(entry.get().getValue()));
} else {
return CheckResults.create(UNCACHED_NEGATIVE, Optional.<Money>absent());
return CheckResults.create(UNCACHED_NEGATIVE, Optional.<Money>empty());
}
} catch (InvalidCacheLoadException | ExecutionException e) {
throw new RuntimeException("Could not load premium list entry " + entryKey, e);
@ -173,7 +173,7 @@ public final class PremiumListUtils {
.id(premiumList.getName())
.now();
checkState(
Objects.equals(existing, oldPremiumList.orNull()),
Objects.equals(existing, oldPremiumList.orElse(null)),
"PremiumList was concurrently edited");
PremiumList newList = premiumList.asBuilder()
.setLastUpdateTime(now)

View file

@ -30,7 +30,6 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.joda.time.DateTimeZone.UTC;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Splitter;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
@ -49,6 +48,7 @@ import google.registry.model.registry.label.DomainLabelMetrics.MetricsReservedLi
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import javax.annotation.Nullable;
@ -338,7 +338,7 @@ public final class ReservedList
*/
public Optional<ReservationType> getReservationInList(String label) {
ReservedListEntry entry = getReservedListEntries().get(label);
return Optional.fromNullable(entry == null ? null : entry.reservationType);
return Optional.ofNullable(entry == null ? null : entry.reservationType);
}
@Override

View file

@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkArgument;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.DateTimeUtils.isAtOrAfter;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.googlecode.objectify.VoidWork;
import com.googlecode.objectify.Work;
@ -30,6 +29,7 @@ import google.registry.model.annotations.NotBackedUp.Reason;
import google.registry.util.FormattingLogger;
import google.registry.util.RequestStatusChecker;
import google.registry.util.RequestStatusCheckerImpl;
import java.util.Optional;
import javax.annotation.Nullable;
import org.joda.time.DateTime;
import org.joda.time.Duration;
@ -99,7 +99,7 @@ public class Lock extends ImmutableObject {
// It's important to use transactNew rather than transact, because a Lock can be used to control
// access to resources like GCS that can't be transactionally rolled back. Therefore, the lock
// must be definitively acquired before it is used, even when called inside another transaction.
return Optional.fromNullable(ofy().transactNew(new Work<Lock>() {
return Optional.ofNullable(ofy().transactNew(new Work<Lock>() {
@Override
public Lock run() {
String lockId = makeLockId(resourceName, tld);