Replace FluentIterable with streams

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=180005797
This commit is contained in:
guyben 2017-12-23 07:08:35 -08:00 committed by Ben McIlwain
parent 552ab12314
commit 3f7cd00882
13 changed files with 121 additions and 104 deletions

View file

@ -17,6 +17,7 @@ package google.registry.export;
import static com.google.appengine.api.datastore.DatastoreServiceFactory.getDatastoreService;
import static com.google.appengine.api.taskqueue.QueueFactory.getQueue;
import static com.google.common.base.Strings.nullToEmpty;
import static com.google.common.collect.ImmutableList.toImmutableList;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.modules.ModulesService;
@ -24,9 +25,9 @@ import com.google.appengine.api.modules.ModulesServiceFactory;
import com.google.appengine.api.taskqueue.TaskHandle;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.google.appengine.api.taskqueue.TaskOptions.Method;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Streams;
import google.registry.util.NonFinalForTesting;
import java.util.NoSuchElementException;
@ -88,10 +89,10 @@ public class DatastoreBackupService {
public Iterable<DatastoreBackupInfo> findAllByNamePrefix(final String namePrefix) {
// Need the raw DatastoreService to access the internal _AE_Backup_Information entities.
// TODO(b/19081037): make an Objectify entity class for these raw Datastore entities instead.
return FluentIterable.from(
getDatastoreService().prepare(new Query(BACKUP_INFO_KIND)).asIterable())
return Streams.stream(getDatastoreService().prepare(new Query(BACKUP_INFO_KIND)).asIterable())
.filter(entity -> nullToEmpty((String) entity.getProperty("name")).startsWith(namePrefix))
.transform(DatastoreBackupInfo::new);
.map(DatastoreBackupInfo::new)
.collect(toImmutableList());
}
/**

View file

@ -21,7 +21,6 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Throwables;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import google.registry.flows.EppException.ParameterValueRangeErrorException;
import google.registry.flows.EppException.ParameterValueSyntaxErrorException;
@ -40,6 +39,7 @@ import google.registry.xml.XmlException;
import google.registry.xml.XmlTransformer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.List;
/** {@link XmlTransformer} for marshalling to and from the Epp model classes. */
public class EppXmlTransformer {
@ -87,17 +87,17 @@ public class EppXmlTransformer {
return INPUT_TRANSFORMER.unmarshal(clazz, new ByteArrayInputStream(bytes));
} catch (XmlException e) {
// If this XmlException is wrapping a known type find it. If not, it's a syntax error.
FluentIterable<Throwable> causalChain = FluentIterable.from(Throwables.getCausalChain(e));
if (!(causalChain.filter(IpVersionMismatchException.class).isEmpty())) {
List<Throwable> causalChain = Throwables.getCausalChain(e);
if (causalChain.stream().anyMatch(IpVersionMismatchException.class::isInstance)) {
throw new IpAddressVersionMismatchException();
}
if (!(causalChain.filter(WrongProtocolVersionException.class).isEmpty())) {
if (causalChain.stream().anyMatch(WrongProtocolVersionException.class::isInstance)) {
throw new UnimplementedProtocolVersionException();
}
if (!(causalChain.filter(InvalidRepoIdException.class).isEmpty())) {
if (causalChain.stream().anyMatch(InvalidRepoIdException.class::isInstance)) {
throw new InvalidRepoIdEppException();
}
if (!(causalChain.filter(UnknownCurrencyException.class).isEmpty())) {
if (causalChain.stream().anyMatch(UnknownCurrencyException.class::isInstance)) {
throw new UnknownCurrencyEppException();
}
throw new GenericSyntaxErrorException(e.getMessage());

View file

@ -22,16 +22,13 @@ import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Ordering;
import com.google.common.collect.Streams;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Id;
@ -44,6 +41,7 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.AbstractList;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collection;
import java.util.Deque;
import java.util.LinkedHashMap;
@ -52,6 +50,7 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** A collection of static methods that deal with reflection on model classes. */
@ -94,17 +93,19 @@ public class ModelUtils {
/** Return a string representing the persisted schema of a type or enum. */
static String getSchema(Class<?> clazz) {
StringBuilder stringBuilder = new StringBuilder();
Iterable<?> body;
Stream<?> body;
if (clazz.isEnum()) {
stringBuilder.append("enum ");
body = FluentIterable.from(clazz.getEnumConstants());
body = Arrays.stream(clazz.getEnumConstants());
} else {
stringBuilder.append("class ");
body =
FluentIterable.from(getAllFields(clazz).values())
.filter((Field field) -> !field.isAnnotationPresent(Ignore.class))
.transform(
(Field field) -> {
getAllFields(clazz)
.values()
.stream()
.filter(field -> !field.isAnnotationPresent(Ignore.class))
.map(
field -> {
String annotation =
field.isAnnotationPresent(Id.class)
? "@Id "
@ -117,8 +118,9 @@ public class ModelUtils {
});
}
return stringBuilder
.append(clazz.getName()).append(" {\n ")
.append(Joiner.on(";\n ").join(Ordering.usingToString().sortedCopy(body)))
.append(clazz.getName())
.append(" {\n ")
.append(body.map(Object::toString).sorted().collect(Collectors.joining(";\n ")))
.append(";\n}")
.toString();
}

View file

@ -15,6 +15,7 @@
package google.registry.model.common;
import static com.google.common.collect.DiscreteDomain.integers;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static google.registry.util.DateTimeUtils.isAtOrAfter;
@ -23,7 +24,6 @@ import static org.joda.time.DateTimeZone.UTC;
import com.google.common.base.Splitter;
import com.google.common.collect.ContiguousSet;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Range;
import com.googlecode.objectify.annotation.Embed;
import com.googlecode.objectify.annotation.Index;
@ -83,9 +83,11 @@ public class TimeOfYear extends ImmutableObject {
Range<Integer> yearRange = Range.closed(
normalizedRange.lowerEndpoint().getYear(),
normalizedRange.upperEndpoint().getYear());
return FluentIterable.from(ContiguousSet.create(yearRange, integers()))
.transform(this::getDateTimeWithYear)
.filter(normalizedRange);
return ContiguousSet.create(yearRange, integers())
.stream()
.map(this::getDateTimeWithYear)
.filter(normalizedRange)
.collect(toImmutableList());
}
/** Get the first {@link DateTime} with this month/day/millis that is at or after the start. */

View file

@ -14,16 +14,17 @@
package google.registry.model.ofy;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.googlecode.objectify.ObjectifyService.ofy;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Streams;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Result;
import com.googlecode.objectify.cmd.DeleteType;
import com.googlecode.objectify.cmd.Deleter;
import java.util.Arrays;
import java.util.stream.Stream;
/**
* A Deleter that forwards to {@code ofy().delete()}, but can be augmented via subclassing to
@ -35,21 +36,25 @@ abstract class AugmentedDeleter implements Deleter {
/** Extension method to allow this Deleter to do extra work prior to the actual delete. */
protected abstract void handleDeletion(Iterable<Key<?>> keys);
private void handleDeletionStream(Stream<?> entityStream) {
handleDeletion(entityStream.map(Key::create).collect(toImmutableList()));
}
@Override
public Result<Void> entities(Iterable<?> entities) {
handleDeletion(Iterables.transform(entities, Key::create));
handleDeletionStream(Streams.stream(entities));
return delegate.entities(entities);
}
@Override
public Result<Void> entities(Object... entities) {
handleDeletion(FluentIterable.from(entities).transform(Key::create));
handleDeletionStream(Arrays.stream(entities));
return delegate.entities(entities);
}
@Override
public Result<Void> entity(Object entity) {
handleDeletion(Arrays.asList(Key.create(entity)));
handleDeletionStream(Stream.of(entity));
return delegate.entity(entity);
}

View file

@ -14,11 +14,10 @@
package google.registry.model.ofy;
import com.google.common.base.Joiner;
import com.google.common.collect.FluentIterable;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import google.registry.model.BackupGroupRoot;
import java.util.Arrays;
import java.util.Map;
import org.joda.time.DateTime;
@ -43,19 +42,19 @@ class TimestampInversionException extends RuntimeException {
private TimestampInversionException(DateTime transactionTime, String problem) {
super(
Joiner.on('\n')
.join(
String.format(
"Timestamp inversion between transaction time (%s) and %s",
transactionTime, problem),
getFileAndLine(
FluentIterable.from(new Exception().getStackTrace())
.firstMatch(
(StackTraceElement element) ->
!element
.getClassName()
.startsWith(Objectify.class.getPackage().getName())
&& !element.getClassName().startsWith(Ofy.class.getName()))
.get())));
String.format(
"Timestamp inversion between transaction time (%s) and %s\n%s",
transactionTime,
problem,
getFileAndLine(
Arrays.stream(new Exception().getStackTrace())
.filter(
element ->
!element
.getClassName()
.startsWith(Objectify.class.getPackage().getName())
&& !element.getClassName().startsWith(Ofy.class.getName()))
.findFirst()
.get())));
}
}

View file

@ -21,9 +21,7 @@ import static google.registry.util.DateTimeUtils.END_OF_TIME;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Streams;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.EntitySubclass;
@ -31,6 +29,7 @@ import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Parent;
import google.registry.model.Buildable;
import google.registry.model.EppResource;
import google.registry.model.ImmutableObject;
import google.registry.model.annotations.ExternalMessagingName;
import google.registry.model.domain.DomainRenewData;
@ -232,34 +231,38 @@ public abstract class PollMessage extends ImmutableObject
}
public Builder setResponseData(ImmutableList<? extends ResponseData> responseData) {
FluentIterable<? extends ResponseData> iterable = FluentIterable.from(responseData);
getInstance().contactPendingActionNotificationResponses =
forceEmptyToNull(
Streams.stream(iterable)
responseData
.stream()
.filter(ContactPendingActionNotificationResponse.class::isInstance)
.map(ContactPendingActionNotificationResponse.class::cast)
.collect(toImmutableList()));
getInstance().contactTransferResponses =
forceEmptyToNull(
Streams.stream(iterable)
responseData
.stream()
.filter(ContactTransferResponse.class::isInstance)
.map(ContactTransferResponse.class::cast)
.collect(toImmutableList()));
getInstance().domainPendingActionNotificationResponses =
forceEmptyToNull(
Streams.stream(iterable)
responseData
.stream()
.filter(DomainPendingActionNotificationResponse.class::isInstance)
.map(DomainPendingActionNotificationResponse.class::cast)
.collect(toImmutableList()));
getInstance().domainTransferResponses =
forceEmptyToNull(
Streams.stream(iterable)
responseData
.stream()
.filter(DomainTransferResponse.class::isInstance)
.map(DomainTransferResponse.class::cast)
.collect(toImmutableList()));
getInstance().hostPendingActionNotificationResponses =
forceEmptyToNull(
Streams.stream(iterable)
responseData
.stream()
.filter(HostPendingActionNotificationResponse.class::isInstance)
.map(HostPendingActionNotificationResponse.class::cast)
.collect(toImmutableList()));

View file

@ -16,6 +16,7 @@ package google.registry.model.smd;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Iterables.isEmpty;
import static google.registry.model.CacheUtils.memoizeWithShortExpiration;
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
@ -26,7 +27,6 @@ import static google.registry.util.DateTimeUtils.isBeforeOrAt;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Supplier;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.googlecode.objectify.Key;
@ -164,15 +164,17 @@ public class SignedMarkRevocationList extends ImmutableObject {
ofy()
.saveWithoutBackup()
.entities(
FluentIterable.from(CollectionUtils.partitionMap(revokes, SHARD_SIZE))
.transform(
(ImmutableMap<String, DateTime> shardRevokes) -> {
CollectionUtils.partitionMap(revokes, SHARD_SIZE)
.stream()
.map(
shardRevokes -> {
SignedMarkRevocationList shard = create(creationTime, shardRevokes);
shard.id = allocateId();
shard.isShard =
true; // Avoid the exception in disallowUnshardedSaves().
return shard;
}));
})
.collect(toImmutableList()));
});
return this;
}

View file

@ -15,18 +15,19 @@
package google.registry.rdap;
import static com.google.common.base.Strings.nullToEmpty;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet;
import static google.registry.model.EppResourceUtils.isLinked;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.CollectionUtils.union;
import static google.registry.util.DomainNameUtils.ACE_PREFIX;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import com.google.common.collect.Streams;
import com.google.common.net.InetAddresses;
import com.googlecode.objectify.Key;
import google.registry.config.RdapNoticeDescriptor;
@ -522,23 +523,24 @@ public class RdapJsonFormatter {
if (displayContacts) {
Map<Key<ContactResource>, ContactResource> loadedContacts =
ofy().load().keys(domainResource.getReferencedContacts());
ImmutableList.Builder<Object> entitiesBuilder = new ImmutableList.Builder<>();
for (DesignatedContact designatedContact :
FluentIterable.from(domainResource.getContacts())
.append(DesignatedContact.create(Type.REGISTRANT, domainResource.getRegistrant()))
.toSortedList(DESIGNATED_CONTACT_ORDERING)) {
ContactResource loadedContact = loadedContacts.get(designatedContact.getContactKey());
entitiesBuilder.add(makeRdapJsonForContact(
loadedContact,
false,
Optional.of(designatedContact.getType()),
linkBase,
null,
now,
outputDataType,
authorization));
}
ImmutableList<Object> entities = entitiesBuilder.build();
ImmutableList<ImmutableMap<String, Object>> entities =
Streams.concat(
domainResource.getContacts().stream(),
Stream.of(
DesignatedContact.create(Type.REGISTRANT, domainResource.getRegistrant())))
.sorted(DESIGNATED_CONTACT_ORDERING)
.map(
designatedContact ->
makeRdapJsonForContact(
loadedContacts.get(designatedContact.getContactKey()),
false,
Optional.of(designatedContact.getType()),
linkBase,
null,
now,
outputDataType,
authorization))
.collect(toImmutableList());
if (!entities.isEmpty()) {
jsonBuilder.put("entities", entities);
}
@ -845,14 +847,13 @@ public class RdapJsonFormatter {
jsonBuilder.put("events", events);
}
// include the registrar contacts as subentities
ImmutableList.Builder<Map<String, Object>> registrarContactsBuilder =
new ImmutableList.Builder<>();
for (RegistrarContact registrarContact : registrar.getContacts()) {
if (isVisible(registrarContact)) {
registrarContactsBuilder.add(makeRdapJsonForRegistrarContact(registrarContact, null));
}
}
ImmutableList<Map<String, Object>> registrarContacts = registrarContactsBuilder.build();
ImmutableList<ImmutableMap<String, Object>> registrarContacts =
registrar
.getContacts()
.stream()
.filter(RdapJsonFormatter::isVisible)
.map(registrarContact -> makeRdapJsonForRegistrarContact(registrarContact, null))
.collect(toImmutableList());
if (!registrarContacts.isEmpty()) {
jsonBuilder.put("entities", registrarContacts);
}
@ -1096,7 +1097,7 @@ public class RdapJsonFormatter {
.map(status -> statusToRdapStatusMap.getOrDefault(status, RdapStatus.OBSCURED));
if (isDeleted) {
stream =
Stream.concat(
Streams.concat(
stream.filter(rdapStatus -> !Objects.equals(rdapStatus, RdapStatus.ACTIVE)),
Stream.of(RdapStatus.REMOVED));
}

View file

@ -25,7 +25,6 @@ import static google.registry.util.DomainNameUtils.ACE_PREFIX;
import com.google.common.base.Ascii;
import com.google.common.base.Joiner;
import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multiset;
@ -50,6 +49,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.inject.Inject;
@ -290,12 +290,13 @@ public class VerifyOteAction implements Runnable, JsonAction {
/** Returns a string showing all possible actions and how many times each was performed. */
@Override
public String toString() {
return FluentIterable.from(EnumSet.allOf(StatType.class))
.transform(
statType ->
String.format("%s: %d", statType.description(), statCounts.count(statType)))
.append(String.format("TOTAL: %d", statCounts.size()))
.join(Joiner.on("\n"));
return String.format(
"%s\nTOTAL: %d",
EnumSet.allOf(StatType.class)
.stream()
.map(stat -> String.format("%s: %d", stat.description(), statCounts.count(stat)))
.collect(Collectors.joining("\n")),
statCounts.size());
}
}
}

View file

@ -14,7 +14,7 @@
package google.registry.util;
import com.google.common.collect.FluentIterable;
import java.util.Arrays;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -44,8 +44,9 @@ public class FormattingLogger {
private void log(Level level, @Nullable Throwable cause, String msg) {
StackTraceElement callerFrame =
FluentIterable.from(new Exception().getStackTrace())
.firstMatch(frame -> !frame.getClassName().equals(FormattingLogger.class.getName()))
Arrays.stream(new Exception().getStackTrace())
.filter(frame -> !frame.getClassName().equals(FormattingLogger.class.getName()))
.findFirst()
.get();
if (cause == null) {
logger.logp(level, callerFrame.getClassName(), callerFrame.getMethodName(), msg);

View file

@ -15,10 +15,10 @@
package google.registry.rde;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
import static google.registry.testing.DatastoreHelper.createTld;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import google.registry.model.contact.ContactAddress;
@ -87,7 +87,7 @@ public class ContactResourceToXjcConverterTest {
// o One or more <status> elements that describe the status of the
// contact object.
assertThat(FluentIterable.from(bean.getStatuses()).transform(XjcContactStatusType::getS))
assertThat(bean.getStatuses().stream().map(XjcContactStatusType::getS))
.containsExactly(
XjcContactStatusValueType.CLIENT_DELETE_PROHIBITED,
XjcContactStatusValueType.SERVER_UPDATE_PROHIBITED);

View file

@ -16,6 +16,7 @@ package google.registry.rde;
import static com.google.common.io.BaseEncoding.base16;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.newDomainResource;
import static google.registry.testing.DatastoreHelper.persistEppResource;
@ -26,7 +27,6 @@ import static google.registry.xjc.rgp.XjcRgpStatusValueType.TRANSFER_PERIOD;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.joda.money.CurrencyUnit.USD;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.net.InetAddresses;
@ -106,9 +106,9 @@ public class DomainResourceToXjcConverterTest {
assertThat(bean.getClID()).isEqualTo("GetTheeBack");
assertThat(
FluentIterable.from(bean.getContacts())
.transform(
input -> String.format("%s %s", input.getType().toString(), input.getValue())))
bean.getContacts()
.stream()
.map(input -> String.format("%s %s", input.getType().toString(), input.getValue())))
.containsExactly("ADMIN 5372808-IRL", "TECH 5372808-TRL");
assertThat(bean.getCrDate()).isEqualTo(DateTime.parse("1900-01-01T00:00:00Z"));
@ -144,7 +144,7 @@ public class DomainResourceToXjcConverterTest {
// "pendingDelete" sub-statuses, including "redemptionPeriod",
// "pendingRestore", and "pendingDelete", that a domain name can be
// in as a result of grace period processing as specified in [RFC3915].
assertThat(FluentIterable.from(bean.getRgpStatuses()).transform(XjcRgpStatusType::getS))
assertThat(bean.getRgpStatuses().stream().map(XjcRgpStatusType::getS))
.containsExactly(TRANSFER_PERIOD, RENEW_PERIOD);
assertThat(bean.getSecDNS()).named("secdns").isNotNull();
@ -159,7 +159,7 @@ public class DomainResourceToXjcConverterTest {
assertThat(bean.getRoid()).isEqualTo("2-Q9JYB4C");
assertThat(FluentIterable.from(bean.getStatuses()).transform(XjcDomainStatusType::getS))
assertThat(bean.getStatuses().stream().map(XjcDomainStatusType::getS))
.containsExactly(
XjcDomainStatusValueType.CLIENT_DELETE_PROHIBITED,
XjcDomainStatusValueType.CLIENT_RENEW_PROHIBITED,