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

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