Run automatic Java 8 conversion over codebase

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=171174380
This commit is contained in:
mcilwain 2017-10-05 10:48:38 -07:00 committed by Ben McIlwain
parent 44df5da771
commit 5edb7935ed
190 changed files with 2312 additions and 3096 deletions

View file

@ -19,14 +19,12 @@ import static google.registry.request.Action.Method.POST;
import static java.util.Arrays.asList;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import google.registry.config.RegistryConfig.Config;
import google.registry.groups.GroupsConnection;
import google.registry.groups.GroupsConnection.Role;
import google.registry.model.registrar.Registrar;
import google.registry.model.registrar.RegistrarContact;
import google.registry.model.registrar.RegistrarContact.Type;
import google.registry.request.Action;
import google.registry.request.HttpException.BadRequestException;
import google.registry.request.HttpException.InternalServerErrorException;
@ -70,26 +68,26 @@ public class CreateGroupsAction implements Runnable {
List<RegistrarContact.Type> types = asList(RegistrarContact.Type.values());
// Concurrently create the groups for each RegistrarContact.Type, collecting the results from
// each call (which are either an Exception if it failed, or absent() if it succeeded).
List<Optional<Exception>> results = Concurrent.transform(
types,
NUM_SIMULTANEOUS_CONNECTIONS,
new Function<RegistrarContact.Type, Optional<Exception>>() {
@Override
public Optional<Exception> apply(Type type) {
try {
String groupKey = getGroupEmailAddressForContactType(
registrar.getClientId(), type, gSuiteDomainName);
String parentGroup =
getGroupEmailAddressForContactType("registrar", type, gSuiteDomainName);
// Creates the group, then adds it as a member to the global registrar group for
// that type.
groupsConnection.createGroup(groupKey);
groupsConnection.addMemberToGroup(parentGroup, groupKey, Role.MEMBER);
return Optional.<Exception> absent();
} catch (Exception e) {
return Optional.of(e);
}
}});
List<Optional<Exception>> results =
Concurrent.transform(
types,
NUM_SIMULTANEOUS_CONNECTIONS,
type -> {
try {
String groupKey =
getGroupEmailAddressForContactType(
registrar.getClientId(), type, gSuiteDomainName);
String parentGroup =
getGroupEmailAddressForContactType("registrar", type, gSuiteDomainName);
// Creates the group, then adds it as a member to the global registrar group for
// that type.
groupsConnection.createGroup(groupKey);
groupsConnection.addMemberToGroup(parentGroup, groupKey, Role.MEMBER);
return Optional.<Exception>absent();
} catch (Exception e) {
return Optional.of(e);
}
});
// Return the correct server response based on the results of the group creations.
if (Optional.presentInstances(results).iterator().hasNext()) {
StringWriter responseString = new StringWriter();

View file

@ -16,6 +16,7 @@ package google.registry.tools.server;
import static com.google.appengine.tools.cloudstorage.GcsServiceFactory.createGcsService;
import static com.google.common.base.Predicates.notNull;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Iterators.filter;
import static com.google.common.io.BaseEncoding.base16;
import static google.registry.mapreduce.inputs.EppResourceInputs.createEntityInput;
@ -31,8 +32,6 @@ import com.google.appengine.tools.cloudstorage.RetryParams;
import com.google.appengine.tools.mapreduce.Mapper;
import com.google.appengine.tools.mapreduce.Reducer;
import com.google.appengine.tools.mapreduce.ReducerInput;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
@ -144,17 +143,13 @@ public class GenerateZoneFilesAction implements Runnable, JsonActionRunner.JsonA
ImmutableList.of(
new NullInput<EppResource>(),
createEntityInput(DomainResource.class)));
ImmutableList<String> filenames = FluentIterable.from(tlds)
.transform(
new Function<String, String>() {
@Override
public String apply(String tld) {
return String.format(
GCS_PATH_FORMAT,
bucket,
String.format(FILENAME_FORMAT, tld, exportTime));
}})
.toList();
ImmutableList<String> filenames =
tlds.stream()
.map(
tld ->
String.format(
GCS_PATH_FORMAT, bucket, String.format(FILENAME_FORMAT, tld, exportTime)))
.collect(toImmutableList());
return ImmutableMap.<String, Object>of(
"jobPath", createJobPath(jobId),
"filenames", filenames);

View file

@ -15,6 +15,7 @@
package google.registry.tools.server;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.request.Action.Method.POST;
import static google.registry.util.PipelineUtils.createJobPath;
@ -22,7 +23,6 @@ import static google.registry.util.PipelineUtils.createJobPath;
import com.google.appengine.tools.mapreduce.Input;
import com.google.appengine.tools.mapreduce.Mapper;
import com.google.appengine.tools.mapreduce.inputs.InMemoryInput;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.googlecode.objectify.Key;
@ -33,7 +33,7 @@ import google.registry.model.ofy.CommitLogCheckpointRoot;
import google.registry.request.Action;
import google.registry.request.Response;
import google.registry.request.auth.Auth;
import java.util.Arrays;
import java.util.stream.Stream;
import javax.inject.Inject;
/**
@ -63,13 +63,14 @@ public class KillAllCommitLogsAction implements Runnable {
"DO NOT RUN ANYWHERE ELSE EXCEPT CRASH OR TESTS.");
// Create a in-memory input, assigning each bucket to its own shard for maximum parallelization,
// with one extra shard for the CommitLogCheckpointRoot.
Input<Key<?>> input = new InMemoryInput<>(
Lists.partition(
FluentIterable
.from(Arrays.<Key<?>>asList(CommitLogCheckpointRoot.getKey()))
.append(CommitLogBucket.getAllBucketKeys())
.toList(),
1));
Input<Key<?>> input =
new InMemoryInput<>(
Lists.partition(
Stream.concat(
Stream.of(CommitLogCheckpointRoot.getKey()),
CommitLogBucket.getAllBucketKeys().stream())
.collect(toImmutableList()),
1));
response.sendJavaScriptRedirect(createJobPath(mrRunner
.setJobName("Delete all commit logs")
.setModuleName("tools")

View file

@ -19,6 +19,7 @@ import static google.registry.model.EppResourceUtils.queryNotDeleted;
import static google.registry.model.registry.Registries.assertTldsExist;
import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.POST;
import static java.util.Comparator.comparing;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
@ -28,7 +29,6 @@ import google.registry.request.Action;
import google.registry.request.Parameter;
import google.registry.request.auth.Auth;
import google.registry.util.Clock;
import java.util.Comparator;
import java.util.List;
import javax.inject.Inject;
@ -42,12 +42,6 @@ public final class ListDomainsAction extends ListObjectsAction<DomainResource> {
/** An App Engine limitation on how many subqueries can be used in a single query. */
private static final int MAX_NUM_SUBQUERIES = 30;
private static final Comparator<DomainResource> COMPARATOR =
new Comparator<DomainResource>() {
@Override
public int compare(DomainResource a, DomainResource b) {
return a.getFullyQualifiedDomainName().compareTo(b.getFullyQualifiedDomainName());
}};
public static final String PATH = "/_dr/admin/list/domains";
@Inject @Parameter("tlds") ImmutableSet<String> tlds;
@ -64,7 +58,8 @@ public final class ListDomainsAction extends ListObjectsAction<DomainResource> {
checkArgument(!tlds.isEmpty(), "Must specify TLDs to query");
assertTldsExist(tlds);
ImmutableSortedSet.Builder<DomainResource> builder =
new ImmutableSortedSet.Builder<DomainResource>(COMPARATOR);
new ImmutableSortedSet.Builder<DomainResource>(
comparing(DomainResource::getFullyQualifiedDomainName));
for (List<String> batch : Lists.partition(tlds.asList(), MAX_NUM_SUBQUERIES)) {
builder.addAll(queryNotDeleted(DomainResource.class, clock.nowUtc(), "tld in", batch));
}

View file

@ -14,19 +14,19 @@
package google.registry.tools.server;
import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.POST;
import static java.util.Comparator.comparing;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import google.registry.model.EppResourceUtils;
import google.registry.model.host.HostResource;
import google.registry.request.Action;
import google.registry.request.auth.Auth;
import google.registry.util.Clock;
import java.util.Comparator;
import javax.inject.Inject;
import org.joda.time.DateTime;
@ -40,14 +40,6 @@ public final class ListHostsAction extends ListObjectsAction<HostResource> {
public static final String PATH = "/_dr/admin/list/hosts";
private static final Comparator<HostResource> comparator =
new Comparator<HostResource>() {
@Override
public int compare(HostResource host1, HostResource host2) {
return host1.getFullyQualifiedHostName()
.compareTo(host2.getFullyQualifiedHostName());
}};
@Inject Clock clock;
@Inject ListHostsAction() {}
@ -59,13 +51,8 @@ public final class ListHostsAction extends ListObjectsAction<HostResource> {
@Override
public ImmutableSet<HostResource> loadObjects() {
final DateTime now = clock.nowUtc();
return FluentIterable
.from(ofy().load().type(HostResource.class))
.filter(new Predicate<HostResource>() {
@Override
public boolean apply(HostResource host) {
return EppResourceUtils.isActive(host, now);
}})
.toSortedSet(comparator);
return Streams.stream(ofy().load().type(HostResource.class))
.filter(host -> EppResourceUtils.isActive(host, now))
.collect(toImmutableSortedSet(comparing(HostResource::getFullyQualifiedHostName)));
}
}

View file

@ -16,6 +16,7 @@ package google.registry.tools.server;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import com.google.common.base.Function;
import com.google.common.base.Functions;
@ -29,7 +30,6 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.ImmutableTable;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import google.registry.model.ImmutableObject;
@ -42,6 +42,7 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;
import javax.inject.Inject;
/**
@ -150,14 +151,9 @@ public abstract class ListObjectsAction<T extends ImmutableObject> implements Ru
final ImmutableMap<String, String> nameMapping =
((fullFieldNames != null) && fullFieldNames.isPresent() && fullFieldNames.get())
? getFieldAliases() : getFieldAliases().inverse();
return ImmutableSet.copyOf(Iterables.transform(
Iterables.concat(getPrimaryKeyFields(), fieldsToUse),
new Function<String, String>() {
@Override
public String apply(String field) {
// Rename fields that are in the map according to the map, and leave the others as is.
return nameMapping.containsKey(field) ? nameMapping.get(field) : field;
}}));
return Stream.concat(getPrimaryKeyFields().stream(), fieldsToUse.stream())
.map(field -> nameMapping.getOrDefault(field, field))
.collect(toImmutableSet());
}
/**
@ -209,22 +205,16 @@ public abstract class ListObjectsAction<T extends ImmutableObject> implements Ru
*/
private static ImmutableMap<String, Integer> computeColumnWidths(
ImmutableTable<?, String, String> data, final boolean includingHeader) {
return ImmutableMap.copyOf(Maps.transformEntries(
data.columnMap(),
new Maps.EntryTransformer<String, Map<?, String>, Integer>() {
@Override
public Integer transformEntry(String columnName, Map<?, String> columnValues) {
// Return the length of the longest string in this column (including the column name).
return Ordering.natural().max(Iterables.transform(
Iterables.concat(
ImmutableList.of(includingHeader ? columnName : ""),
columnValues.values()),
new Function<String, Integer>() {
@Override
public Integer apply(String value) {
return value.length();
}}));
}}));
return ImmutableMap.copyOf(
Maps.transformEntries(
data.columnMap(),
(columnName, columnValues) ->
Stream.concat(
Stream.of(includingHeader ? columnName : ""),
columnValues.values().stream())
.map(String::length)
.max(Ordering.natural())
.get()));
}
/**
@ -250,12 +240,8 @@ public abstract class ListObjectsAction<T extends ImmutableObject> implements Ru
lines.add(rowFormatter.apply(headerRow));
// Add a row of separator lines (column names mapping to '-' * column width).
Map<String, String> separatorRow = Maps.transformValues(columnWidths,
new Function<Integer, String>() {
@Override
public String apply(Integer width) {
return Strings.repeat("-", width);
}});
Map<String, String> separatorRow =
Maps.transformValues(columnWidths, width -> Strings.repeat("-", width));
lines.add(rowFormatter.apply(separatorRow));
}
@ -275,14 +261,12 @@ public abstract class ListObjectsAction<T extends ImmutableObject> implements Ru
*/
private static Function<Map<String, String>, String> makeRowFormatter(
final Map<String, Integer> columnWidths) {
return new Function<Map<String, String>, String>() {
@Override
public String apply(Map<String, String> rowByColumns) {
List<String> paddedFields = new ArrayList<>();
for (Map.Entry<String, String> cell : rowByColumns.entrySet()) {
paddedFields.add(Strings.padEnd(cell.getValue(), columnWidths.get(cell.getKey()), ' '));
}
return Joiner.on(" ").join(paddedFields);
}};
return rowByColumns -> {
List<String> paddedFields = new ArrayList<>();
for (Map.Entry<String, String> cell : rowByColumns.entrySet()) {
paddedFields.add(Strings.padEnd(cell.getValue(), columnWidths.get(cell.getKey()), ' '));
}
return Joiner.on(" ").join(paddedFields);
};
}
}

View file

@ -14,12 +14,11 @@
package google.registry.tools.server;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static google.registry.model.registry.Registries.getTlds;
import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.POST;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
@ -50,13 +49,7 @@ public final class ListTldsAction extends ListObjectsAction<Registry> {
@Override
public ImmutableSet<Registry> loadObjects() {
return FluentIterable.from(getTlds())
.transform(new Function<String, Registry>() {
@Override
public Registry apply(String tldString) {
return Registry.get(tldString);
}})
.toSet();
return getTlds().stream().map(Registry::get).collect(toImmutableSet());
}
@Override

View file

@ -16,6 +16,7 @@ package google.registry.tools.server;
import static com.google.common.base.Predicates.equalTo;
import static com.google.common.base.Predicates.not;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Maps.toMap;
import static google.registry.flows.EppXmlTransformer.unmarshal;
import static google.registry.model.ofy.ObjectifyService.ofy;
@ -23,7 +24,6 @@ import static google.registry.util.CollectionUtils.isNullOrEmpty;
import static google.registry.util.DomainNameUtils.ACE_PREFIX;
import com.google.common.base.Ascii;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
@ -51,7 +51,7 @@ import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import java.util.stream.Stream;
import javax.inject.Inject;
/**
@ -82,14 +82,7 @@ public class VerifyOteAction implements Runnable, JsonAction {
public Map<String, Object> handleJsonRequest(Map<String, ?> json) {
final boolean summarize = Boolean.parseBoolean((String) json.get("summarize"));
return toMap(
(List<String>) json.get("registrars"),
new Function<String, Object>() {
@Nonnull
@Override
public Object apply(@Nonnull String registrar) {
return checkRegistrar(registrar, summarize);
}
});
(List<String>) json.get("registrars"), registrar -> checkRegistrar(registrar, summarize));
}
/** Checks whether the provided registrar has passed OT&amp;E and returns relevant information. */
@ -112,66 +105,37 @@ public class VerifyOteAction implements Runnable, JsonAction {
}
private static final Predicate<EppInput> HAS_CLAIMS_NOTICE =
new Predicate<EppInput>() {
@Override
public boolean apply(@Nonnull EppInput eppInput) {
LaunchCreateExtension launchCreate =
eppInput.getSingleExtension(LaunchCreateExtension.class);
return launchCreate != null && launchCreate.getNotice() != null;
}
};
private static final Predicate<EppInput> HAS_FEE =
new Predicate<EppInput>() {
@Override
public boolean apply(@Nonnull EppInput eppInput) {
return eppInput.getSingleExtension(FeeCreateCommandExtension.class) != null;
}
eppInput -> {
LaunchCreateExtension launchCreate =
eppInput.getSingleExtension(LaunchCreateExtension.class);
return launchCreate != null && launchCreate.getNotice() != null;
};
private static final Predicate<EppInput> HAS_SEC_DNS =
new Predicate<EppInput>() {
@Override
public boolean apply(@Nonnull EppInput eppInput) {
return (eppInput.getSingleExtension(SecDnsCreateExtension.class) != null)
eppInput ->
(eppInput.getSingleExtension(SecDnsCreateExtension.class) != null)
|| (eppInput.getSingleExtension(SecDnsUpdateExtension.class) != null);
}
};
private static final Predicate<EppInput> IS_SUNRISE =
new Predicate<EppInput>() {
@Override
public boolean apply(@Nonnull EppInput eppInput) {
LaunchCreateExtension launchCreate =
eppInput.getSingleExtension(LaunchCreateExtension.class);
return launchCreate != null && !isNullOrEmpty(launchCreate.getSignedMarks());
}
eppInput -> {
LaunchCreateExtension launchCreate =
eppInput.getSingleExtension(LaunchCreateExtension.class);
return launchCreate != null && !isNullOrEmpty(launchCreate.getSignedMarks());
};
private static final Predicate<EppInput> IS_IDN =
new Predicate<EppInput>() {
@Override
public boolean apply(@Nonnull EppInput eppInput) {
return ((DomainCommand.Create)
eppInput ->
((DomainCommand.Create)
((ResourceCommandWrapper) eppInput.getCommandWrapper().getCommand())
.getResourceCommand())
.getFullyQualifiedDomainName()
.startsWith(ACE_PREFIX);
}
};
private static final Predicate<EppInput> IS_SUBORDINATE =
new Predicate<EppInput>() {
@Override
public boolean apply(@Nonnull EppInput eppInput) {
return !isNullOrEmpty(
eppInput ->
!isNullOrEmpty(
((HostCommand.Create)
((ResourceCommandWrapper) eppInput.getCommandWrapper().getCommand())
.getResourceCommand())
.getInetAddresses());
}
};
/** Enum defining the distinct statistics (types of registrar actions) to record. */
public enum StatType {
CONTACT_CREATES(0, equalTo(Type.CONTACT_CREATE)),
@ -192,7 +156,10 @@ public class VerifyOteAction implements Runnable, JsonAction {
DOMAIN_CREATES_ASCII(1, equalTo(Type.DOMAIN_CREATE), not(IS_IDN)),
DOMAIN_CREATES_IDN(1, equalTo(Type.DOMAIN_CREATE), IS_IDN),
DOMAIN_CREATES_WITH_CLAIMS_NOTICE(1, equalTo(Type.DOMAIN_CREATE), HAS_CLAIMS_NOTICE),
DOMAIN_CREATES_WITH_FEE(1, equalTo(Type.DOMAIN_CREATE), HAS_FEE),
DOMAIN_CREATES_WITH_FEE(
1,
equalTo(Type.DOMAIN_CREATE),
eppInput -> eppInput.getSingleExtension(FeeCreateCommandExtension.class) != null),
DOMAIN_CREATES_WITH_SEC_DNS(1, equalTo(Type.DOMAIN_CREATE), HAS_SEC_DNS),
DOMAIN_CREATES_WITHOUT_SEC_DNS(0, equalTo(Type.DOMAIN_CREATE), not(HAS_SEC_DNS)),
DOMAIN_DELETES(2, equalTo(Type.DOMAIN_DELETE)),
@ -214,15 +181,7 @@ public class VerifyOteAction implements Runnable, JsonAction {
/** The number of StatTypes with a non-zero requirement. */
private static final int NUM_REQUIREMENTS =
FluentIterable.from(values())
.filter(
new Predicate<StatType>() {
@Override
public boolean apply(@Nonnull StatType statType) {
return statType.requirement > 0;
}
})
.size();
(int) Stream.of(values()).filter(statType -> statType.requirement > 0).count();
/** Required number of times registrars must complete this action. */
final int requirement;
@ -304,15 +263,10 @@ public class VerifyOteAction implements Runnable, JsonAction {
? Optional.<EppInput>absent()
: Optional.of(unmarshal(EppInput.class, xmlBytes));
if (!statCounts.addAll(
FluentIterable.from(EnumSet.allOf(StatType.class))
.filter(
new Predicate<StatType>() {
@Override
public boolean apply(@Nonnull StatType statType) {
return statType.matches(historyEntry.getType(), eppInput);
}
})
.toList())) {
EnumSet.allOf(StatType.class)
.stream()
.filter(statType -> statType.matches(historyEntry.getType(), eppInput))
.collect(toImmutableList()))) {
statCounts.add(StatType.UNCLASSIFIED_FLOWS);
}
}
@ -339,14 +293,8 @@ public class VerifyOteAction implements Runnable, JsonAction {
public String toString() {
return FluentIterable.from(EnumSet.allOf(StatType.class))
.transform(
new Function<StatType, String>() {
@Nonnull
@Override
public String apply(@Nonnull StatType statType) {
return String.format(
"%s: %d", statType.description(), statCounts.count(statType));
}
})
statType ->
String.format("%s: %d", statType.description(), statCounts.count(statType)))
.append(String.format("TOTAL: %d", statCounts.size()))
.join(Joiner.on("\n"));
}