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

@ -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());
}