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

@ -21,7 +21,6 @@ import static google.registry.util.DateTimeUtils.END_OF_TIME;
import static google.registry.util.DateTimeUtils.earliestOf;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Work;
@ -167,10 +166,7 @@ class CommitLogCheckpointStrategy {
ImmutableMap<Integer, DateTime> computeBucketCheckpointTimes(
ImmutableMap<Integer, DateTime> firstPassTimes,
final DateTime threshold) {
return ImmutableMap.copyOf(transformValues(firstPassTimes, new Function<DateTime, DateTime>() {
@Override
public DateTime apply(DateTime firstPassTime) {
return earliestOf(firstPassTime, threshold);
}}));
return ImmutableMap.copyOf(
transformValues(firstPassTimes, firstPassTime -> earliestOf(firstPassTime, threshold)));
}
}

View file

@ -16,6 +16,7 @@ package google.registry.backup;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Verify.verifyNotNull;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Lists.partition;
import static google.registry.backup.BackupUtils.GcsMetadataKeys.LOWER_BOUND_CHECKPOINT;
@ -29,15 +30,14 @@ import static google.registry.util.DateTimeUtils.isAtOrAfter;
import static google.registry.util.FormattingLogger.getLoggerForCallerClass;
import static java.nio.channels.Channels.newOutputStream;
import static java.util.Arrays.asList;
import static java.util.Comparator.comparingLong;
import com.google.appengine.tools.cloudstorage.GcsFileOptions;
import com.google.appengine.tools.cloudstorage.GcsFilename;
import com.google.appengine.tools.cloudstorage.GcsService;
import com.google.common.base.Function;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import com.googlecode.objectify.Key;
import google.registry.config.RegistryConfig.Config;
import google.registry.model.ImmutableObject;
@ -52,7 +52,6 @@ import google.registry.util.FormattingLogger;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
@ -152,21 +151,17 @@ public final class ExportCommitLogDiffAction implements Runnable {
// transaction-consistent by virtue of our checkpoint strategy and our customized Ofy; see
// CommitLogCheckpointStrategy for the proof. We break ties by sorting on bucket ID to ensure
// a deterministic order.
return FluentIterable.from(upperCheckpoint.getBucketTimestamps().keySet())
.transformAndConcat(new Function<Integer, Iterable<Key<CommitLogManifest>>>() {
@Override
public Iterable<Key<CommitLogManifest>> apply(Integer bucketNum) {
return loadDiffKeysFromBucket(lowerCheckpoint, upperCheckpoint, bucketNum);
}})
.toSortedList(new Comparator<Key<CommitLogManifest>>() {
@Override
public int compare(Key<CommitLogManifest> a, Key<CommitLogManifest> b) {
// Compare keys by timestamp (which is encoded in the id as millis), then by bucket id.
return ComparisonChain.start()
.compare(a.getId(), b.getId())
.compare(a.getParent().getId(), b.getParent().getId())
.result();
}});
return upperCheckpoint
.getBucketTimestamps()
.keySet()
.stream()
.flatMap(
bucketNum ->
Streams.stream(loadDiffKeysFromBucket(lowerCheckpoint, upperCheckpoint, bucketNum)))
.sorted(
comparingLong(Key<CommitLogManifest>::getId)
.thenComparingLong(a -> a.getParent().getId()))
.collect(toImmutableList());
}
/**

View file

@ -37,7 +37,6 @@ import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.Callable;
import javax.annotation.Nullable;
import javax.inject.Inject;
import org.joda.time.DateTime;
@ -113,12 +112,7 @@ class GcsDiffFileLister {
final String filename = listItems.next().getName();
DateTime upperBoundTime = DateTime.parse(filename.substring(DIFF_FILE_PREFIX.length()));
if (isInRange(upperBoundTime, fromTime, toTime)) {
upperBoundTimesToMetadata.put(upperBoundTime, executor.submit(
new Callable<GcsFileMetadata>() {
@Override
public GcsFileMetadata call() throws Exception {
return getMetadata(filename);
}}));
upperBoundTimesToMetadata.put(upperBoundTime, executor.submit(() -> getMetadata(filename)));
lastUpperBoundTime = latestOf(upperBoundTime, lastUpperBoundTime);
}
}

View file

@ -52,7 +52,6 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.Callable;
import javax.inject.Inject;
import org.joda.time.DateTime;
@ -117,16 +116,16 @@ public class RestoreCommitLogsAction implements Runnable {
}
}
// Restore the CommitLogCheckpointRoot and CommitLogBuckets.
saveOfy(FluentIterable.from(bucketTimestamps.entrySet())
.transform(new Function<Entry<Integer, DateTime>, ImmutableObject> () {
@Override
public ImmutableObject apply(Entry<Integer, DateTime> entry) {
return new CommitLogBucket.Builder()
.setBucketNum(entry.getKey())
.setLastWrittenTime(entry.getValue())
.build();
}})
.append(CommitLogCheckpointRoot.create(lastCheckpoint.getCheckpointTime())));
saveOfy(
FluentIterable.from(bucketTimestamps.entrySet())
.transform(
(Function<Entry<Integer, DateTime>, ImmutableObject>)
entry ->
new CommitLogBucket.Builder()
.setBucketNum(entry.getKey())
.setLastWrittenTime(entry.getValue())
.build())
.append(CommitLogCheckpointRoot.create(lastCheckpoint.getCheckpointTime())));
}
/**
@ -153,11 +152,7 @@ public class RestoreCommitLogsAction implements Runnable {
try {
deleteResult.now();
} catch (Exception e) {
retry(new Runnable() {
@Override
public void run() {
deleteAsync(manifest.getDeletions()).now();
}});
retry(() -> deleteAsync(manifest.getDeletions()).now());
}
return manifest;
}
@ -167,11 +162,7 @@ public class RestoreCommitLogsAction implements Runnable {
logger.info("Would have saved " + entitiesToSave);
return;
}
retry(new Runnable() {
@Override
public void run() {
datastoreService.put(entitiesToSave);
}});
retry(() -> datastoreService.put(entitiesToSave));
}
private void saveOfy(final Iterable<? extends ImmutableObject> objectsToSave) {
@ -179,11 +170,7 @@ public class RestoreCommitLogsAction implements Runnable {
logger.info("Would have saved " + asList(objectsToSave));
return;
}
retry(new Runnable() {
@Override
public void run() {
ofy().saveWithoutBackup().entities(objectsToSave).now();
}});
retry(() -> ofy().saveWithoutBackup().entities(objectsToSave).now());
}
private Result<?> deleteAsync(Set<Key<?>> keysToDelete) {
@ -198,12 +185,10 @@ public class RestoreCommitLogsAction implements Runnable {
/** Retrier for saves and deletes, since we can't proceed with any failures. */
private void retry(final Runnable runnable) {
retrier.callWithRetry(
new Callable<Void>() {
@Override
public Void call() throws Exception {
runnable.run();
return null;
}},
() -> {
runnable.run();
return null;
},
RuntimeException.class);
}
}

View file

@ -18,6 +18,7 @@ import static com.google.appengine.api.taskqueue.QueueConstants.maxLeaseCount;
import static com.google.appengine.api.taskqueue.QueueFactory.getQueue;
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.math.IntMath.divide;
import static com.googlecode.objectify.Key.getKind;
import static google.registry.flows.ResourceFlowUtils.denyPendingTransfer;
@ -52,8 +53,6 @@ import com.google.appengine.tools.mapreduce.Mapper;
import com.google.appengine.tools.mapreduce.Reducer;
import com.google.appengine.tools.mapreduce.ReducerInput;
import com.google.auto.value.AutoValue;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@ -94,7 +93,6 @@ import google.registry.util.SystemClock;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import javax.inject.Inject;
import javax.inject.Named;
import org.joda.time.DateTime;
@ -180,22 +178,11 @@ public class DeleteContactsAndHostsAction implements Runnable {
return;
}
final List<TaskHandle> tasks =
FluentIterable.from(deletionRequests)
.transform(
new Function<DeletionRequest, TaskHandle>() {
@Override
public TaskHandle apply(DeletionRequest deletionRequest) {
return deletionRequest.task();
}
})
.toList();
deletionRequests.stream().map(DeletionRequest::task).collect(toImmutableList());
retrier.callWithRetry(
new Callable<Void>() {
@Override
public Void call() throws Exception {
queue.deleteTask(tasks);
return null;
}
() -> {
queue.deleteTask(tasks);
return null;
},
TransientFailureException.class);
for (DeletionRequest deletionRequest : deletionRequests) {

View file

@ -15,6 +15,7 @@
package google.registry.batch;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static google.registry.flows.ResourceFlowUtils.updateForeignKeyIndexDeletionTime;
import static google.registry.mapreduce.MapreduceRunner.PARAM_DRY_RUN;
import static google.registry.model.ofy.ObjectifyService.ofy;
@ -24,11 +25,8 @@ import static google.registry.request.Action.Method.POST;
import static org.joda.time.DateTimeZone.UTC;
import com.google.appengine.tools.mapreduce.Mapper;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
@ -94,21 +92,11 @@ public class DeleteProberDataAction implements Runnable {
}
private static ImmutableSet<String> getProberRoidSuffixes() {
return FluentIterable.from(getTldsOfType(TldType.TEST))
.filter(new Predicate<String>() {
@Override
public boolean apply(String tld) {
// Extra sanity check to prevent us from nuking prod data if a real TLD accidentally
// gets set to type TEST.
return tld.endsWith(".test");
}})
.transform(
new Function<String, String>() {
@Override
public String apply(String tld) {
return Registry.get(tld).getRoidSuffix();
}})
.toSet();
return getTldsOfType(TldType.TEST)
.stream()
.filter(tld -> tld.endsWith(".test"))
.map(tld -> Registry.get(tld).getRoidSuffix())
.collect(toImmutableSet());
}
/** Provides the map method that runs for each existing DomainBase entity. */

View file

@ -15,6 +15,7 @@
package google.registry.batch;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Sets.difference;
import static google.registry.mapreduce.MapreduceRunner.PARAM_DRY_RUN;
import static google.registry.mapreduce.inputs.EppResourceInputs.createChildEntityInput;
@ -32,13 +33,11 @@ import static google.registry.util.PipelineUtils.createJobPath;
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.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Range;
import com.google.common.collect.Streams;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.VoidWork;
import com.googlecode.objectify.Work;
@ -262,14 +261,10 @@ public class ExpandRecurringBillingEventsAction implements Runnable {
DateTime cursorTime,
DateTime executeTime,
final Registry tld) {
return FluentIterable.from(eventTimes)
.transform(new Function<DateTime, DateTime>() {
@Override
public DateTime apply(DateTime eventTime) {
return eventTime.plus(tld.getAutoRenewGracePeriodLength());
}})
return Streams.stream(eventTimes)
.map(eventTime -> eventTime.plus(tld.getAutoRenewGracePeriodLength()))
.filter(Range.closedOpen(cursorTime, executeTime))
.toSet();
.collect(toImmutableSet());
}
/**
@ -279,19 +274,13 @@ public class ExpandRecurringBillingEventsAction implements Runnable {
private ImmutableSet<DateTime> getExistingBillingTimes(
Iterable<BillingEvent.OneTime> oneTimesForDomain,
final BillingEvent.Recurring recurringEvent) {
return FluentIterable.from(oneTimesForDomain)
.filter(new Predicate<BillingEvent.OneTime>() {
@Override
public boolean apply(OneTime billingEvent) {
return Key.create(recurringEvent)
.equals(billingEvent.getCancellationMatchingBillingEvent());
}})
.transform(new Function<OneTime, DateTime>() {
@Override
public DateTime apply(OneTime billingEvent) {
return billingEvent.getBillingTime();
}})
.toSet();
return Streams.stream(oneTimesForDomain)
.filter(
billingEvent ->
Key.create(recurringEvent)
.equals(billingEvent.getCancellationMatchingBillingEvent()))
.map(OneTime::getBillingTime)
.collect(toImmutableSet());
}
}

View file

@ -17,6 +17,7 @@ package google.registry.batch;
import static com.google.appengine.api.taskqueue.QueueConstants.maxLeaseCount;
import static com.google.appengine.api.taskqueue.QueueFactory.getQueue;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static google.registry.flows.async.AsyncFlowEnqueuer.PARAM_HOST_KEY;
import static google.registry.flows.async.AsyncFlowEnqueuer.PARAM_REQUESTED_TIME;
import static google.registry.flows.async.AsyncFlowEnqueuer.QUEUE_ASYNC_HOST_RENAME;
@ -38,8 +39,6 @@ import com.google.appengine.tools.mapreduce.Mapper;
import com.google.appengine.tools.mapreduce.Reducer;
import com.google.appengine.tools.mapreduce.ReducerInput;
import com.google.auto.value.AutoValue;
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.googlecode.objectify.Key;
@ -61,7 +60,6 @@ import google.registry.util.SystemClock;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
@ -180,12 +178,11 @@ public class RefreshDnsOnHostRenameAction implements Runnable {
}
if (referencingHostKey != null) {
retrier.callWithRetry(
new Callable<Void>() {
@Override
public Void call() throws Exception {
dnsQueue.addDomainRefreshTask(domain.getFullyQualifiedDomainName());
return null;
}}, TransientFailureException.class);
() -> {
dnsQueue.addDomainRefreshTask(domain.getFullyQualifiedDomainName());
return null;
},
TransientFailureException.class);
logger.infofmt(
"Enqueued DNS refresh for domain %s referenced by host %s.",
domain.getFullyQualifiedDomainName(), referencingHostKey);
@ -244,22 +241,13 @@ public class RefreshDnsOnHostRenameAction implements Runnable {
return;
}
final List<TaskHandle> tasks =
FluentIterable.from(refreshRequests)
.transform(
new Function<DnsRefreshRequest, TaskHandle>() {
@Override
public TaskHandle apply(DnsRefreshRequest refreshRequest) {
return refreshRequest.task();
}
})
.toList();
refreshRequests.stream().map(DnsRefreshRequest::task).collect(toImmutableList());
retrier.callWithRetry(
new Callable<Void>() {
@Override
public Void call() throws Exception {
queue.deleteTask(tasks);
return null;
}}, TransientFailureException.class);
() -> {
queue.deleteTask(tasks);
return null;
},
TransientFailureException.class);
for (DnsRefreshRequest refreshRequest : refreshRequests) {
asyncFlowMetrics.recordAsyncFlowResult(DNS_REFRESH, result, refreshRequest.requestedTime());
}

View file

@ -15,6 +15,7 @@
package google.registry.batch;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.googlecode.objectify.Key.getKind;
import static google.registry.model.EppResourceUtils.isActive;
@ -36,7 +37,6 @@ import com.google.appengine.tools.mapreduce.ReducerInput;
import com.google.appengine.tools.mapreduce.inputs.DatastoreKeyInput;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.googlecode.objectify.Key;
@ -259,18 +259,24 @@ public class VerifyEntityIntegrityAction implements Runnable {
verifyExistence(key, domain.getTransferData().getServerApproveAutorenewEvent());
verifyExistence(key, domain.getTransferData().getServerApproveAutorenewPollMessage());
verifyExistence(key, domain.getTransferData().getServerApproveBillingEvent());
verifyExistence(key, FluentIterable
.from(domain.getTransferData().getServerApproveEntities())
.transform(
new Function<Key<? extends TransferServerApproveEntity>,
Key<TransferServerApproveEntity>>() {
@SuppressWarnings("unchecked")
@Override
public Key<TransferServerApproveEntity> apply(
Key<? extends TransferServerApproveEntity> key) {
return (Key<TransferServerApproveEntity>) key;
}})
.toSet());
verifyExistence(
key,
domain
.getTransferData()
.getServerApproveEntities()
.stream()
.map(
new Function<
Key<? extends TransferServerApproveEntity>,
Key<TransferServerApproveEntity>>() {
@SuppressWarnings("unchecked")
@Override
public Key<TransferServerApproveEntity> apply(
Key<? extends TransferServerApproveEntity> key) {
return (Key<TransferServerApproveEntity>) key;
}
})
.collect(toImmutableSet()));
verifyExistence(key, domain.getApplication());
verifyExistence(key, domain.getAutorenewBillingEvent());
for (GracePeriod gracePeriod : domain.getGracePeriods()) {

View file

@ -21,19 +21,16 @@ import static google.registry.batch.EntityIntegrityAlertsSchema.FIELD_SCANTIME;
import static google.registry.batch.EntityIntegrityAlertsSchema.FIELD_SOURCE;
import static google.registry.batch.EntityIntegrityAlertsSchema.FIELD_TARGET;
import static google.registry.batch.EntityIntegrityAlertsSchema.TABLE_ID;
import static java.util.stream.Collectors.joining;
import com.google.api.services.bigquery.Bigquery;
import com.google.api.services.bigquery.Bigquery.Tabledata.InsertAll;
import com.google.api.services.bigquery.model.TableDataInsertAllRequest;
import com.google.api.services.bigquery.model.TableDataInsertAllRequest.Rows;
import com.google.api.services.bigquery.model.TableDataInsertAllResponse;
import com.google.api.services.bigquery.model.TableDataInsertAllResponse.InsertErrors;
import com.google.auto.factory.AutoFactory;
import com.google.auto.factory.Provided;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Supplier;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import google.registry.bigquery.BigqueryFactory;
@ -183,30 +180,26 @@ public class VerifyEntityIntegrityStreamer {
new TableDataInsertAllRequest().setRows(rows));
Callable<Void> callable =
new Callable<Void>() {
@Override
public Void call() throws Exception {
TableDataInsertAllResponse response = request.execute();
// Turn errors on the response object into RuntimeExceptions that the retrier will
// retry.
if (response.getInsertErrors() != null && !response.getInsertErrors().isEmpty()) {
throw new RuntimeException(
FluentIterable.from(response.getInsertErrors())
.transform(
new Function<InsertErrors, String>() {
@Override
public String apply(InsertErrors error) {
try {
return error.toPrettyString();
} catch (IOException e) {
return error.toString();
}
}
})
.join(Joiner.on('\n')));
}
return null;
() -> {
TableDataInsertAllResponse response = request.execute();
// Turn errors on the response object into RuntimeExceptions that the retrier will
// retry.
if (response.getInsertErrors() != null && !response.getInsertErrors().isEmpty()) {
throw new RuntimeException(
response
.getInsertErrors()
.stream()
.map(
error -> {
try {
return error.toPrettyString();
} catch (IOException e) {
return error.toString();
}
})
.collect(joining("\n")));
}
return null;
};
retrier.callWithRetry(callable, RuntimeException.class);
} catch (IOException e) {

View file

@ -73,7 +73,6 @@ import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import javax.annotation.Nullable;
import org.joda.time.DateTime;
@ -693,20 +692,15 @@ public class BigqueryConnection implements AutoCloseable {
final Job job,
final T result,
@Nullable final AbstractInputStreamContent data) {
return service.submit(new Callable<T>() {
@Override
public T call() {
runJob(job, data);
return result;
}});
return service.submit(
() -> {
runJob(job, data);
return result;
});
}
private ListenableFuture<Job> runJobToCompletion(final Job job) {
return service.submit(new Callable<Job>() {
@Override
public Job call() {
return runJob(job, null);
}});
return service.submit(() -> runJob(job, null));
}
/** Helper that returns true if a dataset with this name exists. */

View file

@ -16,7 +16,6 @@ package google.registry.config;
import static com.google.common.base.Suppliers.memoize;
import static google.registry.config.ConfigUtils.makeUrl;
import static google.registry.config.YamlUtils.getConfigSettings;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import com.google.common.annotations.VisibleForTesting;
@ -1270,11 +1269,7 @@ public final class RegistryConfig {
* change the contents of the YAML config files.
*/
private static final Supplier<RegistryConfigSettings> CONFIG_SETTINGS =
memoize(new Supplier<RegistryConfigSettings>() {
@Override
public RegistryConfigSettings get() {
return getConfigSettings();
}});
memoize(YamlUtils::getConfigSettings);
private RegistryConfig() {}
}

View file

@ -19,7 +19,7 @@ import static com.google.appengine.api.taskqueue.TaskOptions.Builder.withUrl;
import static com.google.common.base.Predicates.in;
import static com.google.common.base.Predicates.not;
import static com.google.common.base.Strings.nullToEmpty;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Iterables.getFirst;
import static com.google.common.collect.Multimaps.filterKeys;
import static com.google.common.collect.Sets.difference;
@ -36,6 +36,7 @@ import com.google.common.base.Optional;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.google.common.collect.Streams;
import google.registry.request.Action;
import google.registry.request.Parameter;
import google.registry.request.ParameterMap;
@ -116,11 +117,13 @@ public final class TldFanoutAction implements Runnable {
public void run() {
Set<String> tlds =
difference(
ImmutableSet.copyOf(
concat(
runInEmpty ? ImmutableSet.of("") : ImmutableSet.<String>of(),
forEachRealTld ? getTldsOfType(REAL) : ImmutableSet.<String>of(),
forEachTestTld ? getTldsOfType(TEST) : ImmutableSet.<String>of())),
Streams.concat(
Streams.stream(runInEmpty ? ImmutableSet.of("") : ImmutableSet.<String>of()),
Streams.stream(
forEachRealTld ? getTldsOfType(REAL) : ImmutableSet.<String>of()),
Streams.stream(
forEachTestTld ? getTldsOfType(TEST) : ImmutableSet.<String>of()))
.collect(toImmutableSet()),
excludes);
Multimap<String, String> flowThruParams = filterKeys(params, not(in(CONTROL_PARAMS)));
Queue taskQueue = getQueue(queue);

View file

@ -284,39 +284,35 @@ public class CloudDnsWriter extends BaseDnsWriter {
@VisibleForTesting
Callable<Void> getMutateZoneCallback(
final ImmutableMap<String, ImmutableSet<ResourceRecordSet>> desiredRecords) {
return new Callable<Void>() {
@Override
public Void call() throws IOException, ZoneStateException {
// Fetch all existing records for names that this writer is trying to modify
Builder<ResourceRecordSet> existingRecords = new Builder<>();
for (String domainName : desiredRecords.keySet()) {
List<ResourceRecordSet> existingRecordsForDomain =
getResourceRecordsForDomain(domainName);
existingRecords.addAll(existingRecordsForDomain);
return () -> {
// Fetch all existing records for names that this writer is trying to modify
Builder<ResourceRecordSet> existingRecords = new Builder<>();
for (String domainName : desiredRecords.keySet()) {
List<ResourceRecordSet> existingRecordsForDomain = getResourceRecordsForDomain(domainName);
existingRecords.addAll(existingRecordsForDomain);
// Fetch glue records for in-bailiwick nameservers
for (ResourceRecordSet record : existingRecordsForDomain) {
if (!record.getType().equals("NS")) {
continue;
}
for (String hostName : record.getRrdatas()) {
if (hostName.endsWith(domainName) && !hostName.equals(domainName)) {
existingRecords.addAll(getResourceRecordsForDomain(hostName));
}
// Fetch glue records for in-bailiwick nameservers
for (ResourceRecordSet record : existingRecordsForDomain) {
if (!record.getType().equals("NS")) {
continue;
}
for (String hostName : record.getRrdatas()) {
if (hostName.endsWith(domainName) && !hostName.equals(domainName)) {
existingRecords.addAll(getResourceRecordsForDomain(hostName));
}
}
}
// Flatten the desired records into one set.
Builder<ResourceRecordSet> flattenedDesiredRecords = new Builder<>();
for (ImmutableSet<ResourceRecordSet> records : desiredRecords.values()) {
flattenedDesiredRecords.addAll(records);
}
// Delete all existing records and add back the desired records
updateResourceRecords(flattenedDesiredRecords.build(), existingRecords.build());
return null;
}
// Flatten the desired records into one set.
Builder<ResourceRecordSet> flattenedDesiredRecords = new Builder<>();
for (ImmutableSet<ResourceRecordSet> records : desiredRecords.values()) {
flattenedDesiredRecords.addAll(records);
}
// Delete all existing records and add back the desired records
updateResourceRecords(flattenedDesiredRecords.build(), existingRecords.build());
return null;
};
}

View file

@ -18,15 +18,12 @@ import static com.google.appengine.api.datastore.DatastoreServiceFactory.getData
import static com.google.appengine.api.taskqueue.QueueFactory.getQueue;
import static com.google.common.base.Strings.nullToEmpty;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.modules.ModulesService;
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.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
@ -91,18 +88,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())
.filter(new Predicate<Entity>() {
@Override
public boolean apply(Entity entity) {
return nullToEmpty((String) entity.getProperty("name")).startsWith(namePrefix);
}})
.transform(new Function<Entity, DatastoreBackupInfo>() {
@Override
public DatastoreBackupInfo apply(Entity entity) {
return new DatastoreBackupInfo(entity);
}});
return FluentIterable.from(
getDatastoreService().prepare(new Query(BACKUP_INFO_KIND)).asIterable())
.filter(entity -> nullToEmpty((String) entity.getProperty("name")).startsWith(namePrefix))
.transform(DatastoreBackupInfo::new);
}
/**

View file

@ -14,6 +14,8 @@
package google.registry.export;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.request.Action.Method.POST;
import static google.registry.util.CollectionUtils.nullToEmpty;
@ -21,13 +23,11 @@ import static google.registry.util.RegistrarUtils.normalizeClientId;
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import com.google.common.collect.Streams;
import com.googlecode.objectify.VoidWork;
import google.registry.config.RegistryConfig.Config;
import google.registry.groups.GroupsConnection;
@ -44,7 +44,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import javax.annotation.Nullable;
import javax.inject.Inject;
@ -118,16 +117,14 @@ public final class SyncGroupMembersAction implements Runnable {
*/
@Override
public void run() {
List<Registrar> dirtyRegistrars = FluentIterable.from(Registrar.loadAllCached())
.filter(new Predicate<Registrar>() {
@Override
public boolean apply(Registrar registrar) {
// Only grab active registrars that require syncing and are of the correct type.
return registrar.isLive()
&& registrar.getContactsRequireSyncing()
&& registrar.getType() == Registrar.Type.REAL;
}})
.toList();
List<Registrar> dirtyRegistrars =
Streams.stream(Registrar.loadAllCached())
.filter(
registrar ->
registrar.isLive()
&& registrar.getContactsRequireSyncing()
&& registrar.getType() == Registrar.Type.REAL)
.collect(toImmutableList());
if (dirtyRegistrars.isEmpty()) {
sendResponse(Result.NOT_MODIFIED, null);
return;
@ -137,12 +134,12 @@ public final class SyncGroupMembersAction implements Runnable {
new ImmutableMap.Builder<>();
for (final Registrar registrar : dirtyRegistrars) {
try {
retrier.callWithRetry(new Callable<Void>() {
@Override
public Void call() throws Exception {
syncRegistrarContacts(registrar);
return null;
}}, RuntimeException.class);
retrier.callWithRetry(
() -> {
syncRegistrarContacts(registrar);
return null;
},
RuntimeException.class);
resultsBuilder.put(registrar, Optional.<Throwable>absent());
} catch (Throwable e) {
logger.severe(e, e.getMessage());
@ -193,18 +190,12 @@ public final class SyncGroupMembersAction implements Runnable {
groupKey = getGroupEmailAddressForContactType(
registrar.getClientId(), type, gSuiteDomainName);
Set<String> currentMembers = groupsConnection.getMembersOfGroup(groupKey);
Set<String> desiredMembers = FluentIterable.from(registrarContacts)
.filter(new Predicate<RegistrarContact>() {
@Override
public boolean apply(RegistrarContact contact) {
return contact.getTypes().contains(type);
}})
.transform(new Function<RegistrarContact, String>() {
@Override
public String apply(RegistrarContact contact) {
return contact.getEmailAddress();
}})
.toSet();
Set<String> desiredMembers =
registrarContacts
.stream()
.filter(contact -> contact.getTypes().contains(type))
.map(RegistrarContact::getEmailAddress)
.collect(toImmutableSet());
for (String email : Sets.difference(desiredMembers, currentMembers)) {
groupsConnection.addMemberToGroup(groupKey, email, Role.MEMBER);
totalAdded++;

View file

@ -15,6 +15,7 @@
package google.registry.export.sheet;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static google.registry.model.common.Cursor.CursorType.SYNC_REGISTRAR_SHEET;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.model.registrar.RegistrarContact.Type.ABUSE;
@ -26,10 +27,8 @@ import static google.registry.model.registrar.RegistrarContact.Type.TECH;
import static google.registry.model.registrar.RegistrarContact.Type.WHOIS;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedSet;
@ -77,104 +76,83 @@ class SyncRegistrarsSheet {
final DateTime executionTime = clock.nowUtc();
sheetSynchronizer.synchronize(
spreadsheetId,
FluentIterable.from(
new Ordering<Registrar>() {
@Override
public int compare(Registrar left, Registrar right) {
return left.getClientId().compareTo(right.getClientId());
}
}.immutableSortedCopy(Registrar.loadAllCached()))
new Ordering<Registrar>() {
@Override
public int compare(Registrar left, Registrar right) {
return left.getClientId().compareTo(right.getClientId());
}
}.immutableSortedCopy(Registrar.loadAllCached())
.stream()
.filter(
new Predicate<Registrar>() {
@Override
public boolean apply(Registrar registrar) {
return registrar.getType() == Registrar.Type.REAL
|| registrar.getType() == Registrar.Type.OTE;
}
registrar ->
registrar.getType() == Registrar.Type.REAL
|| registrar.getType() == Registrar.Type.OTE)
.map(
registrar -> {
ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<>();
ImmutableSortedSet<RegistrarContact> contacts = registrar.getContacts();
RegistrarAddress address =
firstNonNull(
registrar.getLocalizedAddress(),
firstNonNull(
registrar.getInternationalizedAddress(),
new RegistrarAddress.Builder()
.setStreet(ImmutableList.of("UNKNOWN"))
.setCity("UNKNOWN")
.setCountryCode("US")
.build()));
//
// °° WARNING WARNING WARNING
//
// Do not change these mappings simply because the Registrar model changed. Only
// change these mappings if the people who use the spreadsheet requested it be
// changed.
//
// These values are hard-coded because they correspond to actual spreadsheet
// columns. If you change this dictionary, then you'll need to manually add new
// columns to the registrar spreadsheets for all environments before deployment,
// and you'll need to remove deleted columns probably like a week after
// deployment.
//
builder.put("clientIdentifier", convert(registrar.getClientId()));
builder.put("registrarName", convert(registrar.getRegistrarName()));
builder.put("state", convert(registrar.getState()));
builder.put("ianaIdentifier", convert(registrar.getIanaIdentifier()));
builder.put("billingIdentifier", convert(registrar.getBillingIdentifier()));
builder.put("billingAccountMap", convert(registrar.getBillingAccountMap()));
builder.put("primaryContacts", convertContacts(contacts, byType(ADMIN)));
builder.put("techContacts", convertContacts(contacts, byType(TECH)));
builder.put("marketingContacts", convertContacts(contacts, byType(MARKETING)));
builder.put("abuseContacts", convertContacts(contacts, byType(ABUSE)));
builder.put("whoisInquiryContacts", convertContacts(contacts, byType(WHOIS)));
builder.put("legalContacts", convertContacts(contacts, byType(LEGAL)));
builder.put("billingContacts", convertContacts(contacts, byType(BILLING)));
builder.put(
"contactsMarkedAsWhoisAdmin",
convertContacts(contacts, RegistrarContact::getVisibleInWhoisAsAdmin));
builder.put(
"contactsMarkedAsWhoisTech",
convertContacts(contacts, RegistrarContact::getVisibleInWhoisAsTech));
builder.put("emailAddress", convert(registrar.getEmailAddress()));
builder.put("address.street", convert(address.getStreet()));
builder.put("address.city", convert(address.getCity()));
builder.put("address.state", convert(address.getState()));
builder.put("address.zip", convert(address.getZip()));
builder.put("address.countryCode", convert(address.getCountryCode()));
builder.put("phoneNumber", convert(registrar.getPhoneNumber()));
builder.put("faxNumber", convert(registrar.getFaxNumber()));
builder.put("creationTime", convert(registrar.getCreationTime()));
builder.put("lastUpdateTime", convert(registrar.getLastUpdateTime()));
builder.put("allowedTlds", convert(registrar.getAllowedTlds()));
builder.put("whoisServer", convert(registrar.getWhoisServer()));
builder.put("blockPremiumNames", convert(registrar.getBlockPremiumNames()));
builder.put("ipAddressWhitelist", convert(registrar.getIpAddressWhitelist()));
builder.put("url", convert(registrar.getUrl()));
builder.put("referralUrl", convert(registrar.getReferralUrl()));
builder.put("icannReferralEmail", convert(registrar.getIcannReferralEmail()));
return builder.build();
})
.transform(
new Function<Registrar, ImmutableMap<String, String>>() {
@Override
public ImmutableMap<String, String> apply(Registrar registrar) {
ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<>();
ImmutableSortedSet<RegistrarContact> contacts = registrar.getContacts();
RegistrarAddress address =
firstNonNull(
registrar.getLocalizedAddress(),
firstNonNull(
registrar.getInternationalizedAddress(),
new RegistrarAddress.Builder()
.setStreet(ImmutableList.of("UNKNOWN"))
.setCity("UNKNOWN")
.setCountryCode("US")
.build()));
//
// °° WARNING WARNING WARNING
//
// Do not change these mappings simply because the Registrar model changed. Only
// change these mappings if the people who use the spreadsheet requested it be
// changed.
//
// These values are hard-coded because they correspond to actual spreadsheet
// columns. If you change this dictionary, then you'll need to manually add new
// columns to the registrar spreadsheets for all environments before deployment,
// and you'll need to remove deleted columns probably like a week after
// deployment.
//
builder.put("clientIdentifier", convert(registrar.getClientId()));
builder.put("registrarName", convert(registrar.getRegistrarName()));
builder.put("state", convert(registrar.getState()));
builder.put("ianaIdentifier", convert(registrar.getIanaIdentifier()));
builder.put("billingIdentifier", convert(registrar.getBillingIdentifier()));
builder.put("billingAccountMap", convert(registrar.getBillingAccountMap()));
builder.put("primaryContacts", convertContacts(contacts, byType(ADMIN)));
builder.put("techContacts", convertContacts(contacts, byType(TECH)));
builder.put("marketingContacts", convertContacts(contacts, byType(MARKETING)));
builder.put("abuseContacts", convertContacts(contacts, byType(ABUSE)));
builder.put("whoisInquiryContacts", convertContacts(contacts, byType(WHOIS)));
builder.put("legalContacts", convertContacts(contacts, byType(LEGAL)));
builder.put("billingContacts", convertContacts(contacts, byType(BILLING)));
builder.put(
"contactsMarkedAsWhoisAdmin",
convertContacts(
contacts,
new Predicate<RegistrarContact>() {
@Override
public boolean apply(RegistrarContact contact) {
return contact.getVisibleInWhoisAsAdmin();
}
}));
builder.put(
"contactsMarkedAsWhoisTech",
convertContacts(
contacts,
new Predicate<RegistrarContact>() {
@Override
public boolean apply(RegistrarContact contact) {
return contact.getVisibleInWhoisAsTech();
}
}));
builder.put("emailAddress", convert(registrar.getEmailAddress()));
builder.put("address.street", convert(address.getStreet()));
builder.put("address.city", convert(address.getCity()));
builder.put("address.state", convert(address.getState()));
builder.put("address.zip", convert(address.getZip()));
builder.put("address.countryCode", convert(address.getCountryCode()));
builder.put("phoneNumber", convert(registrar.getPhoneNumber()));
builder.put("faxNumber", convert(registrar.getFaxNumber()));
builder.put("creationTime", convert(registrar.getCreationTime()));
builder.put("lastUpdateTime", convert(registrar.getLastUpdateTime()));
builder.put("allowedTlds", convert(registrar.getAllowedTlds()));
builder.put("whoisServer", convert(registrar.getWhoisServer()));
builder.put("blockPremiumNames", convert(registrar.getBlockPremiumNames()));
builder.put("ipAddressWhitelist", convert(registrar.getIpAddressWhitelist()));
builder.put("url", convert(registrar.getUrl()));
builder.put("referralUrl", convert(registrar.getReferralUrl()));
builder.put("icannReferralEmail", convert(registrar.getIcannReferralEmail()));
return builder.build();
}
})
.toList());
.collect(toImmutableList()));
ofy().transact(new VoidWork() {
@Override
public void vrun() {
@ -201,11 +179,7 @@ class SyncRegistrarsSheet {
}
private static Predicate<RegistrarContact> byType(final RegistrarContact.Type type) {
return new Predicate<RegistrarContact>() {
@Override
public boolean apply(RegistrarContact contact) {
return contact.getTypes().contains(type);
}};
return contact -> contact.getTypes().contains(type);
}
/** Converts a value to a string representation that can be stored in a spreadsheet cell. */

View file

@ -134,19 +134,16 @@ public class SyncRegistrarsSheetAction implements Runnable {
}
String sheetLockName = String.format("%s: %s", LOCK_NAME, sheetId.get());
Callable<Void> runner = new Callable<Void>() {
@Nullable
@Override
public Void call() throws IOException {
try {
syncRegistrarsSheet.run(sheetId.get());
Result.OK.send(response, null);
} catch (IOException e) {
Result.FAILED.send(response, e);
}
return null;
}
};
Callable<Void> runner =
() -> {
try {
syncRegistrarsSheet.run(sheetId.get());
Result.OK.send(response, null);
} catch (IOException e) {
Result.FAILED.send(response, e);
}
return null;
};
if (!lockHandler.executeWithLocks(runner, null, timeout, sheetLockName)) {
// If we fail to acquire the lock, it probably means lots of updates are happening at once, in
// which case it should be safe to not bother. The task queue definition should *not* specify

View file

@ -14,15 +14,12 @@
package google.registry.flows;
import static com.google.common.collect.Iterables.any;
import static com.google.common.collect.Sets.difference;
import static com.google.common.collect.Sets.intersection;
import static google.registry.model.domain.fee.Fee.FEE_EXTENSION_URIS;
import static google.registry.model.eppcommon.ProtocolDefinition.ServiceExtension.getCommandExtensionUri;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import google.registry.flows.EppException.CommandUseErrorException;
@ -130,7 +127,13 @@ public final class ExtensionManager {
ImmutableSet<Class<? extends CommandExtension>> implementedExtensions)
throws UnsupportedRepeatedExtensionException {
for (Class<? extends CommandExtension> implemented : implementedExtensions) {
if (FluentIterable.from(suppliedExtensionInstances).filter(implemented).size() > 1) {
if ((int)
suppliedExtensionInstances
.stream()
.filter(implemented::isInstance)
.map(implemented::cast)
.count()
> 1) {
throw new UnsupportedRepeatedExtensionException();
}
}
@ -143,13 +146,9 @@ public final class ExtensionManager {
ImmutableSet.Builder<Class<? extends CommandExtension>> unimplementedExtensionsBuilder =
new ImmutableSet.Builder<>();
for (final CommandExtension instance : suppliedExtensionInstances) {
if (!any(
implementedExtensionClasses,
new Predicate<Class<? extends CommandExtension>>() {
@Override
public boolean apply(Class<? extends CommandExtension> implementedExtensionClass) {
return implementedExtensionClass.isInstance(instance);
}})) {
if (implementedExtensionClasses
.stream()
.noneMatch(implementedExtensionClass -> implementedExtensionClass.isInstance(instance))) {
unimplementedExtensionsBuilder.add(instance.getClass());
}
}

View file

@ -30,7 +30,6 @@ import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Work;
@ -178,29 +177,35 @@ public final class ResourceFlowUtils {
final Class<R> resourceClass,
final Function<DomainBase, ImmutableSet<?>> getPotentialReferences) throws EppException {
// Enter a transactionless context briefly.
EppException failfastException = ofy().doTransactionless(new Work<EppException>() {
@Override
public EppException run() {
final ForeignKeyIndex<R> fki = ForeignKeyIndex.load(resourceClass, targetId, now);
if (fki == null) {
return new ResourceDoesNotExistException(resourceClass, targetId);
}
// Query for the first few linked domains, and if found, actually load them. The query is
// eventually consistent and so might be very stale, but the direct load will not be stale,
// just non-transactional. If we find at least one actual reference then we can reliably
// fail. If we don't find any, we can't trust the query and need to do the full mapreduce.
Iterable<Key<DomainBase>> keys =
queryForLinkedDomains(fki.getResourceKey(), now).limit(FAILFAST_CHECK_COUNT).keys();
Predicate<DomainBase> predicate = new Predicate<DomainBase>() {
@Override
public boolean apply(DomainBase domain) {
return getPotentialReferences.apply(domain).contains(fki.getResourceKey());
}};
return Iterables.any(ofy().load().keys(keys).values(), predicate)
? new ResourceToDeleteIsReferencedException()
: null;
}
});
EppException failfastException =
ofy()
.doTransactionless(
new Work<EppException>() {
@Override
public EppException run() {
final ForeignKeyIndex<R> fki =
ForeignKeyIndex.load(resourceClass, targetId, now);
if (fki == null) {
return new ResourceDoesNotExistException(resourceClass, targetId);
}
/* Query for the first few linked domains, and if found, actually load them. The
* query is eventually consistent and so might be very stale, but the direct
* load will not be stale, just non-transactional. If we find at least one
* actual reference then we can reliably fail. If we don't find any, we can't
* trust the query and need to do the full mapreduce.
*/
Iterable<Key<DomainBase>> keys =
queryForLinkedDomains(fki.getResourceKey(), now)
.limit(FAILFAST_CHECK_COUNT)
.keys();
Predicate<DomainBase> predicate =
domain ->
getPotentialReferences.apply(domain).contains(fki.getResourceKey());
return ofy().load().keys(keys).values().stream().anyMatch(predicate)
? new ResourceToDeleteIsReferencedException()
: null;
}
});
if (failfastException != null) {
throw failfastException;
}
@ -339,13 +344,8 @@ public final class ResourceFlowUtils {
return;
}
// The roid should match one of the contacts.
Optional<Key<ContactResource>> foundContact = tryFind(
domain.getReferencedContacts(),
new Predicate<Key<ContactResource>>() {
@Override
public boolean apply(Key<ContactResource> key) {
return key.getName().equals(authRepoId);
}});
Optional<Key<ContactResource>> foundContact =
tryFind(domain.getReferencedContacts(), key -> key.getName().equals(authRepoId));
if (!foundContact.isPresent()) {
throw new BadAuthInfoForResourceException();
}

View file

@ -26,7 +26,6 @@ import google.registry.model.eppcommon.Trid;
import google.registry.model.host.HostResource;
import google.registry.util.FormattingLogger;
import google.registry.util.Retrier;
import java.util.concurrent.Callable;
import javax.inject.Inject;
import javax.inject.Named;
import org.joda.time.DateTime;
@ -107,11 +106,11 @@ public final class AsyncFlowEnqueuer {
* enqueuing a task.
*/
private void addTaskToQueueWithRetry(final Queue queue, final TaskOptions task) {
retrier.callWithRetry(new Callable<Void>() {
@Override
public Void call() throws Exception {
queue.add(task);
return null;
}}, TransientFailureException.class);
retrier.callWithRetry(
() -> {
queue.add(task);
return null;
},
TransientFailureException.class);
}
}

View file

@ -23,7 +23,6 @@ import static google.registry.flows.ResourceFlowUtils.verifyResourceOwnership;
import static google.registry.model.eppoutput.Result.Code.SUCCESS_WITH_ACTION_PENDING;
import static google.registry.model.ofy.ObjectifyService.ofy;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key;
@ -69,13 +68,6 @@ public final class ContactDeleteFlow implements TransactionalFlow {
StatusValue.PENDING_DELETE,
StatusValue.SERVER_DELETE_PROHIBITED);
private static final Function<DomainBase, ImmutableSet<?>> GET_REFERENCED_CONTACTS =
new Function<DomainBase, ImmutableSet<?>>() {
@Override
public ImmutableSet<?> apply(DomainBase domain) {
return domain.getReferencedContacts();
}};
@Inject ExtensionManager extensionManager;
@Inject @ClientId String clientId;
@Inject @TargetId String targetId;
@ -93,7 +85,7 @@ public final class ContactDeleteFlow implements TransactionalFlow {
extensionManager.validate();
validateClientIsLoggedIn(clientId);
DateTime now = ofy().getTransactionTime();
failfastForAsyncDelete(targetId, now, ContactResource.class, GET_REFERENCED_CONTACTS);
failfastForAsyncDelete(targetId, now, ContactResource.class, DomainBase::getReferencedContacts);
ContactResource existingContact = loadAndVerifyExistence(ContactResource.class, targetId, now);
verifyNoDisallowedStatuses(existingContact, DISALLOWED_STATUSES);
verifyOptionalAuthInfo(authInfo, existingContact);

View file

@ -14,8 +14,7 @@
package google.registry.flows.domain;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.collect.MoreCollectors.onlyElement;
import static google.registry.flows.FlowUtils.validateClientIsLoggedIn;
import static google.registry.flows.ResourceFlowUtils.verifyResourceDoesNotExist;
import static google.registry.flows.domain.DomainFlowUtils.cloneAndLinkReferences;
@ -39,6 +38,7 @@ import static google.registry.util.DateTimeUtils.leapSafeAddYears;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import com.google.common.net.InternetDomainName;
import com.googlecode.objectify.Key;
import dagger.Lazy;
@ -212,7 +212,10 @@ public class DomainAllocateFlow implements TransactionalFlow {
private <T extends ImmutableObject> T getOnly(
Iterable<? extends ImmutableObject> objects, Class<T> clazz) {
return getOnlyElement(filter(objects, clazz));
return Streams.stream(objects)
.filter(clazz::isInstance)
.map(clazz::cast)
.collect(onlyElement());
}
private void verifyIsSuperuser() throws OnlySuperuserCanAllocateException {

View file

@ -14,6 +14,7 @@
package google.registry.flows.domain;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Iterables.getOnlyElement;
import static google.registry.flows.FlowUtils.persistEntityChanges;
import static google.registry.flows.FlowUtils.validateClientIsLoggedIn;
@ -43,8 +44,6 @@ import static google.registry.model.index.DomainApplicationIndex.loadActiveAppli
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.model.registry.label.ReservedList.matchesAnchorTenantReservation;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.net.InternetDomainName;
@ -94,7 +93,6 @@ import google.registry.model.registry.Registry;
import google.registry.model.registry.Registry.TldState;
import google.registry.model.reporting.HistoryEntry;
import google.registry.model.reporting.IcannReportingTypes.ActivityReportField;
import google.registry.model.smd.AbstractSignedMark;
import google.registry.model.smd.EncodedSignedMark;
import javax.inject.Inject;
import org.joda.time.DateTime;
@ -244,32 +242,31 @@ public final class DomainApplicationCreateFlow implements TransactionalFlow {
.setDomainName(domainName)
.setYears(years)
.build());
DomainApplication newApplication = new DomainApplication.Builder()
.setCreationTrid(trid)
.setCreationClientId(clientId)
.setPersistedCurrentSponsorClientId(clientId)
.setRepoId(createDomainRepoId(ObjectifyService.allocateId(), tld))
.setLaunchNotice(launchCreate == null ? null : launchCreate.getNotice())
.setIdnTableName(idnTableName)
.setPhase(launchCreate.getPhase())
.setPeriod(command.getPeriod())
.setApplicationStatus(ApplicationStatus.VALIDATED)
.addStatusValue(StatusValue.PENDING_CREATE)
.setDsData(secDnsCreate == null ? null : secDnsCreate.getDsData())
.setRegistrant(command.getRegistrant())
.setAuthInfo(command.getAuthInfo())
.setFullyQualifiedDomainName(targetId)
.setNameservers(command.getNameservers())
.setContacts(command.getContacts())
.setEncodedSignedMarks(FluentIterable
.from(launchCreate.getSignedMarks())
.transform(new Function<AbstractSignedMark, EncodedSignedMark>() {
@Override
public EncodedSignedMark apply(AbstractSignedMark abstractSignedMark) {
return (EncodedSignedMark) abstractSignedMark;
}})
.toList())
.build();
DomainApplication newApplication =
new DomainApplication.Builder()
.setCreationTrid(trid)
.setCreationClientId(clientId)
.setPersistedCurrentSponsorClientId(clientId)
.setRepoId(createDomainRepoId(ObjectifyService.allocateId(), tld))
.setLaunchNotice(launchCreate == null ? null : launchCreate.getNotice())
.setIdnTableName(idnTableName)
.setPhase(launchCreate.getPhase())
.setPeriod(command.getPeriod())
.setApplicationStatus(ApplicationStatus.VALIDATED)
.addStatusValue(StatusValue.PENDING_CREATE)
.setDsData(secDnsCreate == null ? null : secDnsCreate.getDsData())
.setRegistrant(command.getRegistrant())
.setAuthInfo(command.getAuthInfo())
.setFullyQualifiedDomainName(targetId)
.setNameservers(command.getNameservers())
.setContacts(command.getContacts())
.setEncodedSignedMarks(
launchCreate
.getSignedMarks()
.stream()
.map(abstractSignedMark -> (EncodedSignedMark) abstractSignedMark)
.collect(toImmutableList()))
.build();
HistoryEntry historyEntry =
buildHistoryEntry(newApplication.getRepoId(), command.getPeriod(), now);
ImmutableSet.Builder<ImmutableObject> entitiesToSave = new ImmutableSet.Builder<>();

View file

@ -28,8 +28,6 @@ import static google.registry.model.registry.label.ReservationType.getTypeOfHigh
import static google.registry.pricing.PricingEngineProxy.isDomainPremium;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
@ -46,7 +44,6 @@ import google.registry.flows.annotations.ReportingSpec;
import google.registry.flows.custom.DomainCheckFlowCustomLogic;
import google.registry.flows.custom.DomainCheckFlowCustomLogic.BeforeResponseParameters;
import google.registry.flows.custom.DomainCheckFlowCustomLogic.BeforeResponseReturnData;
import google.registry.model.domain.DomainApplication;
import google.registry.model.domain.DomainCommand.Check;
import google.registry.model.domain.DomainResource;
import google.registry.model.domain.fee.FeeCheckCommandExtension;
@ -176,12 +173,9 @@ public final class DomainCheckFlow implements Flow {
}
Registry registry = Registry.get(domainName.parent().toString());
if (PENDING_ALLOCATION_TLD_STATES.contains(registry.getTldState(now))
&& FluentIterable.from(loadActiveApplicationsByDomainName(domainName.toString(), now))
.anyMatch(new Predicate<DomainApplication>() {
@Override
public boolean apply(DomainApplication input) {
return !input.getApplicationStatus().isFinalStatus();
}})) {
&& loadActiveApplicationsByDomainName(domainName.toString(), now)
.stream()
.anyMatch(input -> !input.getApplicationStatus().isFinalStatus())) {
return Optional.of("Pending allocation");
}
ImmutableSet<ReservationType> reservationTypes = getReservationTypes(domainName);

View file

@ -41,13 +41,12 @@ import static google.registry.util.DomainNameUtils.ACE_PREFIX;
import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
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.Sets;
import com.google.common.collect.Streams;
import com.google.common.net.InternetDomainName;
import com.googlecode.objectify.Key;
import google.registry.flows.EppException;
@ -940,24 +939,22 @@ public class DomainFlowUtils {
.order("modificationTime")
.list();
Optional<HistoryEntry> entryToCancel =
FluentIterable.from(recentHistoryEntries)
.filter(
new Predicate<HistoryEntry>() {
@Override
public boolean apply(HistoryEntry historyEntry) {
// Look for add and renew transaction records that have yet to be reported
for (DomainTransactionRecord record :
historyEntry.getDomainTransactionRecords()) {
if (cancelableFields.contains(record.getReportField())
&& record.getReportingTime().isAfter(now)) {
return true;
}
}
return false;
}
})
// We only want to cancel out the most recent add or renewal
.last();
Optional.fromJavaUtil(
Streams.findLast(
recentHistoryEntries
.stream()
.filter(
historyEntry -> {
// Look for add and renew transaction records that have yet to be reported
for (DomainTransactionRecord record :
historyEntry.getDomainTransactionRecords()) {
if (cancelableFields.contains(record.getReportField())
&& record.getReportingTime().isAfter(now)) {
return true;
}
}
return false;
})));
ImmutableSet.Builder<DomainTransactionRecord> recordsBuilder = new ImmutableSet.Builder<>();
if (entryToCancel.isPresent()) {
for (DomainTransactionRecord record : entryToCancel.get().getDomainTransactionRecords()) {

View file

@ -14,8 +14,8 @@
package google.registry.flows.domain;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.collect.MoreCollectors.onlyElement;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
import com.google.common.base.Optional;
@ -61,14 +61,29 @@ public final class DomainTransferUtils {
if (transferPeriod.getValue() != 0) {
// Unless superuser sets period to 0, add a transfer billing event.
transferDataBuilder.setServerApproveBillingEvent(
Key.create(getOnlyElement(filter(serverApproveEntities, BillingEvent.OneTime.class))));
Key.create(
serverApproveEntities
.stream()
.filter(BillingEvent.OneTime.class::isInstance)
.map(BillingEvent.OneTime.class::cast)
.collect(onlyElement())));
}
return transferDataBuilder
.setTransferStatus(TransferStatus.PENDING)
.setServerApproveAutorenewEvent(Key.create(
getOnlyElement(filter(serverApproveEntities, BillingEvent.Recurring.class))))
.setServerApproveAutorenewPollMessage(Key.create(
getOnlyElement(filter(serverApproveEntities, PollMessage.Autorenew.class))))
.setServerApproveAutorenewEvent(
Key.create(
serverApproveEntities
.stream()
.filter(BillingEvent.Recurring.class::isInstance)
.map(BillingEvent.Recurring.class::cast)
.collect(onlyElement())))
.setServerApproveAutorenewPollMessage(
Key.create(
serverApproveEntities
.stream()
.filter(PollMessage.Autorenew.class::isInstance)
.map(PollMessage.Autorenew.class::cast)
.collect(onlyElement())))
.setServerApproveEntities(serverApproveEntityKeys.build())
.setTransferPeriod(transferPeriod)
.build();

View file

@ -14,7 +14,7 @@
package google.registry.flows.domain;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static google.registry.util.CollectionUtils.nullToEmpty;
import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
@ -26,6 +26,7 @@ import google.registry.model.domain.fee.BaseFee;
import google.registry.model.domain.fee.BaseFee.FeeType;
import google.registry.model.domain.fee.Credit;
import google.registry.model.domain.fee.Fee;
import java.util.stream.Stream;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
@ -102,7 +103,7 @@ public class FeesAndCredits extends ImmutableObject implements Buildable {
/** Returns all fees and credits for the event. */
public ImmutableList<BaseFee> getFeesAndCredits() {
return ImmutableList.copyOf(concat(getFees(), getCredits()));
return Stream.concat(getFees().stream(), getCredits().stream()).collect(toImmutableList());
}
@Override

View file

@ -23,7 +23,6 @@ import static google.registry.flows.host.HostFlowUtils.validateHostName;
import static google.registry.model.eppoutput.Result.Code.SUCCESS_WITH_ACTION_PENDING;
import static google.registry.model.ofy.ObjectifyService.ofy;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key;
import google.registry.flows.EppException;
@ -71,13 +70,6 @@ public final class HostDeleteFlow implements TransactionalFlow {
StatusValue.PENDING_DELETE,
StatusValue.SERVER_DELETE_PROHIBITED);
private static final Function<DomainBase, ImmutableSet<?>> GET_NAMESERVERS =
new Function<DomainBase, ImmutableSet<?>>() {
@Override
public ImmutableSet<?> apply(DomainBase domain) {
return domain.getNameservers();
}};
@Inject ExtensionManager extensionManager;
@Inject @ClientId String clientId;
@Inject @TargetId String targetId;
@ -95,7 +87,7 @@ public final class HostDeleteFlow implements TransactionalFlow {
validateClientIsLoggedIn(clientId);
DateTime now = ofy().getTransactionTime();
validateHostName(targetId);
failfastForAsyncDelete(targetId, now, HostResource.class, GET_NAMESERVERS);
failfastForAsyncDelete(targetId, now, HostResource.class, DomainBase::getNameservers);
HostResource existingHost = loadAndVerifyExistence(HostResource.class, targetId, now);
verifyNoDisallowedStatuses(existingHost, DISALLOWED_STATUSES);
if (!isSuperuser) {

View file

@ -18,11 +18,10 @@ import static google.registry.model.EppResourceUtils.isActive;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.registry.Registries.findTldForName;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import static java.util.stream.Collectors.joining;
import com.google.common.base.Ascii;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.collect.Iterables;
import com.google.common.net.InternetDomainName;
import google.registry.flows.EppException;
import google.registry.flows.EppException.AuthorizationErrorException;
@ -98,8 +97,12 @@ public class HostFlowUtils {
return Optional.absent();
}
// This is a subordinate host
String domainName = Joiner.on('.').join(Iterables.skip(
hostName.parts(), hostName.parts().size() - (tld.get().parts().size() + 1)));
String domainName =
hostName
.parts()
.stream()
.skip(hostName.parts().size() - (tld.get().parts().size() + 1))
.collect(joining("."));
DomainResource superordinateDomain = loadByForeignKey(DomainResource.class, domainName, now);
if (superordinateDomain == null || !isActive(superordinateDomain, now)) {
throw new SuperordinateDomainDoesNotExistException(domainName);

View file

@ -29,7 +29,6 @@ import google.registry.config.RegistryConfig.Config;
import google.registry.keyring.api.KeyringException;
import google.registry.util.Retrier;
import java.io.IOException;
import java.util.concurrent.Callable;
import javax.inject.Inject;
/** The {@link KmsConnection} which talks to Cloud KMS. */
@ -137,13 +136,7 @@ class KmsConnectionImpl implements KmsConnection {
public byte[] decrypt(final String cryptoKeyName, final String encodedCiphertext) {
try {
return retrier.callWithRetry(
new Callable<byte[]>() {
@Override
public byte[] call() throws IOException {
return attemptDecrypt(cryptoKeyName, encodedCiphertext);
}
},
IOException.class);
() -> attemptDecrypt(cryptoKeyName, encodedCiphertext), IOException.class);
} catch (RuntimeException e) {
throw new KeyringException(
String.format("CloudKMS decrypt operation failed for secret %s", cryptoKeyName), e);

View file

@ -17,6 +17,7 @@ package google.registry.loadtest;
import static com.google.appengine.api.taskqueue.QueueConstants.maxTasksPerAdd;
import static com.google.appengine.api.taskqueue.QueueFactory.getQueue;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Lists.partition;
import static com.google.common.collect.Lists.transform;
import static google.registry.security.XsrfTokenManager.X_CSRF_TOKEN;
@ -27,7 +28,6 @@ import static org.joda.time.DateTimeZone.UTC;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;
import google.registry.config.RegistryEnvironment;
@ -247,12 +247,12 @@ public class LoadTestAction implements Runnable {
startSecond));
tasks.addAll(
createTasks(
FluentIterable.from(
createNumCopies(xmlDomainCreateTmpl, successfulDomainCreatesPerSecond))
.transform(randomNameReplacer("%domain%", MAX_DOMAIN_LABEL_LENGTH))
.transform(listNameReplacer("%contact%", contactNames))
.transform(listNameReplacer("%host%", hostPrefixes))
.toList(),
createNumCopies(xmlDomainCreateTmpl, successfulDomainCreatesPerSecond)
.stream()
.map(randomNameReplacer("%domain%", MAX_DOMAIN_LABEL_LENGTH))
.map(listNameReplacer("%contact%", contactNames))
.map(listNameReplacer("%host%", hostPrefixes))
.collect(toImmutableList()),
startSecond));
}
ImmutableList<TaskOptions> taskOptions = tasks.build();
@ -308,19 +308,11 @@ public class LoadTestAction implements Runnable {
private Function<String, String> listNameReplacer(final String toReplace, List<String> choices) {
final Iterator<String> iterator = Iterators.cycle(choices);
return new Function<String, String>() {
@Override
public String apply(String xml) {
return xml.replace(toReplace, iterator.next());
}};
return xml -> xml.replace(toReplace, iterator.next());
}
private Function<String, String> randomNameReplacer(final String toReplace, final int numChars) {
return new Function<String, String>() {
@Override
public String apply(String xml) {
return xml.replace(toReplace, getRandomLabel(numChars));
}};
return xml -> xml.replace(toReplace, getRandomLabel(numChars));
}
private String getRandomLabel(int numChars) {

View file

@ -30,7 +30,6 @@ import google.registry.util.FormattingLogger;
import google.registry.util.Retrier;
import google.registry.util.SystemSleeper;
import java.util.NoSuchElementException;
import java.util.concurrent.Callable;
import org.joda.time.DateTime;
/** {@link InputReader} that maps over {@link CommitLogManifest}. */
@ -138,12 +137,7 @@ class CommitLogManifestReader extends InputReader<Key<CommitLogManifest>> {
final Cursor currentCursor = queryIterator.getCursor();
try {
return retrier.callWithRetry(
new Callable<Key<CommitLogManifest>>() {
@Override
public Key<CommitLogManifest> call() {
return queryIterator.next();
}
},
() -> queryIterator.next(),
new Retrier.FailureReporter() {
@Override
public void beforeRetry(Throwable thrown, int failures, int maxAttempts) {
@ -155,8 +149,7 @@ class CommitLogManifestReader extends InputReader<Key<CommitLogManifest>> {
public void afterFinalFailure(Throwable thrown, int failures) {
logger.severefmt(
"Max retry attempts reached trying to read item %d/%d. Giving up.",
loaded,
total);
loaded, total);
}
},
DatastoreTimeoutException.class);

View file

@ -14,13 +14,13 @@
package google.registry.mapreduce.inputs;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static google.registry.model.EntityClasses.CLASS_TO_KIND_FUNCTION;
import static google.registry.model.ofy.ObjectifyService.ofy;
import com.google.appengine.api.datastore.Cursor;
import com.google.appengine.api.datastore.QueryResultIterator;
import com.google.appengine.tools.mapreduce.InputReader;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.cmd.Query;
@ -137,7 +137,7 @@ abstract class EppResourceBaseReader<T> extends InputReader<T> {
ImmutableSet<Class<? extends R>> resourceClasses) {
// Ignore EppResource when finding kinds, since it doesn't have one and doesn't imply filtering.
return resourceClasses.contains(EppResource.class)
? ImmutableSet.<String>of()
: FluentIterable.from(resourceClasses).transform(CLASS_TO_KIND_FUNCTION).toSet();
? ImmutableSet.<String>of()
: resourceClasses.stream().map(CLASS_TO_KIND_FUNCTION).collect(toImmutableSet());
}
}

View file

@ -116,17 +116,12 @@ public final class EntityClasses {
/**
* Function that converts an Objectify-registered class to its Datastore kind name.
*
* <p>Note that this mapping is not one-to-one, since polymorphic subclasses of an entity all
* have the same Datastore kind. (In theory, two distinct top-level entities could also map to
* the same kind since it's just {@code class.getSimpleName()}, but we test against that.)
* <p>Note that this mapping is not one-to-one, since polymorphic subclasses of an entity all have
* the same Datastore kind. (In theory, two distinct top-level entities could also map to the same
* kind since it's just {@code class.getSimpleName()}, but we test against that.)
*/
public static final Function<Class<? extends ImmutableObject>, String> CLASS_TO_KIND_FUNCTION =
new Function<Class<? extends ImmutableObject>, String>() {
@Override
public String apply(Class<? extends ImmutableObject> clazz) {
return Key.getKind(clazz);
}
};
(Class<? extends ImmutableObject> clazz) -> Key.getKind(clazz);
private EntityClasses() {}
}

View file

@ -172,11 +172,7 @@ public final class EppResourceUtils {
* Iterables.transform() over a collection of EppResources.
*/
public static <T extends EppResource> Function<T, T> transformAtTime(final DateTime now) {
return new Function<T, T>() {
@Override
public T apply(T resource) {
return cloneProjectedAtTime(resource, now);
}};
return (T resource) -> cloneProjectedAtTime(resource, now);
}
/**

View file

@ -14,6 +14,8 @@
package google.registry.model;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Maps.transformValues;
import static google.registry.model.ofy.ObjectifyService.ofy;
@ -22,7 +24,6 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Maps;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Ignore;
@ -157,30 +158,33 @@ public abstract class ImmutableObject implements Cloneable {
}};
/** Helper function to recursively convert a ImmutableObject to a Map of generic objects. */
private static final Function<Object, Object> TO_MAP_HELPER = new Function<Object, Object>() {
@Override
public Object apply(Object o) {
if (o == null) {
return null;
} else if (o instanceof ImmutableObject) {
// LinkedHashMap to preserve field ordering and because ImmutableMap forbids null values.
Map<String, Object> result = new LinkedHashMap<>();
for (Entry<Field, Object> entry : ModelUtils.getFieldValues(o).entrySet()) {
result.put(entry.getKey().getName(), apply(entry.getValue()));
private static final Function<Object, Object> TO_MAP_HELPER =
new Function<Object, Object>() {
@Override
public Object apply(Object o) {
if (o == null) {
return null;
} else if (o instanceof ImmutableObject) {
// LinkedHashMap to preserve field ordering and because ImmutableMap forbids null
// values.
Map<String, Object> result = new LinkedHashMap<>();
for (Entry<Field, Object> entry : ModelUtils.getFieldValues(o).entrySet()) {
result.put(entry.getKey().getName(), apply(entry.getValue()));
}
return result;
} else if (o instanceof Map) {
return Maps.transformValues((Map<?, ?>) o, this);
} else if (o instanceof Set) {
return ((Set<?>) o).stream().map(this).collect(toImmutableSet());
} else if (o instanceof Collection) {
return ((Collection<?>) o).stream().map(this).collect(toImmutableList());
} else if (o instanceof Number || o instanceof Boolean) {
return o;
} else {
return o.toString();
}
}
return result;
} else if (o instanceof Map) {
return Maps.transformValues((Map<?, ?>) o, this);
} else if (o instanceof Set) {
return FluentIterable.from((Set<?>) o).transform(this).toSet();
} else if (o instanceof Collection) {
return FluentIterable.from((Collection<?>) o).transform(this).toList();
} else if (o instanceof Number || o instanceof Boolean) {
return o;
} else {
return o.toString();
}
}};
};
/** Returns a map of all object fields (including sensitive data) that's used to produce diffs. */
@SuppressWarnings("unchecked")

View file

@ -14,9 +14,10 @@
package google.registry.model;
import com.google.common.base.Function;
import static com.google.common.collect.ImmutableList.toImmutableList;
import com.google.common.base.Functions;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Streams;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
@ -29,14 +30,6 @@ import javax.annotation.Nullable;
* list is passed as {@code null}, it'll be substituted with empty list. Lists are not mutable.
*/
public final class JsonMapBuilder {
private static final Function<Jsonifiable, Map<String, Object>> TO_JSON_OBJECT =
new Function<Jsonifiable, Map<String, Object>>() {
@Override
public Map<String, Object> apply(Jsonifiable input) {
return input.toJsonMap();
}};
private final Map<String, Object> map = new LinkedHashMap<>();
public JsonMapBuilder put(String name, @Nullable Boolean value) {
@ -70,15 +63,21 @@ public final class JsonMapBuilder {
}
public <T> JsonMapBuilder putListOfStrings(String name, @Nullable Iterable<T> value) {
map.put(name, value == null ? Collections.EMPTY_LIST
: FluentIterable.from(value).transform(Functions.toStringFunction()).toList());
map.put(
name,
value == null
? Collections.EMPTY_LIST
: Streams.stream(value).map(Functions.toStringFunction()).collect(toImmutableList()));
return this;
}
public JsonMapBuilder putListOfJsonObjects(
String name, @Nullable Iterable<? extends Jsonifiable> value) {
map.put(name, value == null ? Collections.EMPTY_LIST
: FluentIterable.from(value).transform(TO_JSON_OBJECT).toList());
map.put(
name,
value == null
? Collections.EMPTY_LIST
: Streams.stream(value).map(Jsonifiable::toJsonMap).collect(toImmutableList()));
return this;
}

View file

@ -17,7 +17,6 @@ package google.registry.model;
import static com.google.common.base.Predicates.instanceOf;
import static com.google.common.base.Predicates.isNull;
import static com.google.common.base.Predicates.or;
import static com.google.common.collect.Iterables.all;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.transformValues;
import static com.google.common.collect.Sets.newLinkedHashSet;
@ -34,6 +33,7 @@ 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;
import com.googlecode.objectify.annotation.Ignore;
@ -95,25 +95,21 @@ public class ModelUtils {
body = FluentIterable.from(clazz.getEnumConstants());
} else {
stringBuilder.append("class ");
body = FluentIterable.from(getAllFields(clazz).values())
.filter(new Predicate<Field>() {
@Override
public boolean apply(Field field) {
return !field.isAnnotationPresent(Ignore.class);
}})
.transform(new Function<Field, Object>() {
@Override
public Object apply(Field field) {
String annotation = field.isAnnotationPresent(Id.class)
? "@Id "
: field.isAnnotationPresent(Parent.class)
? "@Parent "
: "";
String type = field.getType().isArray()
? field.getType().getComponentType().getName() + "[]"
: field.getGenericType().toString().replaceFirst("class ", "");
return String.format("%s%s %s", annotation, type, field.getName());
}});
body =
FluentIterable.from(getAllFields(clazz).values())
.filter((Field field) -> !field.isAnnotationPresent(Ignore.class))
.transform(
(Field field) -> {
String annotation =
field.isAnnotationPresent(Id.class)
? "@Id "
: field.isAnnotationPresent(Parent.class) ? "@Parent " : "";
String type =
field.getType().isArray()
? field.getType().getComponentType().getName() + "[]"
: field.getGenericType().toString().replaceFirst("class ", "");
return String.format("%s%s %s", annotation, type, field.getName());
});
}
return stringBuilder
.append(clazz.getName()).append(" {\n ")
@ -212,45 +208,48 @@ public class ModelUtils {
}
/** Functional helper for {@link #cloneEmptyToNull}. */
private static final Function<Object, ?> CLONE_EMPTY_TO_NULL = new Function<Object, Object>() {
@Override
public Object apply(Object obj) {
if (obj instanceof ImmutableSortedMap) {
// ImmutableSortedMapTranslatorFactory handles empty for us. If the object is null, then
// its on-save hook can't run.
private static final Function<Object, ?> CLONE_EMPTY_TO_NULL =
new Function<Object, Object>() {
@Override
public Object apply(Object obj) {
if (obj instanceof ImmutableSortedMap) {
// ImmutableSortedMapTranslatorFactory handles empty for us. If the object is null, then
// its on-save hook can't run.
return obj;
}
if ("".equals(obj)
|| (obj instanceof Collection && ((Collection<?>) obj).isEmpty())
|| (obj instanceof Map && ((Map<?, ?>) obj).isEmpty())
|| (obj != null && obj.getClass().isArray() && Array.getLength(obj) == 0)) {
return null;
}
Predicate<Object> immutableObjectOrNull = or(isNull(), instanceOf(ImmutableObject.class));
if ((obj instanceof Set || obj instanceof List)
&& Streams.stream((Iterable<?>) obj).allMatch(immutableObjectOrNull)) {
// Recurse into sets and lists, but only if they contain ImmutableObjects.
FluentIterable<?> fluent = FluentIterable.from((Iterable<?>) obj).transform(this);
return (obj instanceof List) ? newArrayList(fluent) : newLinkedHashSet(fluent);
}
if (obj instanceof Map
&& ((Map<?, ?>) obj).values().stream().allMatch(immutableObjectOrNull)) {
// Recurse into maps with ImmutableObject values.
return transformValues((Map<?, ?>) obj, this);
}
if (obj instanceof ImmutableObject) {
// Recurse on the fields of an ImmutableObject.
ImmutableObject copy = ImmutableObject.clone((ImmutableObject) obj);
for (Field field : getAllFields(obj.getClass()).values()) {
Object oldValue = getFieldValue(obj, field);
Object newValue = apply(oldValue);
if (!Objects.equals(oldValue, newValue)) {
setFieldValue(copy, field, newValue);
}
}
return copy;
}
return obj;
}
if ("".equals(obj)
|| (obj instanceof Collection && ((Collection<?>) obj).isEmpty())
|| (obj instanceof Map && ((Map<?, ?>) obj).isEmpty())
|| (obj != null && obj.getClass().isArray() && Array.getLength(obj) == 0)) {
return null;
}
Predicate<Object> immutableObjectOrNull = or(isNull(), instanceOf(ImmutableObject.class));
if ((obj instanceof Set || obj instanceof List)
&& all((Iterable<?>) obj, immutableObjectOrNull)) {
// Recurse into sets and lists, but only if they contain ImmutableObjects.
FluentIterable<?> fluent = FluentIterable.from((Iterable<?>) obj).transform(this);
return (obj instanceof List) ? newArrayList(fluent) : newLinkedHashSet(fluent);
}
if (obj instanceof Map && all(((Map<?, ?>) obj).values(), immutableObjectOrNull)) {
// Recurse into maps with ImmutableObject values.
return transformValues((Map<?, ?>) obj, this);
}
if (obj instanceof ImmutableObject) {
// Recurse on the fields of an ImmutableObject.
ImmutableObject copy = ImmutableObject.clone((ImmutableObject) obj);
for (Field field : getAllFields(obj.getClass()).values()) {
Object oldValue = getFieldValue(obj, field);
Object newValue = apply(oldValue);
if (!Objects.equals(oldValue, newValue)) {
setFieldValue(copy, field, newValue);
}
}
return copy;
}
return obj;
}};
};
/** Returns a clone of the object and sets empty collections, arrays, maps and strings to null. */
@SuppressWarnings("unchecked")

View file

@ -16,10 +16,8 @@ package google.registry.model;
import static com.google.common.base.Predicates.or;
import static com.google.common.base.Predicates.subtypeOf;
import static java.util.stream.Collectors.joining;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Ordering;
import java.util.ArrayDeque;
import java.util.Queue;
@ -60,14 +58,11 @@ public final class SchemaVersion {
* types (for classes), or else a list of all possible values (for enums).
*/
public static String getSchema() {
return FluentIterable.from(getAllPersistedTypes())
return getAllPersistedTypes()
.stream()
.filter(or(subtypeOf(Enum.class), subtypeOf(ImmutableObject.class)))
.transform(new Function<Class<?>, String>() {
@Override
public String apply(Class<?> clazz) {
return ModelUtils.getSchema(clazz);
}})
.join(Joiner.on('\n'));
.map(ModelUtils::getSchema)
.collect(joining("\n"));
}
private SchemaVersion() {}

View file

@ -14,15 +14,13 @@
package google.registry.model.billing;
import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet;
import static google.registry.model.ofy.ObjectifyService.ofy;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Maps.EntryTransformer;
import com.google.common.collect.Ordering;
import com.googlecode.objectify.cmd.Query;
import google.registry.model.CacheUtils;
@ -38,19 +36,11 @@ public final class RegistrarBillingUtils {
private static final Supplier<ImmutableSortedSet<CurrencyUnit>> CURRENCIES_CACHE =
CacheUtils.memoizeWithShortExpiration(
new Supplier<ImmutableSortedSet<CurrencyUnit>>() {
@Override
public ImmutableSortedSet<CurrencyUnit> get() {
return FluentIterable
.from(Registries.getTlds())
.transform(new Function<String, CurrencyUnit>() {
@Override
public CurrencyUnit apply(String tld) {
return Registry.get(tld).getCurrency();
}})
.toSortedSet(Ordering.natural());
}
});
() ->
Registries.getTlds()
.stream()
.map((String tld) -> Registry.get(tld).getCurrency())
.collect(toImmutableSortedSet(Ordering.natural())));
/**
* Returns set of currencies in which registrars may be billed.
@ -69,28 +59,25 @@ public final class RegistrarBillingUtils {
*/
public static ImmutableMap<CurrencyUnit, Query<RegistrarBillingEntry>> getBillingEntryQueries(
final Registrar registrar) {
return Maps.toMap(getCurrencies(),
new Function<CurrencyUnit, Query<RegistrarBillingEntry>>() {
@Override
public Query<RegistrarBillingEntry> apply(CurrencyUnit currency) {
return ofy().load()
return Maps.toMap(
getCurrencies(),
(CurrencyUnit currency) ->
ofy()
.load()
.type(RegistrarBillingEntry.class)
.ancestor(registrar)
.filter("currency", currency)
.order("-created");
}});
.order("-created"));
}
/** Returns amount of money registrar currently owes registry in each currency. */
public static Map<CurrencyUnit, Money> loadBalance(Registrar registrar) {
return Maps.transformEntries(getBillingEntryQueries(registrar),
new EntryTransformer<CurrencyUnit, Query<RegistrarBillingEntry>, Money>() {
@Override
public Money transformEntry(
CurrencyUnit currency, Query<RegistrarBillingEntry> query) {
RegistrarBillingEntry entry = query.first().now();
return entry != null ? entry.getBalance() : Money.zero(currency);
}});
return Maps.transformEntries(
getBillingEntryQueries(registrar),
(CurrencyUnit currency, Query<RegistrarBillingEntry> query) -> {
RegistrarBillingEntry entry = query.first().now();
return entry != null ? entry.getBalance() : Money.zero(currency);
});
}
private RegistrarBillingUtils() {}

View file

@ -16,15 +16,16 @@ package google.registry.model.billing;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableList.toImmutableList;
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.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Ordering;
import com.google.common.collect.Streams;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
@ -216,7 +217,8 @@ public final class RegistrarCredit extends ImmutableObject implements Buildable
* <p>The resulting list sorts the credits first by type and then by creation time.
*/
public static ImmutableList<RegistrarCredit> loadAllForRegistrar(Registrar registrar) {
return FluentIterable.from(ofy().load().type(RegistrarCredit.class).ancestor(registrar))
.toSortedList(CREDIT_PRIORITY_ORDERING);
return Streams.stream(ofy().load().type(RegistrarCredit.class).ancestor(registrar))
.sorted(CREDIT_PRIORITY_ORDERING)
.collect(toImmutableList());
}
}

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.Function;
import com.google.common.base.Optional;
import com.google.common.collect.ForwardingNavigableMap;
import com.google.common.collect.ImmutableSortedMap;
@ -193,16 +192,12 @@ public final class RegistrarCreditBalance extends ImmutableObject implements Bui
*/
@VisibleForTesting
BalanceMap(Map<DateTime, ? extends Map<DateTime, Money>> data) {
delegate = ImmutableSortedMap.copyOf(
Maps.transformValues(
data,
new Function<Map<DateTime, Money>, ImmutableSortedMap<DateTime, Money>>() {
@Override
public ImmutableSortedMap<DateTime, Money> apply(Map<DateTime, Money> map) {
return ImmutableSortedMap.copyOf(map, Ordering.natural());
}
}),
Ordering.natural());
delegate =
ImmutableSortedMap.copyOf(
Maps.transformValues(
data,
(Map<DateTime, Money> map) -> ImmutableSortedMap.copyOf(map, Ordering.natural())),
Ordering.natural());
}
@Override

View file

@ -21,7 +21,6 @@ import static google.registry.util.DateTimeUtils.isAtOrAfter;
import static google.registry.util.DateTimeUtils.isBeforeOrAt;
import static org.joda.time.DateTimeZone.UTC;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.ContiguousSet;
import com.google.common.collect.FluentIterable;
@ -85,11 +84,7 @@ public class TimeOfYear extends ImmutableObject {
normalizedRange.lowerEndpoint().getYear(),
normalizedRange.upperEndpoint().getYear());
return FluentIterable.from(ContiguousSet.create(yearRange, integers()))
.transform(new Function<Integer, DateTime>() {
@Override
public DateTime apply(Integer year) {
return getDateTimeWithYear(year);
}})
.transform(this::getDateTimeWithYear)
.filter(normalizedRange);
}

View file

@ -20,7 +20,6 @@ import static google.registry.util.CollectionUtils.nullToEmpty;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static google.registry.util.DateTimeUtils.latestOf;
import com.google.common.base.Function;
import com.google.common.collect.ForwardingMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSortedMap;
@ -103,18 +102,17 @@ public class TimedTransitionProperty<V, T extends TimedTransitionProperty.TimedT
checkArgument(
Ordering.natural().equals(valueMap.comparator()),
"Timed transition value map must have transition time keys in chronological order");
return Maps.transformEntries(valueMap, new Maps.EntryTransformer<DateTime, V, T>() {
// For each entry in the input value map, make the output map have an entry at the
// corresponding time that points to a transition containing that time and that value.
@Override
public T transformEntry(DateTime transitionTime, V value) {
checkArgument(!transitionTime.isBefore(START_OF_TIME),
return Maps.transformEntries(
valueMap,
(DateTime transitionTime, V value) -> {
checkArgument(
!transitionTime.isBefore(START_OF_TIME),
"Timed transition times cannot be earlier than START_OF_TIME / Unix Epoch");
T subclass = TypeUtils.instantiate(timedTransitionSubclass);
((TimedTransition<V>) subclass).transitionTime = transitionTime;
subclass.setValue(value);
return subclass;
}});
});
}
/**
@ -288,13 +286,7 @@ public class TimedTransitionProperty<V, T extends TimedTransitionProperty.TimedT
/** Returns the map of DateTime to value that is the "natural" representation of this property. */
public ImmutableSortedMap<DateTime, V> toValueMap() {
return ImmutableSortedMap.copyOfSorted(Maps.transformValues(
backingMap,
new Function<T, V>() {
@Override
public V apply(T timedTransition) {
return timedTransition.getValue();
}}));
return ImmutableSortedMap.copyOfSorted(Maps.transformValues(backingMap, T::getValue));
}
/**

View file

@ -17,7 +17,6 @@ package google.registry.model.contact;
import static com.google.common.base.Preconditions.checkState;
import static google.registry.util.CollectionUtils.nullToEmpty;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import google.registry.model.ImmutableObject;
import google.registry.model.contact.PostalInfo.Type;
@ -67,11 +66,7 @@ public class ContactCommand {
// There can be no more than 2 postalInfos (enforced by the schema), and if there are 2 they
// must be of different types (not enforced). If the type is repeated, uniqueIndex will throw.
checkState(nullToEmpty(postalInfo).size() <= 2);
return Maps.uniqueIndex(nullToEmpty(postalInfo), new Function<PostalInfo, Type>() {
@Override
public Type apply(PostalInfo info) {
return info.getType();
}});
return Maps.uniqueIndex(nullToEmpty(postalInfo), PostalInfo::getType);
}
public ContactPhoneNumber getVoice() {

View file

@ -15,13 +15,12 @@
package google.registry.model.contact;
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.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.IgnoreSave;
import com.googlecode.objectify.annotation.Index;
@ -33,6 +32,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.stream.Stream;
import javax.xml.bind.annotation.XmlElement;
import org.joda.time.DateTime;
@ -170,10 +170,9 @@ public class ContactResource extends EppResource implements
*/
@XmlElement(name = "postalInfo")
public ImmutableList<PostalInfo> getPostalInfosAsList() {
return FluentIterable
.from(Lists.newArrayList(localizedPostalInfo, internationalizedPostalInfo))
return Stream.of(localizedPostalInfo, internationalizedPostalInfo)
.filter(Predicates.notNull())
.toList();
.collect(toImmutableList());
}
@Override

View file

@ -17,8 +17,8 @@ package google.registry.model.domain;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Predicates.not;
import static com.google.common.base.Strings.emptyToNull;
import static com.google.common.collect.Iterables.all;
import static com.google.common.collect.Iterables.any;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet;
import static com.google.common.collect.Sets.difference;
import static com.google.common.collect.Sets.union;
import static google.registry.model.ofy.ObjectifyService.ofy;
@ -30,9 +30,7 @@ import static google.registry.util.DomainNameUtils.canonicalizeDomainName;
import static google.registry.util.DomainNameUtils.getTldFromDomainName;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Ordering;
@ -49,6 +47,7 @@ import google.registry.model.domain.launch.LaunchNotice;
import google.registry.model.domain.secdns.DelegationSignerData;
import google.registry.model.host.HostResource;
import java.util.Set;
import java.util.stream.Stream;
/** Shared base class for {@link DomainResource} and {@link DomainApplication}. */
@ReportedOn
@ -134,33 +133,28 @@ public abstract class DomainBase extends EppResource {
/** Loads and returns the fully qualified host names of all linked nameservers. */
public ImmutableSortedSet<String> loadNameserverFullyQualifiedHostNames() {
return FluentIterable.from(ofy().load().keys(getNameservers()).values())
.transform(
new Function<HostResource, String>() {
@Override
public String apply(HostResource host) {
return host.getFullyQualifiedHostName();
}
})
.toSortedSet(Ordering.natural());
return ofy()
.load()
.keys(getNameservers())
.values()
.stream()
.map(HostResource::getFullyQualifiedHostName)
.collect(toImmutableSortedSet(Ordering.natural()));
}
/** A key to the registrant who registered this domain. */
public Key<ContactResource> getRegistrant() {
return FluentIterable
.from(nullToEmpty(allContacts))
return nullToEmpty(allContacts)
.stream()
.filter(IS_REGISTRANT)
.first()
.findFirst()
.get()
.getContactKey();
}
/** Associated contacts for the domain (other than registrant). */
public ImmutableSet<DesignatedContact> getContacts() {
return FluentIterable
.from(nullToEmpty(allContacts))
.filter(not(IS_REGISTRANT))
.toSet();
return nullToEmpty(allContacts).stream().filter(not(IS_REGISTRANT)).collect(toImmutableSet());
}
public DomainAuthInfo getAuthInfo() {
@ -183,11 +177,7 @@ public abstract class DomainBase extends EppResource {
/** Predicate to determine if a given {@link DesignatedContact} is the registrant. */
private static final Predicate<DesignatedContact> IS_REGISTRANT =
new Predicate<DesignatedContact>() {
@Override
public boolean apply(DesignatedContact contact) {
return DesignatedContact.Type.REGISTRANT.equals(contact.type);
}};
(DesignatedContact contact) -> DesignatedContact.Type.REGISTRANT.equals(contact.type);
/** An override of {@link EppResource#asBuilder} with tighter typing. */
@Override
@ -208,7 +198,7 @@ public abstract class DomainBase extends EppResource {
T instance = getInstance();
checkArgumentNotNull(
emptyToNull(instance.fullyQualifiedDomainName), "Missing fullyQualifiedDomainName");
checkArgument(any(instance.allContacts, IS_REGISTRANT), "Missing registrant");
checkArgument(instance.allContacts.stream().anyMatch(IS_REGISTRANT), "Missing registrant");
instance.tld = getTldFromDomainName(instance.fullyQualifiedDomainName);
return super.build();
}
@ -255,13 +245,13 @@ public abstract class DomainBase extends EppResource {
}
public B setContacts(ImmutableSet<DesignatedContact> contacts) {
checkArgument(all(contacts, not(IS_REGISTRANT)), "Registrant cannot be a contact");
checkArgument(contacts.stream().noneMatch(IS_REGISTRANT), "Registrant cannot be a contact");
// Replace the non-registrant contacts inside allContacts.
getInstance().allContacts = FluentIterable
.from(nullToEmpty(getInstance().allContacts))
.filter(IS_REGISTRANT)
.append(contacts)
.toSet();
getInstance().allContacts =
Stream.concat(
nullToEmpty(getInstance().allContacts).stream().filter(IS_REGISTRANT),
contacts.stream())
.collect(toImmutableSet());
return thisCastToDerived();
}

View file

@ -27,7 +27,6 @@ import static google.registry.util.CollectionUtils.nullToEmpty;
import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy;
import static google.registry.util.CollectionUtils.union;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
@ -458,13 +457,7 @@ public class DomainCommand {
throw new InvalidReferencesException(
clazz, ImmutableSet.copyOf(difference(foreignKeys, fkis.keySet())));
}
return ImmutableMap.copyOf(transformValues(
fkis,
new Function<ForeignKeyIndex<T>, Key<T>>() {
@Override
public Key<T> apply(ForeignKeyIndex<T> fki) {
return fki.getResourceKey();
}}));
return ImmutableMap.copyOf(transformValues(fkis, ForeignKeyIndex::getResourceKey));
}
/** Exception to throw when referenced objects don't exist. */

View file

@ -16,14 +16,12 @@ package google.registry.model.domain.rgp;
import static com.google.common.base.Predicates.equalTo;
import static com.google.common.base.Predicates.not;
import static java.util.Arrays.asList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import google.registry.model.translators.EnumToAttributeAdapter;
import google.registry.model.translators.EnumToAttributeAdapter.EppEnum;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@ -101,15 +99,12 @@ public enum GracePeriodStatus implements EppEnum {
/** Provide a quick lookup of GracePeriodStatus from XML name. */
private static final ImmutableMap<String, GracePeriodStatus> XML_NAME_TO_GRACE_PERIOD_STATUS =
Maps.uniqueIndex(
// SUNRUSH_ADD isn't a real grace period type visible in EPP XML, so exclude it.
Iterables.filter(asList(GracePeriodStatus.values()), not(equalTo(SUNRUSH_ADD))),
new Function<GracePeriodStatus, String>() {
@Override
public String apply(GracePeriodStatus gracePeriodStatus) {
return gracePeriodStatus.xmlName;
}
});
Stream.of(GracePeriodStatus.values())
.filter(not(equalTo(SUNRUSH_ADD)))
.collect(
toImmutableMap(
(GracePeriodStatus gracePeriodStatus) -> gracePeriodStatus.xmlName,
value -> value));
@XmlAttribute(name = "s")
private final String xmlName;

View file

@ -14,11 +14,9 @@
package google.registry.model.eppcommon;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Maps.uniqueIndex;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import google.registry.model.domain.allocate.AllocateCreateExtension;
@ -98,16 +96,9 @@ public class ProtocolDefinition {
}
/** Converts a service extension enum to its URI. */
private static final Function<ServiceExtension, String> TO_URI_FUNCTION =
new Function<ServiceExtension, String>() {
@Override
public String apply(ServiceExtension serviceExtension) {
return serviceExtension.getUri();
}};
/** This stores a map from URI back to the service extension enum. */
private static final ImmutableMap<String, ServiceExtension> serviceExtensionByUri =
uniqueIndex(EnumSet.allOf(ServiceExtension.class), TO_URI_FUNCTION);
uniqueIndex(EnumSet.allOf(ServiceExtension.class), ServiceExtension::getUri);
/** Returns the service extension enum associated with a URI, or null if none are associated. */
public static ServiceExtension getServiceExtensionFromUri(String uri) {
@ -116,16 +107,11 @@ public class ProtocolDefinition {
/** A set of all the visible extension URIs. */
private static final ImmutableSet<String> visibleServiceExtensionUris =
FluentIterable.from(EnumSet.allOf(ServiceExtension.class))
.filter(
new Predicate<ServiceExtension>() {
@Override
public boolean apply(ServiceExtension serviceExtension) {
return serviceExtension.getVisible();
}
})
.transform(TO_URI_FUNCTION)
.toSet();
EnumSet.allOf(ServiceExtension.class)
.stream()
.filter(ServiceExtension::getVisible)
.map(ServiceExtension::getUri)
.collect(toImmutableSet());
/** Return the set of all visible service extension URIs. */
public static ImmutableSet<String> getVisibleServiceExtensionUris() {

View file

@ -19,7 +19,6 @@ import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy;
import com.google.common.base.Ascii;
import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import google.registry.model.ImmutableObject;
@ -153,7 +152,13 @@ public class EppInput extends ImmutableObject {
/** Get the extension based on type, or null. If there are multiple, it chooses the first. */
@Nullable
public <E extends CommandExtension> E getSingleExtension(Class<E> clazz) {
return FluentIterable.from(getCommandWrapper().getExtensions()).filter(clazz).first().orNull();
return getCommandWrapper()
.getExtensions()
.stream()
.filter(clazz::isInstance)
.map(clazz::cast)
.findFirst()
.orElse(null);
}
/** A tag that goes inside of an EPP {@literal <command>}. */

View file

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

View file

@ -18,7 +18,6 @@ import static com.google.common.collect.Maps.filterValues;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.TypeUtils.instantiate;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.googlecode.objectify.Key;
@ -166,10 +165,6 @@ public abstract class ForeignKeyIndex<E extends EppResource> extends BackupGroup
Class<E> clazz, Iterable<String> foreignKeys, final DateTime now) {
return filterValues(
ofy().load().type(mapToFkiClass(clazz)).ids(foreignKeys),
new Predicate<ForeignKeyIndex<?>>() {
@Override
public boolean apply(ForeignKeyIndex<?> fki) {
return now.isBefore(fki.deletionTime);
}});
(ForeignKeyIndex<?> fki) -> now.isBefore(fki.deletionTime));
}
}

View file

@ -16,14 +16,13 @@ package google.registry.model.ofy;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.DiscreteDomain.integers;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static google.registry.config.RegistryConfig.getCommitLogBucketCount;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.ContiguousSet;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Range;
@ -137,13 +136,10 @@ public class CommitLogBucket extends ImmutableObject implements Buildable {
/** Returns all commit log bucket keys, in ascending order by bucket ID. */
public static ImmutableSet<Key<CommitLogBucket>> getAllBucketKeys() {
return FluentIterable.from(getBucketIds())
.transform(new Function<Integer, Key<CommitLogBucket>>() {
@Override
public Key<CommitLogBucket> apply(Integer bucketId) {
return getBucketKeyUnsafe(bucketId);
}})
.toSet();
return getBucketIds()
.stream()
.map(CommitLogBucket::getBucketKeyUnsafe)
.collect(toImmutableSet());
}
@Override

View file

@ -17,6 +17,7 @@ package google.registry.model.ofy;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Predicates.in;
import static com.google.common.base.Predicates.not;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Maps.filterKeys;
import static com.google.common.collect.Sets.difference;
import static com.google.common.collect.Sets.union;
@ -25,7 +26,6 @@ import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.DateTimeUtils.isBeforeOrAt;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key;
@ -150,14 +150,13 @@ class CommitLoggedWork<R> extends VoidWork {
ImmutableSet.copyOf(filterKeys(rootsForTouchedKeys, not(in(touchedKeys))).values());
manifest = CommitLogManifest.create(info.bucketKey, info.transactionTime, info.getDeletes());
final Key<CommitLogManifest> manifestKey = Key.create(manifest);
mutations = FluentIterable
.from(union(info.getSaves(), untouchedRootsWithTouchedChildren))
.transform(new Function<Object, ImmutableObject>() {
@Override
public CommitLogMutation apply(Object saveEntity) {
return CommitLogMutation.create(manifestKey, saveEntity);
}})
.toSet();
mutations =
union(info.getSaves(), untouchedRootsWithTouchedChildren)
.stream()
.map(
(Function<Object, ImmutableObject>)
(Object saveEntity) -> CommitLogMutation.create(manifestKey, saveEntity))
.collect(toImmutableSet());
ofy().saveWithoutBackup()
.entities(new ImmutableSet.Builder<>()
.add(manifest)

View file

@ -29,7 +29,7 @@ import com.google.appengine.api.taskqueue.TransientFailureException;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Streams;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyFactory;
@ -136,7 +136,7 @@ public class Ofy {
@Override
protected void handleDeletion(Iterable<Key<?>> keys) {
assertInTransaction();
checkState(Iterables.all(keys, notNull()), "Can't delete a null key.");
checkState(Streams.stream(keys).allMatch(notNull()), "Can't delete a null key.");
checkProhibitedAnnotations(keys, NotBackedUp.class, VirtualEntity.class);
TRANSACTION_INFO.get().putDeletes(keys);
}
@ -168,7 +168,7 @@ public class Ofy {
@Override
protected void handleSave(Iterable<?> entities) {
assertInTransaction();
checkState(Iterables.all(entities, notNull()), "Can't save a null entity.");
checkState(Streams.stream(entities).allMatch(notNull()), "Can't save a null entity.");
checkProhibitedAnnotations(entities, NotBackedUp.class, VirtualEntity.class);
ImmutableMap<Key<?>, ?> keysToEntities = uniqueIndex(entities, OBJECTS_TO_KEYS);
TRANSACTION_INFO.get().putSaves(keysToEntities);

View file

@ -15,7 +15,6 @@
package google.registry.model.ofy;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
@ -43,17 +42,20 @@ 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(new Predicate<StackTraceElement>() {
@Override
public boolean apply(StackTraceElement element) {
return !element.getClassName().startsWith(Objectify.class.getPackage().getName())
&& !element.getClassName().startsWith(Ofy.class.getName());
}}).get())));
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())));
}
}

View file

@ -16,6 +16,7 @@ package google.registry.model.ofy;
import static com.google.common.base.Functions.constant;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Maps.filterValues;
import static com.google.common.collect.Maps.toMap;
import static google.registry.model.ofy.CommitLogBucket.getArbitraryBucketId;
@ -23,7 +24,6 @@ import static google.registry.model.ofy.ObjectifyService.ofy;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key;
@ -88,9 +88,11 @@ class TransactionInfo {
}
ImmutableSet<Object> getSaves() {
return FluentIterable
.from(changesBuilder.build().values())
return changesBuilder
.build()
.values()
.stream()
.filter(Predicates.not(IS_DELETE))
.toSet();
.collect(toImmutableSet());
}
}

View file

@ -14,6 +14,7 @@
package google.registry.model.poll;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static google.registry.util.CollectionUtils.forceEmptyToNull;
import static google.registry.util.CollectionUtils.nullToEmpty;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
@ -24,6 +25,7 @@ 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;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.EntitySubclass;
@ -235,26 +237,48 @@ public abstract class PollMessage extends ImmutableObject
public Builder setResponseData(ImmutableList<? extends ResponseData> responseData) {
FluentIterable<? extends ResponseData> iterable = FluentIterable.from(responseData);
getInstance().contactPendingActionNotificationResponses = forceEmptyToNull(
iterable.filter(ContactPendingActionNotificationResponse.class).toList());
getInstance().contactTransferResponses = forceEmptyToNull(
iterable.filter(ContactTransferResponse.class).toList());
getInstance().domainPendingActionNotificationResponses = forceEmptyToNull(
iterable.filter(DomainPendingActionNotificationResponse.class).toList());
getInstance().domainTransferResponses = forceEmptyToNull(
iterable.filter(DomainTransferResponse.class).toList());
getInstance().hostPendingActionNotificationResponses = forceEmptyToNull(
iterable.filter(HostPendingActionNotificationResponse.class).toList());
getInstance().contactPendingActionNotificationResponses =
forceEmptyToNull(
Streams.stream(iterable)
.filter(ContactPendingActionNotificationResponse.class::isInstance)
.map(ContactPendingActionNotificationResponse.class::cast)
.collect(toImmutableList()));
getInstance().contactTransferResponses =
forceEmptyToNull(
Streams.stream(iterable)
.filter(ContactTransferResponse.class::isInstance)
.map(ContactTransferResponse.class::cast)
.collect(toImmutableList()));
getInstance().domainPendingActionNotificationResponses =
forceEmptyToNull(
Streams.stream(iterable)
.filter(DomainPendingActionNotificationResponse.class::isInstance)
.map(DomainPendingActionNotificationResponse.class::cast)
.collect(toImmutableList()));
getInstance().domainTransferResponses =
forceEmptyToNull(
Streams.stream(iterable)
.filter(DomainTransferResponse.class::isInstance)
.map(DomainTransferResponse.class::cast)
.collect(toImmutableList()));
getInstance().hostPendingActionNotificationResponses =
forceEmptyToNull(
Streams.stream(iterable)
.filter(HostPendingActionNotificationResponse.class::isInstance)
.map(HostPendingActionNotificationResponse.class::cast)
.collect(toImmutableList()));
return this;
}
public Builder setResponseExtensions(
ImmutableList<? extends ResponseExtension> responseExtensions) {
getInstance().launchInfoResponseExtension = FluentIterable
.from(responseExtensions)
.filter(LaunchInfoResponseExtension.class)
.first()
.orNull();
getInstance().launchInfoResponseExtension =
responseExtensions
.stream()
.filter(LaunchInfoResponseExtension.class::isInstance)
.map(LaunchInfoResponseExtension.class::cast)
.findFirst()
.orElse(null);
return this;
}
}

View file

@ -21,6 +21,7 @@ import static com.google.common.base.Predicates.in;
import static com.google.common.base.Predicates.notNull;
import static com.google.common.base.Strings.emptyToNull;
import static com.google.common.base.Strings.nullToEmpty;
import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet;
import static com.google.common.collect.Sets.immutableEnumSet;
import static com.google.common.io.BaseEncoding.base64;
import static google.registry.config.RegistryConfig.getDefaultRegistrarReferralUrl;
@ -35,13 +36,13 @@ import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import static google.registry.util.X509Utils.getCertificateHash;
import static google.registry.util.X509Utils.loadCertificate;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Comparator.comparing;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Strings;
import com.google.common.base.Supplier;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
@ -50,6 +51,7 @@ import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Ordering;
import com.google.common.collect.Range;
import com.google.common.collect.Sets;
import com.google.common.collect.Streams;
import com.google.re2j.Pattern;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Work;
@ -193,12 +195,9 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
* Compare two instances of {@link RegistrarContact} by their email addresses lexicographically.
*/
private static final Comparator<RegistrarContact> CONTACT_EMAIL_COMPARATOR =
new Comparator<RegistrarContact>() {
@Override
public int compare(RegistrarContact rc1, RegistrarContact rc2) {
return rc1.getEmailAddress().compareTo(rc2.getEmailAddress());
}
};
comparing(
(RegistrarContact arg) -> arg.getEmailAddress(),
(String leftProperty, String rightProperty) -> leftProperty.compareTo(rightProperty));
/**
* A caching {@link Supplier} of a clientId to {@link Registrar} map.
@ -207,19 +206,21 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
* query inside an unrelated client-affecting transaction.
*/
private static final Supplier<ImmutableMap<String, Registrar>> CACHE_BY_CLIENT_ID =
memoizeWithShortExpiration(new Supplier<ImmutableMap<String, Registrar>>() {
@Override
public ImmutableMap<String, Registrar> get() {
return ofy().doTransactionless(new Work<ImmutableMap<String, Registrar>>() {
@Override
public ImmutableMap<String, Registrar> run() {
ImmutableMap.Builder<String, Registrar> builder = new ImmutableMap.Builder<>();
for (Registrar registrar : loadAll()) {
builder.put(registrar.getClientId(), registrar);
}
return builder.build();
}});
}});
memoizeWithShortExpiration(
() ->
ofy()
.doTransactionless(
new Work<ImmutableMap<String, Registrar>>() {
@Override
public ImmutableMap<String, Registrar> run() {
ImmutableMap.Builder<String, Registrar> builder =
new ImmutableMap.Builder<>();
for (Registrar registrar : loadAll()) {
builder.put(registrar.getClientId(), registrar);
}
return builder.build();
}
}));
@Parent
Key<EntityGroupRoot> parent = getCrossTldKey();
@ -421,14 +422,13 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
BillingMethod billingMethod;
@NonFinalForTesting
private static Supplier<byte[]> saltSupplier = new Supplier<byte[]>() {
@Override
public byte[] get() {
// There are 32 bytes in a sha-256 hash, and the salt should generally be the same size.
byte[] salt = new byte[32];
new SecureRandom().nextBytes(salt);
return salt;
}};
private static Supplier<byte[]> saltSupplier =
() -> {
// There are 32 bytes in a sha-256 hash, and the salt should generally be the same size.
byte[] salt = new byte[32];
new SecureRandom().nextBytes(salt);
return salt;
};
public String getClientId() {
return clientIdentifier;
@ -576,9 +576,9 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
* address.
*/
public ImmutableSortedSet<RegistrarContact> getContacts() {
return FluentIterable.from(getContactsIterable())
return Streams.stream(getContactsIterable())
.filter(notNull())
.toSortedSet(CONTACT_EMAIL_COMPARATOR);
.collect(toImmutableSortedSet(CONTACT_EMAIL_COMPARATOR));
}
/**
@ -586,16 +586,10 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
* their email address.
*/
public ImmutableSortedSet<RegistrarContact> getContactsOfType(final RegistrarContact.Type type) {
return FluentIterable.from(getContactsIterable())
return Streams.stream(getContactsIterable())
.filter(notNull())
.filter(
new Predicate<RegistrarContact>() {
@Override
public boolean apply(@Nullable RegistrarContact contact) {
return contact.getTypes().contains(type);
}
})
.toSortedSet(CONTACT_EMAIL_COMPARATOR);
.filter((@Nullable RegistrarContact contact) -> contact.getTypes().contains(type))
.collect(toImmutableSortedSet(CONTACT_EMAIL_COMPARATOR));
}
private Iterable<RegistrarContact> getContactsIterable() {

View file

@ -16,17 +16,17 @@ package google.registry.model.registrar;
import static com.google.common.base.Functions.toStringFunction;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Sets.difference;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.CollectionUtils.nullToEmptyImmutableSortedCopy;
import static google.registry.util.ObjectifyUtils.OBJECTS_TO_KEYS;
import static java.util.stream.Collectors.joining;
import com.google.common.base.Enums;
import com.google.common.base.Joiner;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Streams;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.VoidWork;
import com.googlecode.objectify.annotation.Entity;
@ -141,7 +141,9 @@ public class RegistrarContact extends ImmutableObject implements Jsonifiable {
}
public static ImmutableSet<Type> typesFromStrings(Iterable<String> typeNames) {
return FluentIterable.from(typeNames).transform(Enums.stringConverter(Type.class)).toSet();
return Streams.stream(typeNames)
.map(Enums.stringConverter(Type.class))
.collect(toImmutableSet());
}
/**
@ -154,15 +156,25 @@ public class RegistrarContact extends ImmutableObject implements Jsonifiable {
*/
public static void updateContacts(
final Registrar registrar, final Set<RegistrarContact> contacts) {
ofy().transact(new VoidWork() {
@Override
public void vrun() {
ofy().delete().keys(difference(
ImmutableSet.copyOf(
ofy().load().type(RegistrarContact.class).ancestor(registrar).keys()),
FluentIterable.from(contacts).transform(OBJECTS_TO_KEYS).toSet()));
ofy().save().entities(contacts);
}});
ofy()
.transact(
new VoidWork() {
@Override
public void vrun() {
ofy()
.delete()
.keys(
difference(
ImmutableSet.copyOf(
ofy()
.load()
.type(RegistrarContact.class)
.ancestor(registrar)
.keys()),
contacts.stream().map(OBJECTS_TO_KEYS).collect(toImmutableSet())));
ofy().save().entities(contacts);
}
});
}
public Key<Registrar> getParent() {
@ -260,7 +272,7 @@ public class RegistrarContact extends ImmutableObject implements Jsonifiable {
.put("emailAddress", emailAddress)
.put("phoneNumber", phoneNumber)
.put("faxNumber", faxNumber)
.put("types", Joiner.on(',').join(transform(getTypes(), toStringFunction())))
.put("types", getTypes().stream().map(toStringFunction()).collect(joining(",")))
.put("visibleInWhoisAsAdmin", visibleInWhoisAsAdmin)
.put("visibleInWhoisAsTech", visibleInWhoisAsTech)
.put("visibleInDomainWhoisAsAbuse", visibleInDomainWhoisAsAbuse)

View file

@ -19,6 +19,7 @@ import static com.google.common.base.Predicates.equalTo;
import static com.google.common.base.Predicates.in;
import static com.google.common.base.Predicates.not;
import static com.google.common.base.Strings.emptyToNull;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Maps.filterValues;
import static google.registry.model.CacheUtils.memoizeWithShortExpiration;
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
@ -28,9 +29,9 @@ 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.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import com.google.common.net.InternetDomainName;
import com.googlecode.objectify.Work;
import google.registry.model.registry.Registry.TldType;
@ -50,19 +51,22 @@ public final class Registries {
* query inside an unrelated client-affecting transaction.
*/
private static Supplier<ImmutableMap<String, TldType>> createFreshCache() {
return memoizeWithShortExpiration(new Supplier<ImmutableMap<String, TldType>>() {
@Override
public ImmutableMap<String, TldType> get() {
return ofy().doTransactionless(new Work<ImmutableMap<String, TldType>>() {
@Override
public ImmutableMap<String, TldType> run() {
ImmutableMap.Builder<String, TldType> builder = new ImmutableMap.Builder<>();
for (Registry registry : ofy().load().type(Registry.class).ancestor(getCrossTldKey())) {
builder.put(registry.getTldStr(), registry.getTldType());
}
return builder.build();
}});
}});
return memoizeWithShortExpiration(
() ->
ofy()
.doTransactionless(
new Work<ImmutableMap<String, TldType>>() {
@Override
public ImmutableMap<String, TldType> run() {
ImmutableMap.Builder<String, TldType> builder =
new ImmutableMap.Builder<>();
for (Registry registry :
ofy().load().type(Registry.class).ancestor(getCrossTldKey())) {
builder.put(registry.getTldStr(), registry.getTldType());
}
return builder.build();
}
}));
}
/** Manually reset the static cache backing the methods on this class. */
@ -93,7 +97,8 @@ public final class Registries {
for (String tld : tlds) {
checkArgumentNotNull(emptyToNull(tld), "Null or empty TLD specified");
}
ImmutableSet<String> badTlds = FluentIterable.from(tlds).filter(not(in(getTlds()))).toSet();
ImmutableSet<String> badTlds =
Streams.stream(tlds).filter(not(in(getTlds()))).collect(toImmutableSet());
checkArgument(badTlds.isEmpty(), "TLDs do not exist: %s", Joiner.on(", ").join(badTlds));
return tlds;
}

View file

@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.equalTo;
import static com.google.common.base.Predicates.not;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static google.registry.config.RegistryConfig.getSingletonCacheRefreshDuration;
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
import static google.registry.model.ofy.ObjectifyService.ofy;
@ -34,7 +35,6 @@ 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.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Iterables;
@ -799,15 +799,12 @@ public class Registry extends ImmutableObject implements Buildable {
}
}
ImmutableSet<Entry<String, Collection<String>>> conflicts =
FluentIterable.from(allAuthCodes.asMap().entrySet())
.filter(
new Predicate<Entry<String, Collection<String>>>() {
@Override
public boolean apply(Entry<String, Collection<String>> entry) {
return entry.getValue().size() > 1;
}
})
.toSet();
allAuthCodes
.asMap()
.entrySet()
.stream()
.filter((Entry<String, Collection<String>> entry) -> entry.getValue().size() > 1)
.collect(toImmutableSet());
checkArgument(
conflicts.isEmpty(),
"Cannot set reserved lists because of auth code conflicts for labels: %s",
@ -837,14 +834,7 @@ public class Registry extends ImmutableObject implements Buildable {
ImmutableSortedMap<DateTime, Money> renewCostsMap) {
checkArgumentNotNull(renewCostsMap, "Renew billing costs map cannot be null");
checkArgument(
Iterables.all(
renewCostsMap.values(),
new Predicate<Money>() {
@Override
public boolean apply(Money amount) {
return amount.isPositiveOrZero();
}
}),
renewCostsMap.values().stream().allMatch(Money::isPositiveOrZero),
"Renew billing cost cannot be negative");
getInstance().renewBillingCostTransitions =
TimedTransitionProperty.fromValueMap(renewCostsMap, BillingCostTransition.class);
@ -855,14 +845,7 @@ public class Registry extends ImmutableObject implements Buildable {
public Builder setEapFeeSchedule(ImmutableSortedMap<DateTime, Money> eapFeeSchedule) {
checkArgumentNotNull(eapFeeSchedule, "EAP schedule map cannot be null");
checkArgument(
Iterables.all(
eapFeeSchedule.values(),
new Predicate<Money>() {
@Override
public boolean apply(Money amount) {
return amount.isPositiveOrZero();
}
}),
eapFeeSchedule.values().stream().allMatch(Money::isPositiveOrZero),
"EAP fee cannot be negative");
getInstance().eapFeeSchedule =
TimedTransitionProperty.fromValueMap(eapFeeSchedule, BillingCostTransition.class);
@ -939,17 +922,12 @@ public class Registry extends ImmutableObject implements Buildable {
instance.getServerStatusChangeCost().getCurrencyUnit().equals(instance.currency),
"Server status change cost must be in the registry's currency");
Predicate<Money> currencyCheck =
new Predicate<Money>() {
@Override
public boolean apply(Money money) {
return money.getCurrencyUnit().equals(instance.currency);
}
};
(Money money) -> money.getCurrencyUnit().equals(instance.currency);
checkArgument(
Iterables.all(instance.getRenewBillingCostTransitions().values(), currencyCheck),
instance.getRenewBillingCostTransitions().values().stream().allMatch(currencyCheck),
"Renew cost must be in the registry's currency");
checkArgument(
Iterables.all(instance.eapFeeSchedule.toValueMap().values(), currencyCheck),
instance.eapFeeSchedule.toValueMap().values().stream().allMatch(currencyCheck),
"All EAP fees must be in the registry's currency");
checkArgumentNotNull(
instance.pricingEngineClassName, "All registries must have a configured pricing engine");

View file

@ -15,6 +15,7 @@
package google.registry.model.registry.label;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Iterables.partition;
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
import static google.registry.model.ofy.ObjectifyService.ofy;
@ -30,12 +31,11 @@ import static org.joda.time.DateTimeZone.UTC;
import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.cache.CacheLoader.InvalidCacheLoadException;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.VoidWork;
import com.googlecode.objectify.Work;
@ -202,15 +202,9 @@ public final class PremiumListUtils {
@VisibleForTesting
public static ImmutableSet<PremiumListEntry> parentPremiumListEntriesOnRevision(
Iterable<PremiumListEntry> entries, final Key<PremiumListRevision> revisionKey) {
return FluentIterable.from(entries)
.transform(
new Function<PremiumListEntry, PremiumListEntry>() {
@Override
public PremiumListEntry apply(PremiumListEntry entry) {
return entry.asBuilder().setParent(revisionKey).build();
}
})
.toSet();
return Streams.stream(entries)
.map((PremiumListEntry entry) -> entry.asBuilder().setParent(revisionKey).build())
.collect(toImmutableSet());
}
/** Deletes the PremiumList and all of its child entities. */

View file

@ -17,6 +17,7 @@ package google.registry.model.registry.label;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Iterables.getOnlyElement;
import static google.registry.config.RegistryConfig.getDomainLabelListCacheDuration;
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
@ -28,14 +29,12 @@ import static google.registry.util.CollectionUtils.nullToEmpty;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.joda.time.DateTimeZone.UTC;
import com.google.common.base.Function;
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;
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.net.InternetDomainName;
@ -217,15 +216,10 @@ public final class ReservedList
if (label.length() == 0) {
return ImmutableSet.of(FULLY_BLOCKED);
}
return FluentIterable.from(getReservedListEntries(label, tld))
.transform(
new Function<ReservedListEntry, ReservationType>() {
@Override
public ReservationType apply(ReservedListEntry reservedListEntry) {
return reservedListEntry.reservationType;
}
})
.toSet();
return getReservedListEntries(label, tld)
.stream()
.map((ReservedListEntry reservedListEntry) -> reservedListEntry.reservationType)
.collect(toImmutableSet());
}
/**

View file

@ -25,7 +25,6 @@ import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static google.registry.util.DateTimeUtils.isBeforeOrAt;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
@ -93,31 +92,36 @@ public class SignedMarkRevocationList extends ImmutableObject {
* single {@link SignedMarkRevocationList} object.
*/
private static final Supplier<SignedMarkRevocationList> CACHE =
memoizeWithShortExpiration(new Supplier<SignedMarkRevocationList>() {
@Override
public SignedMarkRevocationList get() {
// Open a new transactional read even if we are in a transaction currently.
return ofy().transactNewReadOnly(new Work<SignedMarkRevocationList>() {
@Override
public SignedMarkRevocationList run() {
Iterable<SignedMarkRevocationList> shards = ofy()
.load()
.type(SignedMarkRevocationList.class)
.ancestor(getCrossTldKey());
DateTime creationTime =
isEmpty(shards)
? START_OF_TIME
: checkNotNull(Iterables.get(shards, 0).creationTime, "creationTime");
ImmutableMap.Builder<String, DateTime> revokes = new ImmutableMap.Builder<>();
for (SignedMarkRevocationList shard : shards) {
revokes.putAll(shard.revokes);
checkState(
creationTime.equals(shard.creationTime),
"Inconsistent creation times: %s vs. %s", creationTime, shard.creationTime);
}
return create(creationTime, revokes.build());
}});
}});
memoizeWithShortExpiration(
() ->
ofy()
.transactNewReadOnly(
new Work<SignedMarkRevocationList>() {
@Override
public SignedMarkRevocationList run() {
Iterable<SignedMarkRevocationList> shards =
ofy()
.load()
.type(SignedMarkRevocationList.class)
.ancestor(getCrossTldKey());
DateTime creationTime =
isEmpty(shards)
? START_OF_TIME
: checkNotNull(
Iterables.get(shards, 0).creationTime, "creationTime");
ImmutableMap.Builder<String, DateTime> revokes =
new ImmutableMap.Builder<>();
for (SignedMarkRevocationList shard : shards) {
revokes.putAll(shard.revokes);
checkState(
creationTime.equals(shard.creationTime),
"Inconsistent creation times: %s vs. %s",
creationTime,
shard.creationTime);
}
return create(creationTime, revokes.build());
}
}));
/** Return a single logical instance that combines all Datastore shards. */
public static SignedMarkRevocationList get() {
@ -154,25 +158,34 @@ public class SignedMarkRevocationList extends ImmutableObject {
/** Save this list to Datastore in sharded form. Returns {@code this}. */
public SignedMarkRevocationList save() {
ofy().transact(new VoidWork() {
@Override
public void vrun() {
ofy().deleteWithoutBackup().keys(ofy()
.load()
.type(SignedMarkRevocationList.class)
.ancestor(getCrossTldKey())
.keys());
ofy().saveWithoutBackup().entities(FluentIterable
.from(CollectionUtils.partitionMap(revokes, SHARD_SIZE))
.transform(new Function<ImmutableMap<String, DateTime>, SignedMarkRevocationList>() {
ofy()
.transact(
new VoidWork() {
@Override
public SignedMarkRevocationList apply(ImmutableMap<String, DateTime> shardRevokes) {
SignedMarkRevocationList shard = create(creationTime, shardRevokes);
shard.id = allocateId();
shard.isShard = true; // Avoid the exception in disallowUnshardedSaves().
return shard;
}}));
}});
public void vrun() {
ofy()
.deleteWithoutBackup()
.keys(
ofy()
.load()
.type(SignedMarkRevocationList.class)
.ancestor(getCrossTldKey())
.keys());
ofy()
.saveWithoutBackup()
.entities(
FluentIterable.from(CollectionUtils.partitionMap(revokes, SHARD_SIZE))
.transform(
(ImmutableMap<String, DateTime> shardRevokes) -> {
SignedMarkRevocationList shard =
create(creationTime, shardRevokes);
shard.id = allocateId();
shard.isShard =
true; // Avoid the exception in disallowUnshardedSaves().
return shard;
}));
}
});
return this;
}

View file

@ -23,7 +23,6 @@ import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMap;
import com.googlecode.objectify.Key;
@ -96,54 +95,47 @@ public class ClaimsListShard extends ImmutableObject {
private static final Retrier LOADER_RETRIER = new Retrier(new SystemSleeper(), 2);
private static final Callable<ClaimsListShard> LOADER_CALLABLE =
new Callable<ClaimsListShard>() {
@Override
public ClaimsListShard call() throws Exception {
// Find the most recent revision.
Key<ClaimsListRevision> revisionKey = getCurrentRevision();
() -> {
// Find the most recent revision.
Key<ClaimsListRevision> revisionKey = getCurrentRevision();
Map<String, String> combinedLabelsToKeys = new HashMap<>();
DateTime creationTime = START_OF_TIME;
if (revisionKey != null) {
// Grab all of the keys for the shards that belong to the current revision.
final List<Key<ClaimsListShard>> shardKeys =
ofy().load().type(ClaimsListShard.class).ancestor(revisionKey).keys().list();
Map<String, String> combinedLabelsToKeys = new HashMap<>();
DateTime creationTime = START_OF_TIME;
if (revisionKey != null) {
// Grab all of the keys for the shards that belong to the current revision.
final List<Key<ClaimsListShard>> shardKeys =
ofy().load().type(ClaimsListShard.class).ancestor(revisionKey).keys().list();
// Load all of the shards concurrently, each in a separate transaction.
List<ClaimsListShard> shards =
Concurrent.transform(
shardKeys,
new Function<Key<ClaimsListShard>, ClaimsListShard>() {
@Override
public ClaimsListShard apply(final Key<ClaimsListShard> key) {
return ofy()
.transactNewReadOnly(
new Work<ClaimsListShard>() {
@Override
public ClaimsListShard run() {
ClaimsListShard claimsListShard = ofy().load().key(key).now();
checkState(
claimsListShard != null,
"Key not found when loading claims list shards.");
return claimsListShard;
}
});
}
});
// Load all of the shards concurrently, each in a separate transaction.
List<ClaimsListShard> shards =
Concurrent.transform(
shardKeys,
(final Key<ClaimsListShard> key) ->
ofy()
.transactNewReadOnly(
new Work<ClaimsListShard>() {
@Override
public ClaimsListShard run() {
ClaimsListShard claimsListShard = ofy().load().key(key).now();
checkState(
claimsListShard != null,
"Key not found when loading claims list shards.");
return claimsListShard;
}
}));
// Combine the shards together and return the concatenated ClaimsList.
if (!shards.isEmpty()) {
creationTime = shards.get(0).creationTime;
for (ClaimsListShard shard : shards) {
combinedLabelsToKeys.putAll(shard.labelsToKeys);
checkState(
creationTime.equals(shard.creationTime),
"Inconsistent claims list shard creation times.");
}
// Combine the shards together and return the concatenated ClaimsList.
if (!shards.isEmpty()) {
creationTime = shards.get(0).creationTime;
for (ClaimsListShard shard : shards) {
combinedLabelsToKeys.putAll(shard.labelsToKeys);
checkState(
creationTime.equals(shard.creationTime),
"Inconsistent claims list shard creation times.");
}
}
return create(creationTime, ImmutableMap.copyOf(combinedLabelsToKeys));
}
return create(creationTime, ImmutableMap.copyOf(combinedLabelsToKeys));
};
/**
@ -152,12 +144,7 @@ public class ClaimsListShard extends ImmutableObject {
*/
private static final Supplier<ClaimsListShard> CACHE =
memoizeWithShortExpiration(
new Supplier<ClaimsListShard>() {
@Override
public ClaimsListShard get() {
return LOADER_RETRIER.callWithRetry(LOADER_CALLABLE, IllegalStateException.class);
}
});
() -> LOADER_RETRIER.callWithRetry(LOADER_CALLABLE, IllegalStateException.class));
public DateTime getCreationTime() {
return creationTime;
@ -186,20 +173,21 @@ public class ClaimsListShard extends ImmutableObject {
final Key<ClaimsListRevision> parentKey = ClaimsListRevision.createKey();
// Save the ClaimsList shards in separate transactions.
Concurrent.transform(CollectionUtils.partitionMap(labelsToKeys, shardSize),
new Function<ImmutableMap<String, String>, ClaimsListShard>() {
@Override
public ClaimsListShard apply(final ImmutableMap<String, String> labelsToKeysShard) {
return ofy().transactNew(new Work<ClaimsListShard>() {
@Override
public ClaimsListShard run() {
ClaimsListShard shard = create(creationTime, labelsToKeysShard);
shard.isShard = true;
shard.parent = parentKey;
ofy().saveWithoutBackup().entity(shard);
return shard;
}});
}});
Concurrent.transform(
CollectionUtils.partitionMap(labelsToKeys, shardSize),
(final ImmutableMap<String, String> labelsToKeysShard) ->
ofy()
.transactNew(
new Work<ClaimsListShard>() {
@Override
public ClaimsListShard run() {
ClaimsListShard shard = create(creationTime, labelsToKeysShard);
shard.isShard = true;
shard.parent = parentKey;
ofy().saveWithoutBackup().entity(shard);
return shard;
}
}));
// Persist the new revision, thus causing the newly created shards to go live.
ofy().transactNew(new VoidWork() {

View file

@ -153,9 +153,7 @@ public class EventMetric extends AbstractMetric<Distribution> {
lock.lock();
try {
if (!values.containsKey(labelValues)) {
values.put(labelValues, new MutableDistribution(distributionFitter));
}
values.computeIfAbsent(labelValues, k -> new MutableDistribution(distributionFitter));
values.get(labelValues).add(sample, count);
valueStartTimestamps.putIfAbsent(labelValues, startTimestamp);

View file

@ -14,7 +14,6 @@
package google.registry.monitoring.metrics;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
@ -57,27 +56,24 @@ final class MetricMetrics {
"Count of Timeseries being pushed to Monitoring API",
"Timeseries Count",
LABELS,
new Supplier<ImmutableMap<ImmutableList<String>, Long>>() {
@Override
public ImmutableMap<ImmutableList<String>, Long> get() {
HashMap<ImmutableList<String>, Long> timeseriesCount = new HashMap<>();
() -> {
HashMap<ImmutableList<String>, Long> timeseriesCount = new HashMap<>();
for (Metric<?> metric : MetricRegistryImpl.getDefault().getRegisteredMetrics()) {
ImmutableList<String> key =
ImmutableList.of(
metric.getMetricSchema().kind().toString(),
metric.getValueClass().toString());
for (Metric<?> metric : MetricRegistryImpl.getDefault().getRegisteredMetrics()) {
ImmutableList<String> key =
ImmutableList.of(
metric.getMetricSchema().kind().toString(),
metric.getValueClass().toString());
int cardinality = metric.getCardinality();
if (!timeseriesCount.containsKey(key)) {
timeseriesCount.put(key, (long) cardinality);
} else {
timeseriesCount.put(key, timeseriesCount.get(key) + cardinality);
}
int cardinality = metric.getCardinality();
if (!timeseriesCount.containsKey(key)) {
timeseriesCount.put(key, (long) cardinality);
} else {
timeseriesCount.put(key, timeseriesCount.get(key) + cardinality);
}
return ImmutableMap.copyOf(timeseriesCount);
}
return ImmutableMap.copyOf(timeseriesCount);
},
Long.class);

View file

@ -67,15 +67,11 @@ abstract class AbstractMetricSubject<T, S extends AbstractMetricSubject<T, S>>
* Function to convert a metric point to a nice string representation for use in error messages.
*/
protected final Function<MetricPoint<T>, String> metricPointConverter =
new Function<MetricPoint<T>, String>() {
@Override
public String apply(MetricPoint<T> metricPoint) {
return String.format(
metricPoint ->
String.format(
"%s => %s",
Joiner.on(':').join(metricPoint.labelValues()),
getMessageRepresentation(metricPoint.value()));
}
};
protected AbstractMetricSubject(FailureMetadata metadata, Metric<T> actual) {
super(metadata, checkNotNull(actual));

View file

@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertAbout;
import com.google.common.collect.BoundType;
import com.google.common.collect.Range;
import com.google.common.truth.FailureMetadata;
import com.google.common.truth.Subject;
import google.registry.monitoring.metrics.Distribution;
import google.registry.monitoring.metrics.Metric;
import google.registry.monitoring.metrics.MetricPoint;
@ -48,15 +47,9 @@ public final class DistributionMetricSubject
extends AbstractMetricSubject<Distribution, DistributionMetricSubject> {
/** {@link Subject.Factory} for assertions about {@link Metric<Distribution>} objects. */
private static final Subject.Factory<DistributionMetricSubject, Metric<Distribution>>
SUBJECT_FACTORY =
// The Truth extensibility documentation indicates that the target should be nullable.
(FailureMetadata failureMetadata, @Nullable Metric<Distribution> target) ->
new DistributionMetricSubject(failureMetadata, target);
/** Static assertThat({@link Metric<Distribution>}) shortcut method. */
public static DistributionMetricSubject assertThat(@Nullable Metric<Distribution> metric) {
return assertAbout(SUBJECT_FACTORY).that(metric);
return assertAbout(DistributionMetricSubject::new).that(metric);
}
private DistributionMetricSubject(FailureMetadata metadata, Metric<Distribution> actual) {

View file

@ -17,7 +17,6 @@ package google.registry.monitoring.metrics.contrib;
import static com.google.common.truth.Truth.assertAbout;
import com.google.common.truth.FailureMetadata;
import com.google.common.truth.Subject;
import google.registry.monitoring.metrics.Metric;
import google.registry.monitoring.metrics.MetricPoint;
import javax.annotation.Nullable;
@ -45,14 +44,9 @@ import javax.annotation.Nullable;
public final class LongMetricSubject extends AbstractMetricSubject<Long, LongMetricSubject> {
/** {@link Subject.Factory} for assertions about {@link Metric<Long>} objects. */
private static final Subject.Factory<LongMetricSubject, Metric<Long>> SUBJECT_FACTORY =
// The Truth extensibility documentation indicates that the target should be nullable.
(FailureMetadata failureMetadata, @Nullable Metric<Long> target) ->
new LongMetricSubject(failureMetadata, target);
/** Static assertThat({@link Metric<Long>}) shortcut method. */
public static LongMetricSubject assertThat(@Nullable Metric<Long> metric) {
return assertAbout(SUBJECT_FACTORY).that(metric);
return assertAbout(LongMetricSubject::new).that(metric);
}
private LongMetricSubject(FailureMetadata metadata, Metric<Long> actual) {

View file

@ -19,14 +19,11 @@ import static com.google.common.base.Predicates.not;
import static com.google.common.collect.Multimaps.filterKeys;
import static google.registry.request.Action.Method.POST;
import static google.registry.util.FormattingLogger.getLoggerForCallerClass;
import static java.util.stream.Collectors.joining;
import com.google.api.services.bigquery.Bigquery;
import com.google.api.services.bigquery.model.TableDataInsertAllRequest;
import com.google.api.services.bigquery.model.TableDataInsertAllResponse;
import com.google.api.services.bigquery.model.TableDataInsertAllResponse.InsertErrors;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
@ -85,18 +82,19 @@ public class MetricsExportAction implements Runnable {
.execute();
if (response.getInsertErrors() != null && !response.getInsertErrors().isEmpty()) {
throw new RuntimeException(FluentIterable
.from(response.getInsertErrors())
.transform(new Function<InsertErrors, String>() {
@Override
public String apply(InsertErrors error) {
try {
return error.toPrettyString();
} catch (IOException e) {
return error.toString();
}
}})
.join(Joiner.on('\n')));
throw new RuntimeException(
response
.getInsertErrors()
.stream()
.map(
error -> {
try {
return error.toPrettyString();
} catch (IOException e) {
return error.toString();
}
})
.collect(joining("\n")));
}
} catch (Throwable e) {
logger.warningfmt("Caught Unknown Exception: %s", e);

View file

@ -61,12 +61,7 @@ public class WhiteboxModule {
@Provides
@Named("insertIdGenerator")
static Supplier<String> provideInsertIdGenerator() {
return new Supplier<String>() {
@Override
public String get() {
return UUID.randomUUID().toString();
}
};
return () -> UUID.randomUUID().toString();
}
/** Provides an EppMetric builder with the request ID and startTimestamp already initialized. */

View file

@ -14,16 +14,16 @@
package google.registry.rdap;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.rdap.RdapUtils.getRegistrarByIanaIdentifier;
import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.HEAD;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Streams;
import com.google.common.primitives.Booleans;
import com.google.common.primitives.Longs;
import com.googlecode.objectify.cmd.Query;
@ -156,17 +156,13 @@ public class RdapEntitySearchAction extends RdapActionBase {
}
// Get the registrar matches.
ImmutableList<Registrar> registrars =
FluentIterable.from(Registrar.loadAllCached())
Streams.stream(Registrar.loadAllCached())
.filter(
new Predicate<Registrar>() {
@Override
public boolean apply(Registrar registrar) {
return partialStringQuery.matches(registrar.getRegistrarName())
&& shouldBeVisible(registrar);
}
})
registrar ->
partialStringQuery.matches(registrar.getRegistrarName())
&& shouldBeVisible(registrar))
.limit(rdapResultSetMaxSize + 1)
.toList();
.collect(toImmutableList());
// Get the contact matches and return the results, fetching an additional contact to detect
// truncation. If we are including deleted entries, we must fetch more entries, in case some
// get excluded due to permissioning.

View file

@ -15,12 +15,12 @@
package google.registry.rdap;
import static com.google.common.base.Strings.nullToEmpty;
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.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Optional;
import com.google.common.base.Predicates;
@ -30,6 +30,7 @@ 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;
@ -282,19 +283,11 @@ public class RdapJsonFormatter {
/** Sets the ordering for hosts; just use the fully qualified host name. */
private static final Ordering<HostResource> HOST_RESOURCE_ORDERING =
Ordering.natural().onResultOf(new Function<HostResource, String>() {
@Override
public String apply(HostResource host) {
return host.getFullyQualifiedHostName();
}});
Ordering.natural().onResultOf(HostResource::getFullyQualifiedHostName);
/** Sets the ordering for designated contacts; order them in a fixed order by contact type. */
private static final Ordering<DesignatedContact> DESIGNATED_CONTACT_ORDERING =
Ordering.natural().onResultOf(new Function<DesignatedContact, DesignatedContact.Type>() {
@Override
public DesignatedContact.Type apply(DesignatedContact designatedContact) {
return designatedContact.getType();
}});
Ordering.natural().onResultOf(DesignatedContact::getType);
ImmutableMap<String, Object> getJsonTosNotice(String rdapLinkBase) {
return getJsonHelpNotice(rdapTosPath, rdapLinkBase);
@ -1076,15 +1069,9 @@ public class RdapJsonFormatter {
.filter(Predicates.not(Predicates.equalTo(RdapStatus.ACTIVE)))
.append(RdapStatus.REMOVED);
}
return iterable
.transform(
new Function<RdapStatus, String>() {
@Override
public String apply(RdapStatus status) {
return status.getDisplayName();
}
})
.toSortedSet(Ordering.natural())
return Streams.stream(iterable)
.map(RdapStatus::getDisplayName)
.collect(toImmutableSortedSet(Ordering.natural()))
.asList();
}

View file

@ -17,7 +17,6 @@ package google.registry.rdap;
import static com.google.common.collect.Iterables.tryFind;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import google.registry.model.registrar.Registrar;
/** Utility functions for RDAP. */
@ -29,11 +28,9 @@ public final class RdapUtils {
static Optional<Registrar> getRegistrarByIanaIdentifier(final long ianaIdentifier) {
return tryFind(
Registrar.loadAllCached(),
new Predicate<Registrar>() {
@Override
public boolean apply(Registrar registrar) {
Long registrarIanaIdentifier = registrar.getIanaIdentifier();
return (registrarIanaIdentifier != null) && (registrarIanaIdentifier == ianaIdentifier);
}});
registrar -> {
Long registrarIanaIdentifier = registrar.getIanaIdentifier();
return (registrarIanaIdentifier != null) && (registrarIanaIdentifier == ianaIdentifier);
});
}
}

View file

@ -89,26 +89,30 @@ class EscrowTaskRunner {
Duration timeout,
final CursorType cursorType,
final Duration interval) {
Callable<Void> lockRunner = new Callable<Void>() {
@Override
public Void call() throws Exception {
logger.info("tld=" + registry.getTld());
DateTime startOfToday = clock.nowUtc().withTimeAtStartOfDay();
Cursor cursor = ofy().load().key(Cursor.createKey(cursorType, registry)).now();
final DateTime nextRequiredRun = (cursor == null ? startOfToday : cursor.getCursorTime());
if (nextRequiredRun.isAfter(startOfToday)) {
throw new NoContentException("Already completed");
}
logger.info("cursor=" + nextRequiredRun);
task.runWithLock(nextRequiredRun);
ofy().transact(new VoidWork() {
@Override
public void vrun() {
ofy().save().entity(
Cursor.create(cursorType, nextRequiredRun.plus(interval), registry));
}});
return null;
}};
Callable<Void> lockRunner =
() -> {
logger.info("tld=" + registry.getTld());
DateTime startOfToday = clock.nowUtc().withTimeAtStartOfDay();
Cursor cursor = ofy().load().key(Cursor.createKey(cursorType, registry)).now();
final DateTime nextRequiredRun = (cursor == null ? startOfToday : cursor.getCursorTime());
if (nextRequiredRun.isAfter(startOfToday)) {
throw new NoContentException("Already completed");
}
logger.info("cursor=" + nextRequiredRun);
task.runWithLock(nextRequiredRun);
ofy()
.transact(
new VoidWork() {
@Override
public void vrun() {
ofy()
.save()
.entity(
Cursor.create(cursorType, nextRequiredRun.plus(interval), registry));
}
});
return null;
};
String lockName = String.format("%s %s", task.getClass().getSimpleName(), registry.getTld());
if (!lockHandler.executeWithLocks(lockRunner, tld, timeout, lockName)) {
// This will happen if either: a) the task is double-executed; b) the task takes a long time

View file

@ -23,7 +23,6 @@ import static javax.servlet.http.HttpServletResponse.SC_NO_CONTENT;
import com.google.common.base.Ascii;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
@ -262,16 +261,14 @@ public final class RdeStagingAction implements Runnable {
return ImmutableSetMultimap.copyOf(
Multimaps.filterValues(
pendingDepositChecker.getTldsAndWatermarksPendingDepositForRdeAndBrda(),
new Predicate<PendingDeposit>() {
@Override
public boolean apply(PendingDeposit pending) {
if (clock.nowUtc().isBefore(pending.watermark().plus(transactionCooldown))) {
logger.infofmt("Ignoring within %s cooldown: %s", transactionCooldown, pending);
return false;
} else {
return true;
}
}}));
pending -> {
if (clock.nowUtc().isBefore(pending.watermark().plus(transactionCooldown))) {
logger.infofmt("Ignoring within %s cooldown: %s", transactionCooldown, pending);
return false;
} else {
return true;
}
}));
}
private ImmutableSetMultimap<String, PendingDeposit> getManualPendingDeposits() {

View file

@ -15,18 +15,18 @@
package google.registry.rde;
import static com.google.common.base.Strings.nullToEmpty;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static google.registry.model.EppResourceUtils.loadAtPointInTime;
import static google.registry.model.ofy.ObjectifyService.ofy;
import com.google.appengine.tools.mapreduce.Mapper;
import com.google.auto.value.AutoValue;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Streams;
import com.googlecode.objectify.Result;
import google.registry.model.EppResource;
import google.registry.model.contact.ContactResource;
@ -96,25 +96,16 @@ public final class RdeStagingMapper extends Mapper<EppResource, PendingDeposit,
// Get the set of all point-in-time watermarks we need, to minimize rewinding.
ImmutableSet<DateTime> dates =
FluentIterable
.from(shouldEmitOnAllTlds
? pendings.values()
: pendings.get(((DomainResource) resource).getTld()))
.transform(new Function<PendingDeposit, DateTime>() {
@Override
public DateTime apply(PendingDeposit pending) {
return pending.watermark();
}})
.toSet();
Streams.stream(
shouldEmitOnAllTlds
? pendings.values()
: pendings.get(((DomainResource) resource).getTld()))
.map(PendingDeposit::watermark)
.collect(toImmutableSet());
// Launch asynchronous fetches of point-in-time representations of resource.
ImmutableMap<DateTime, Result<EppResource>> resourceAtTimes =
ImmutableMap.copyOf(Maps.asMap(dates,
new Function<DateTime, Result<EppResource>>() {
@Override
public Result<EppResource> apply(DateTime input) {
return loadAtPointInTime(resource, input);
}}));
ImmutableMap.copyOf(Maps.asMap(dates, input -> loadAtPointInTime(resource, input)));
// Convert resource to an XML fragment for each watermark/mode pair lazily and cache the result.
Fragmenter fragmenter = new Fragmenter(resourceAtTimes);

View file

@ -101,12 +101,11 @@ public final class RdeStagingReducer extends Reducer<PendingDeposit, DepositFrag
@Override
public void reduce(final PendingDeposit key, final ReducerInput<DepositFragment> fragments) {
Callable<Void> lockRunner = new Callable<Void>() {
@Override
public Void call() throws Exception {
reduceWithLock(key, fragments);
return null;
}};
Callable<Void> lockRunner =
() -> {
reduceWithLock(key, fragments);
return null;
};
String lockName = String.format("RdeStaging %s %s", key.tld(), key.mode());
if (!lockHandler.executeWithLocks(lockRunner, null, lockTimeout, lockName)) {
logger.warningfmt("Lock in use: %s", lockName);

View file

@ -59,7 +59,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.concurrent.Callable;
import javax.inject.Inject;
import javax.inject.Named;
import org.bouncycastle.openpgp.PGPKeyPair;
@ -157,12 +156,10 @@ public final class RdeUploadAction implements Runnable, EscrowTask {
verifyFileExists(reportFilename);
final long xmlLength = readXmlLength(xmlLengthFilename);
retrier.callWithRetry(
new Callable<Void>() {
@Override
public Void call() throws Exception {
upload(xmlFilename, xmlLength, watermark, name);
return null;
}},
() -> {
upload(xmlFilename, xmlLength, watermark, name);
return null;
},
JSchException.class);
ofy().transact(new VoidWork() {
@Override

View file

@ -20,12 +20,11 @@ import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.model.registry.Registries.findTldForName;
import static google.registry.util.PipelineUtils.createJobPath;
import static java.util.stream.Collectors.joining;
import com.google.appengine.tools.mapreduce.Mapper;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.net.InternetDomainName;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Work;
@ -196,10 +195,11 @@ public class RdeHostLinkAction implements Runnable {
}
// This is a subordinate host
String domainName =
Joiner.on('.')
.join(
Iterables.skip(
hostName.parts(), hostName.parts().size() - (tld.get().parts().size() + 1)));
hostName
.parts()
.stream()
.skip(hostName.parts().size() - (tld.get().parts().size() + 1))
.collect(joining("."));
DomainResource superordinateDomain = loadByForeignKey(DomainResource.class, domainName, now);
// Hosts can't be linked if domains import hasn't been run
checkState(

View file

@ -16,13 +16,12 @@ package google.registry.rde.imports;
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.ImmutableSet.toImmutableSet;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.rde.imports.RdeImportUtils.generateTridForImport;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.googlecode.objectify.Key;
import google.registry.model.contact.ContactAddress;
import google.registry.model.contact.ContactPhoneNumber;
@ -54,23 +53,6 @@ final class XjcToContactResourceConverter extends XjcToEppResourceConverter {
XmlToEnumMapper.create(PostalInfo.Type.values());
private static final XmlToEnumMapper<TransferStatus> TRANSFER_STATUS_MAPPER =
XmlToEnumMapper.create(TransferStatus.values());
private static final Function<XjcContactIntLocType, PostalInfoChoice> choiceConverter =
new Function<XjcContactIntLocType, PostalInfoChoice>() {
@Override
public PostalInfoChoice apply(XjcContactIntLocType choice) {
return convertPostalInfoChoice(choice);
}
};
private static final Function<XjcContactStatusType, StatusValue> STATUS_VALUE_CONVERTER =
new Function<XjcContactStatusType, StatusValue>() {
@Override
public StatusValue apply(XjcContactStatusType status) {
return convertStatusValue(status);
}
};
/** Converts {@link XjcRdeContact} to {@link ContactResource}. */
static ContactResource convertContact(XjcRdeContact contact) {
ofy().save().entity(
@ -88,11 +70,12 @@ final class XjcToContactResourceConverter extends XjcToEppResourceConverter {
return new ContactResource.Builder()
.setRepoId(contact.getRoid())
.setStatusValues(
FluentIterable.from(contact.getStatuses())
.transform(STATUS_VALUE_CONVERTER)
// LINKED is implicit and should not be imported onto the new contact.
contact
.getStatuses()
.stream()
.map(XjcToContactResourceConverter::convertStatusValue)
.filter(not(equalTo(StatusValue.LINKED)))
.toSet())
.collect(toImmutableSet()))
.setLocalizedPostalInfo(
getPostalInfoOfType(contact.getPostalInfos(), XjcContactPostalInfoEnumType.LOC))
.setInternationalizedPostalInfo(
@ -160,9 +143,24 @@ final class XjcToContactResourceConverter extends XjcToEppResourceConverter {
}
return new Disclose.Builder()
.setFlag(disclose.isFlag())
.setNames(ImmutableList.copyOf(Lists.transform(disclose.getNames(), choiceConverter)))
.setOrgs(ImmutableList.copyOf(Lists.transform(disclose.getOrgs(), choiceConverter)))
.setAddrs(ImmutableList.copyOf(Lists.transform(disclose.getAddrs(), choiceConverter)))
.setNames(
disclose
.getNames()
.stream()
.map(XjcToContactResourceConverter::convertPostalInfoChoice)
.collect(toImmutableList()))
.setOrgs(
disclose
.getOrgs()
.stream()
.map(XjcToContactResourceConverter::convertPostalInfoChoice)
.collect(toImmutableList()))
.setAddrs(
disclose
.getAddrs()
.stream()
.map(XjcToContactResourceConverter::convertPostalInfoChoice)
.collect(toImmutableList()))
.build();
}

View file

@ -16,7 +16,7 @@ package google.registry.rde.imports;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static google.registry.util.DomainNameUtils.canonicalizeDomainName;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import static org.joda.time.DateTimeZone.UTC;
@ -24,7 +24,6 @@ import static org.joda.time.DateTimeZone.UTC;
import com.google.common.base.Ascii;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.net.InternetDomainName;
import com.googlecode.objectify.Key;
import google.registry.model.billing.BillingEvent;
@ -80,44 +79,17 @@ final class XjcToDomainResourceConverter extends XjcToEppResourceConverter {
private static final XmlToEnumMapper<TransferStatus> TRANSFER_STATUS_MAPPER =
XmlToEnumMapper.create(TransferStatus.values());
private static final Function<XjcDomainStatusType, StatusValue> STATUS_CONVERTER =
new Function<XjcDomainStatusType, StatusValue>() {
@Override
public StatusValue apply(XjcDomainStatusType status) {
return convertStatusType(status);
}
};
private static final Function<String, Key<HostResource>> HOST_OBJ_CONVERTER =
new Function<String, Key<HostResource>>() {
@Override
public Key<HostResource> apply(String fullyQualifiedHostName) {
// host names are always lower case
fullyQualifiedHostName = canonicalizeDomainName(fullyQualifiedHostName);
Key<HostResource> key =
ForeignKeyIndex.loadAndGetKey(
HostResource.class, fullyQualifiedHostName, DateTime.now(UTC));
checkState(
key != null,
String.format("HostResource not found with name '%s'", fullyQualifiedHostName));
return key;
}
};
private static final Function<XjcDomainContactType, DesignatedContact> CONTACT_CONVERTER =
new Function<XjcDomainContactType, DesignatedContact>() {
@Override
public DesignatedContact apply(XjcDomainContactType contact) {
return convertContactType(contact);
}
};
private static final Function<XjcSecdnsDsDataType, DelegationSignerData> SECDNS_CONVERTER =
new Function<XjcSecdnsDsDataType, DelegationSignerData>() {
@Override
public DelegationSignerData apply(XjcSecdnsDsDataType secdns) {
return convertSecdnsDsDataType(secdns);
}
fullyQualifiedHostName -> {
// host names are always lower case
fullyQualifiedHostName = canonicalizeDomainName(fullyQualifiedHostName);
Key<HostResource> key =
ForeignKeyIndex.loadAndGetKey(
HostResource.class, fullyQualifiedHostName, DateTime.now(UTC));
checkState(
key != null,
String.format("HostResource not found with name '%s'", fullyQualifiedHostName));
return key;
};
/** Converts {@link XjcRgpStatusType} to {@link GracePeriod} */
@ -198,21 +170,40 @@ final class XjcToDomainResourceConverter extends XjcToEppResourceConverter {
.setLastEppUpdateTime(domain.getUpDate())
.setLastEppUpdateClientId(domain.getUpRr() == null ? null : domain.getUpRr().getValue())
.setLastTransferTime(domain.getTrDate())
.setStatusValues(ImmutableSet.copyOf(transform(domain.getStatuses(), STATUS_CONVERTER)))
.setStatusValues(
domain
.getStatuses()
.stream()
.map(XjcToDomainResourceConverter::convertStatusType)
.collect(toImmutableSet()))
.setNameservers(convertNameservers(domain.getNs()))
.setGracePeriods(
ImmutableSet.copyOf(transform(domain.getRgpStatuses(), gracePeriodConverter)))
.setContacts(ImmutableSet.copyOf(transform(domain.getContacts(), CONTACT_CONVERTER)))
domain
.getRgpStatuses()
.stream()
.map(gracePeriodConverter)
.collect(toImmutableSet()))
.setContacts(
domain
.getContacts()
.stream()
.map(XjcToDomainResourceConverter::convertContactType)
.collect(toImmutableSet()))
.setDsData(
domain.getSecDNS() == null
? ImmutableSet.<DelegationSignerData>of()
: ImmutableSet.copyOf(
transform(domain.getSecDNS().getDsDatas(), SECDNS_CONVERTER)))
: domain
.getSecDNS()
.getDsDatas()
.stream()
.map(XjcToDomainResourceConverter::convertSecdnsDsDataType)
.collect(toImmutableSet()))
.setTransferData(convertDomainTransferData(domain.getTrnData()))
// authInfo pw must be a token between 6 and 16 characters in length
// generate a token of 16 characters as the default authInfo pw
.setAuthInfo(DomainAuthInfo
.create(PasswordAuth.create(stringGenerator.createString(16), domain.getRoid())));
.setAuthInfo(
DomainAuthInfo.create(
PasswordAuth.create(stringGenerator.createString(16), domain.getRoid())));
checkArgumentNotNull(
domain.getRegistrant(), "Registrant is missing for domain '%s'", domain.getName());
builder = builder.setRegistrant(convertRegistrant(domain.getRegistrant()));
@ -237,7 +228,7 @@ final class XjcToDomainResourceConverter extends XjcToEppResourceConverter {
checkArgument(
ns.getHostAttrs() == null || ns.getHostAttrs().isEmpty(),
"Host attributes are not yet supported");
return ImmutableSet.copyOf(Iterables.transform(ns.getHostObjs(), HOST_OBJ_CONVERTER));
return ns.getHostObjs().stream().map(HOST_OBJ_CONVERTER).collect(toImmutableSet());
}
/** Converts {@link XjcRdeDomainTransferDataType} to {@link TransferData}. */

View file

@ -16,14 +16,12 @@ package google.registry.rde.imports;
import static com.google.common.base.Predicates.in;
import static com.google.common.base.Predicates.not;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.rde.imports.RdeImportUtils.generateTridForImport;
import static google.registry.util.DomainNameUtils.canonicalizeDomainName;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.net.InetAddresses;
import com.googlecode.objectify.Key;
import google.registry.model.eppcommon.StatusValue;
@ -37,23 +35,6 @@ import java.net.InetAddress;
/** Utility class that converts an {@link XjcRdeHost} into a {@link HostResource}. */
public class XjcToHostResourceConverter extends XjcToEppResourceConverter {
private static final Function<XjcHostStatusType, StatusValue> STATUS_VALUE_CONVERTER =
new Function<XjcHostStatusType, StatusValue>() {
@Override
public StatusValue apply(XjcHostStatusType status) {
return convertStatusType(status);
}
};
private static final Function<XjcHostAddrType, InetAddress> ADDR_CONVERTER =
new Function<XjcHostAddrType, InetAddress>() {
@Override
public InetAddress apply(XjcHostAddrType addr) {
return convertAddrType(addr);
}
};
static HostResource convert(XjcRdeHost host) {
// TODO(b/35384052): Handle subordinate hosts correctly by setting superordinateDomaina and
// lastSuperordinateChange fields.
@ -81,13 +62,16 @@ public class XjcToHostResourceConverter extends XjcToEppResourceConverter {
.setCreationClientId(host.getCrRr().getValue())
.setLastEppUpdateClientId(host.getUpRr() == null ? null : host.getUpRr().getValue())
.setStatusValues(
FluentIterable.from(host.getStatuses())
.transform(STATUS_VALUE_CONVERTER)
// LINKED is implicit and should not be imported onto the new host.
// PENDING_TRANSFER is a property of the superordinate host.
host.getStatuses()
.stream()
.map(XjcToHostResourceConverter::convertStatusType)
.filter(not(in(ImmutableSet.of(StatusValue.LINKED, StatusValue.PENDING_TRANSFER))))
.toSet())
.setInetAddresses(ImmutableSet.copyOf(Lists.transform(host.getAddrs(), ADDR_CONVERTER)))
.collect(toImmutableSet()))
.setInetAddresses(
host.getAddrs()
.stream()
.map(XjcToHostResourceConverter::convertAddrType)
.collect(toImmutableSet()))
.build();
}

View file

@ -22,7 +22,6 @@ import static javax.servlet.http.HttpServletResponse.SC_OK;
import com.google.api.services.bigquery.model.TableFieldSchema;
import com.google.appengine.tools.cloudstorage.GcsFilename;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.google.common.collect.ArrayListMultimap;
@ -130,16 +129,7 @@ public final class IcannReportingStagingAction implements Runnable {
}
private Iterable<String> getHeaders(ImmutableSet<TableFieldSchema> fields) {
return Iterables.transform(
fields,
new Function<TableFieldSchema, String>() {
@Override
public String apply(TableFieldSchema schema) {
// Change from '_' delimiters (Bigquery-compatible) to '-' (ICANN specification)
return schema.getName().replace('_', '-');
}
}
);
return Iterables.transform(fields, schema -> schema.getName().replace('_', '-'));
}
private void stageActivityReports (

View file

@ -32,11 +32,9 @@ import google.registry.request.Response;
import google.registry.request.auth.Auth;
import google.registry.util.FormattingLogger;
import google.registry.util.Retrier;
import google.registry.xml.XmlException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Callable;
import javax.inject.Inject;
/**
@ -82,16 +80,16 @@ public final class IcannReportingUploadAction implements Runnable {
gcsFilename.getObjectName(),
gcsFilename.getBucketName());
retrier.callWithRetry(new Callable<Void>() {
@Override
public Void call() throws IOException, XmlException {
final byte[] payload = readReportFromGcs(gcsFilename);
icannReporter.send(payload, tld, yearMonth, reportType);
response.setContentType(PLAIN_TEXT_UTF_8);
response.setPayload(
String.format("OK, sending: %s", new String(payload, StandardCharsets.UTF_8)));
return null;
}}, IOException.class);
retrier.callWithRetry(
() -> {
final byte[] payload = readReportFromGcs(gcsFilename);
icannReporter.send(payload, tld, yearMonth, reportType);
response.setContentType(PLAIN_TEXT_UTF_8);
response.setPayload(
String.format("OK, sending: %s", new String(payload, StandardCharsets.UTF_8)));
return null;
},
IOException.class);
}
private byte[] readReportFromGcs(GcsFilename reportFilename) throws IOException {

View file

@ -128,12 +128,7 @@ public final class Modules {
public static final class AppIdentityCredentialModule {
@Provides
static Function<Set<String>, AppIdentityCredential> provideAppIdentityCredential() {
return new Function<Set<String>, AppIdentityCredential>() {
@Override
public AppIdentityCredential apply(Set<String> scopes) {
return new AppIdentityCredential(scopes);
}
};
return AppIdentityCredential::new;
}
}
@ -200,12 +195,7 @@ public final class Modules {
@Provides
static Function<Set<String>, GoogleCredential> provideScopedGoogleCredential(
final Provider<GoogleCredential> googleCredentialProvider) {
return new Function<Set<String>, GoogleCredential>() {
@Override
public GoogleCredential apply(Set<String> scopes) {
return googleCredentialProvider.get().createScoped(scopes);
}
};
return scopes -> googleCredentialProvider.get().createScoped(scopes);
}
/**

View file

@ -95,21 +95,19 @@ final class Router {
}
private static Function<Object, ?> newInstantiator(final Method method) {
return new Function<Object, Object>() {
@Override
public Object apply(Object component) {
try {
return method.invoke(component);
} catch (IllegalAccessException e) {
throw new RuntimeException(
"Error reflectively accessing component's @Action factory method", e);
} catch (InvocationTargetException e) {
// This means an exception was thrown during the injection process while instantiating
// the @Action class; we should propagate that underlying exception.
throwIfUnchecked(e.getCause());
throw new AssertionError(
"Component's @Action factory method somehow threw checked exception", e);
}
}};
return component -> {
try {
return method.invoke(component);
} catch (IllegalAccessException e) {
throw new RuntimeException(
"Error reflectively accessing component's @Action factory method", e);
} catch (InvocationTargetException e) {
// This means an exception was thrown during the injection process while instantiating
// the @Action class; we should propagate that underlying exception.
throwIfUnchecked(e.getCause());
throw new AssertionError(
"Component's @Action factory method somehow threw checked exception", e);
}
};
}
}

View file

@ -14,10 +14,11 @@
package google.registry.request;
import com.google.common.base.Function;
import static java.util.stream.Collectors.joining;
import com.google.common.base.Joiner;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Streams;
import java.util.Map;
/**
@ -139,14 +140,8 @@ public class RouterDisplayHelper {
.build());
return headerToString(formatString)
+ String.format("%n")
+ FluentIterable.from(routes)
.transform(
new Function<Route, String>() {
@Override
public String apply(Route route) {
return routeToString(route, formatString);
}
})
.join(Joiner.on(String.format("%n")));
+ Streams.stream(routes)
.map(route -> routeToString(route, formatString))
.collect(joining(String.format("%n")));
}
}

View file

@ -18,19 +18,18 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.emptyToNull;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.io.BaseEncoding.base16;
import static google.registry.flows.EppXmlTransformer.unmarshal;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.tools.CommandUtilities.addHeader;
import static java.util.stream.Collectors.joining;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.base.Ascii;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.template.soy.data.SoyMapData;
import com.googlecode.objectify.Key;
@ -43,7 +42,6 @@ import google.registry.model.domain.DomainCommand;
import google.registry.model.domain.Period;
import google.registry.model.domain.launch.ApplicationStatus;
import google.registry.model.domain.launch.LaunchNotice;
import google.registry.model.domain.secdns.DelegationSignerData;
import google.registry.model.eppinput.EppInput;
import google.registry.model.eppinput.EppInput.ResourceCommandWrapper;
import google.registry.model.reporting.HistoryEntry;
@ -51,6 +49,7 @@ import google.registry.model.smd.SignedMark;
import google.registry.tools.soy.DomainAllocateSoyInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/** Command to allocated a domain from a domain application. */
@Parameters(separators = " =", commandDescription = "Allocate a domain application")
@ -68,20 +67,33 @@ final class AllocateDomainCommand extends MutatingEppToolCommand {
protected String postExecute() throws Exception {
StringBuilder builder = new StringBuilder();
// Check to see that we allocated everything.
return builder.append(ofy().transactNewReadOnly(new Work<String>() {
@Override
public String run() {
String failureMessage = FluentIterable
.from(ofy().load().keys(applicationKeys).values())
.transform(new Function<DomainApplication, String>() {
@Override
public String apply(DomainApplication application) {
return application.getApplicationStatus() == ApplicationStatus.ALLOCATED
? null : application.getFullyQualifiedDomainName();
}})
.join(Joiner.on('\n').skipNulls());
return failureMessage.isEmpty() ? "ALL SUCCEEDED" : addHeader("FAILURES", failureMessage);
}})).toString();
return builder
.append(
ofy()
.transactNewReadOnly(
new Work<String>() {
@Override
public String run() {
String failureMessage =
ofy()
.load()
.keys(applicationKeys)
.values()
.stream()
.map(
application ->
application.getApplicationStatus()
== ApplicationStatus.ALLOCATED
? null
: application.getFullyQualifiedDomainName())
.filter(Objects::nonNull)
.collect(joining("\n"));
return failureMessage.isEmpty()
? "ALL SUCCEEDED"
: addHeader("FAILURES", failureMessage);
}
}))
.toString();
}
/** Extract the registration period from the XML used to create the domain application. */
@ -96,85 +108,98 @@ final class AllocateDomainCommand extends MutatingEppToolCommand {
protected void initMutatingEppToolCommand() {
checkArgument(superuser, "This command MUST be run as --superuser.");
setSoyTemplate(DomainAllocateSoyInfo.getInstance(), DomainAllocateSoyInfo.CREATE);
ofy().transactNewReadOnly(new VoidWork() {
@Override
public void vrun() {
Iterable<Key<DomainApplication>> keys = transform(
Splitter.on(',').split(ids),
new Function<String, Key<DomainApplication>>() {
ofy()
.transactNewReadOnly(
new VoidWork() {
@Override
public Key<DomainApplication> apply(String applicationId) {
return Key.create(DomainApplication.class, applicationId);
}});
for (DomainApplication application : ofy().load().keys(keys).values()) {
// If the application is already allocated print a warning but do not fail.
if (application.getApplicationStatus() == ApplicationStatus.ALLOCATED) {
System.err.printf(
"Application %s has already been allocated\n", application.getRepoId());
continue;
}
// Ensure domain doesn't already have a final status which it shouldn't be updated from.
checkState(
!application.getApplicationStatus().isFinalStatus(),
"Application has final status %s",
application.getApplicationStatus());
try {
HistoryEntry history = checkNotNull(
ofy().load()
.type(HistoryEntry.class)
.ancestor(checkNotNull(application))
.order("modificationTime")
.first()
.now(),
"Could not find any history entries for domain application %s",
application.getRepoId());
String clientTransactionId =
emptyToNull(history.getTrid().getClientTransactionId());
Period period = checkNotNull(extractPeriodFromXml(history.getXmlBytes()));
checkArgument(period.getUnit() == Period.Unit.YEARS);
ImmutableMap.Builder<String, String> contactsMapBuilder = new ImmutableMap.Builder<>();
for (DesignatedContact contact : application.getContacts()) {
contactsMapBuilder.put(
Ascii.toLowerCase(contact.getType().toString()),
ofy().load().key(contact.getContactKey()).now().getForeignKey());
}
LaunchNotice launchNotice = application.getLaunchNotice();
addSoyRecord(application.getCurrentSponsorClientId(), new SoyMapData(
"name", application.getFullyQualifiedDomainName(),
"period", period.getValue(),
"nameservers", application.loadNameserverFullyQualifiedHostNames(),
"registrant", ofy().load().key(application.getRegistrant()).now().getForeignKey(),
"contacts", contactsMapBuilder.build(),
"authInfo", application.getAuthInfo().getPw().getValue(),
"smdId", application.getEncodedSignedMarks().isEmpty()
? null
: unmarshal(
SignedMark.class,
application.getEncodedSignedMarks().get(0).getBytes()).getId(),
"applicationRoid", application.getRepoId(),
"applicationTime", application.getCreationTime().toString(),
"launchNotice", launchNotice == null ? null : ImmutableMap.of(
"noticeId", launchNotice.getNoticeId().getTcnId(),
"expirationTime", launchNotice.getExpirationTime().toString(),
"acceptedTime", launchNotice.getAcceptedTime().toString()),
"dsRecords", FluentIterable.from(application.getDsData())
.transform(new Function<DelegationSignerData, ImmutableMap<String, ?>>() {
@Override
public ImmutableMap<String, ?> apply(DelegationSignerData dsData) {
return ImmutableMap.of(
"keyTag", dsData.getKeyTag(),
"algorithm", dsData.getAlgorithm(),
"digestType", dsData.getDigestType(),
"digest", base16().encode(dsData.getDigest()));
}})
.toList(),
"clTrid", clientTransactionId));
applicationKeys.add(Key.create(application));
} catch (EppException e) {
throw new RuntimeException(e);
}
}
}
});
public void vrun() {
Iterable<Key<DomainApplication>> keys =
transform(
Splitter.on(',').split(ids),
applicationId -> Key.create(DomainApplication.class, applicationId));
for (DomainApplication application : ofy().load().keys(keys).values()) {
// If the application is already allocated print a warning but do not fail.
if (application.getApplicationStatus() == ApplicationStatus.ALLOCATED) {
System.err.printf(
"Application %s has already been allocated\n", application.getRepoId());
continue;
}
// Ensure domain doesn't already have a final status which it shouldn't be updated
// from.
checkState(
!application.getApplicationStatus().isFinalStatus(),
"Application has final status %s",
application.getApplicationStatus());
try {
HistoryEntry history =
checkNotNull(
ofy()
.load()
.type(HistoryEntry.class)
.ancestor(checkNotNull(application))
.order("modificationTime")
.first()
.now(),
"Could not find any history entries for domain application %s",
application.getRepoId());
String clientTransactionId =
emptyToNull(history.getTrid().getClientTransactionId());
Period period = checkNotNull(extractPeriodFromXml(history.getXmlBytes()));
checkArgument(period.getUnit() == Period.Unit.YEARS);
ImmutableMap.Builder<String, String> contactsMapBuilder =
new ImmutableMap.Builder<>();
for (DesignatedContact contact : application.getContacts()) {
contactsMapBuilder.put(
Ascii.toLowerCase(contact.getType().toString()),
ofy().load().key(contact.getContactKey()).now().getForeignKey());
}
LaunchNotice launchNotice = application.getLaunchNotice();
addSoyRecord(
application.getCurrentSponsorClientId(),
new SoyMapData(
"name", application.getFullyQualifiedDomainName(),
"period", period.getValue(),
"nameservers", application.loadNameserverFullyQualifiedHostNames(),
"registrant",
ofy().load().key(application.getRegistrant()).now().getForeignKey(),
"contacts", contactsMapBuilder.build(),
"authInfo", application.getAuthInfo().getPw().getValue(),
"smdId",
application.getEncodedSignedMarks().isEmpty()
? null
: unmarshal(
SignedMark.class,
application.getEncodedSignedMarks().get(0).getBytes())
.getId(),
"applicationRoid", application.getRepoId(),
"applicationTime", application.getCreationTime().toString(),
"launchNotice",
launchNotice == null
? null
: ImmutableMap.of(
"noticeId", launchNotice.getNoticeId().getTcnId(),
"expirationTime",
launchNotice.getExpirationTime().toString(),
"acceptedTime", launchNotice.getAcceptedTime().toString()),
"dsRecords",
application
.getDsData()
.stream()
.map(
dsData ->
ImmutableMap.of(
"keyTag", dsData.getKeyTag(),
"algorithm", dsData.getAlgorithm(),
"digestType", dsData.getDigestType(),
"digest", base16().encode(dsData.getDigest())))
.collect(toImmutableList()),
"clTrid", clientTransactionId));
applicationKeys.add(Key.create(application));
} catch (EppException e) {
throw new RuntimeException(e);
}
}
}
});
}
}

View file

@ -63,11 +63,7 @@ class AppEngineConnection implements Connection {
* <p>Computing this is expensive since it needs to load {@code ServerSecret} so do it once.
*/
private final Supplier<String> xsrfToken =
memoize(new Supplier<String>() {
@Override
public String get() {
return xsrfTokenManager.generateToken(getUserId());
}});
memoize(() -> xsrfTokenManager.generateToken(getUserId()));
@Override
public void prefetchXsrfToken() throws IOException {

View file

@ -60,28 +60,34 @@ final class AuctionStatusCommand implements RemoteApiCommand {
@Override
public void run() throws Exception {
final ImmutableSet<String> domains = ImmutableSet.copyOf(mainArguments);
Files.write(output, FluentIterable
.from(domains)
.transformAndConcat(new Function<String, Iterable<String>>() {
@Override
public Iterable<String> apply(String fullyQualifiedDomainName) {
checkState(
findTldForName(InternetDomainName.from(fullyQualifiedDomainName)).isPresent(),
"No tld found for %s", fullyQualifiedDomainName);
return ofy().transactNewReadOnly(new Work<Iterable<String>>() {
@Override
public Iterable<String> run() {
ImmutableList.Builder<DomainApplication> applications =
new ImmutableList.Builder<>();
for (String domain : domains) {
applications.addAll(
loadActiveApplicationsByDomainName(domain, ofy().getTransactionTime()));
}
return Lists.transform(
FluentIterable.from(applications.build()).toSortedList(ORDERING),
APPLICATION_FORMATTER);
}});
}}), UTF_8);
Files.write(
output,
FluentIterable.from(domains)
.transformAndConcat(
fullyQualifiedDomainName -> {
checkState(
findTldForName(InternetDomainName.from(fullyQualifiedDomainName)).isPresent(),
"No tld found for %s",
fullyQualifiedDomainName);
return ofy()
.transactNewReadOnly(
new Work<Iterable<String>>() {
@Override
public Iterable<String> run() {
ImmutableList.Builder<DomainApplication> applications =
new ImmutableList.Builder<>();
for (String domain : domains) {
applications.addAll(
loadActiveApplicationsByDomainName(
domain, ofy().getTransactionTime()));
}
return Lists.transform(
ImmutableList.sortedCopyOf(ORDERING, applications.build()),
APPLICATION_FORMATTER);
}
});
}),
UTF_8);
}
private static final Ordering<DomainApplication> ORDERING = new Ordering<DomainApplication>() {
@ -97,11 +103,10 @@ final class AuctionStatusCommand implements RemoteApiCommand {
}};
private static final Function<DomainApplication, String> APPLICATION_FORMATTER =
new Function<DomainApplication, String>() {
@Override
public String apply(DomainApplication app) {
ContactResource registrant = checkNotNull(ofy().load().key(app.getRegistrant()).now());
Object[] keysAndValues = new Object[] {
app -> {
ContactResource registrant = checkNotNull(ofy().load().key(app.getRegistrant()).now());
Object[] keysAndValues =
new Object[] {
"Domain", app.getFullyQualifiedDomainName(),
"Type", app.getEncodedSignedMarks().isEmpty() ? "Landrush" : "Sunrise",
"Application Status", app.getApplicationStatus(),
@ -111,8 +116,8 @@ final class AuctionStatusCommand implements RemoteApiCommand {
"Registrar Name", app.getCurrentSponsorClientId(),
"Registrant Email", registrant.getEmailAddress(),
"Registrant Phone", registrant.getVoiceNumber().getPhoneNumber()
};
return String.format(
Strings.repeat("%-25s= %s\n", keysAndValues.length / 2), keysAndValues);
}};
};
return String.format(
Strings.repeat("%-25s= %s\n", keysAndValues.length / 2), keysAndValues);
};
}

View file

@ -17,6 +17,7 @@ package google.registry.tools;
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static google.registry.model.registry.Registries.assertTldExists;
import static google.registry.util.PreconditionsUtils.checkArgumentPresent;
import static org.joda.time.DateTimeZone.UTC;
@ -25,9 +26,9 @@ import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Streams;
import com.google.re2j.Matcher;
import com.google.re2j.Pattern;
import google.registry.model.billing.RegistrarCredit;
@ -42,6 +43,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;
import org.joda.money.BigMoney;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
@ -107,31 +109,25 @@ final class CreateAuctionCreditsCommand extends MutatingCommand {
CURRENCY_CODE;
public static List<String> getHeaders() {
return FluentIterable.from(values())
.transform(new Function<CsvHeader, String>() {
@Override
public String apply(CsvHeader header) {
// Returns the name of the header as it appears in the CSV file.
return UPPER_UNDERSCORE.to(UPPER_CAMEL, header.name());
}})
.toList();
return Stream.of(values())
.map(header -> UPPER_UNDERSCORE.to(UPPER_CAMEL, header.name()))
.collect(toImmutableList());
}
}
private static final Pattern QUOTED_STRING = Pattern.compile("\"(.*)\"");
/** Helper function to unwrap a quoted string, failing if the string is not quoted. */
private static final Function<String, String> UNQUOTER = new Function<String, String>() {
@Override
public String apply(String input) {
Matcher matcher = QUOTED_STRING.matcher(input);
checkArgument(matcher.matches(), "Input not quoted");
return matcher.group(1);
}};
private static final Function<String, String> UNQUOTER =
input -> {
Matcher matcher = QUOTED_STRING.matcher(input);
checkArgument(matcher.matches(), "Input not quoted");
return matcher.group(1);
};
/** Returns the input string of quoted CSV values split into the list of unquoted values. */
private static List<String> splitCsvLine(String line) {
return FluentIterable.from(Splitter.on(',').split(line)).transform(UNQUOTER).toList();
return Streams.stream(Splitter.on(',').split(line)).map(UNQUOTER).collect(toImmutableList());
}
@Override

View file

@ -16,6 +16,7 @@ package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Sets.difference;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.model.registry.Registries.assertTldsExist;
@ -27,9 +28,7 @@ import com.beust.jcommander.Parameters;
import com.google.appengine.tools.remoteapi.RemoteApiException;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
@ -50,7 +49,6 @@ import java.io.StringReader;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import javax.inject.Inject;
/**
@ -164,12 +162,12 @@ public class CreateLrpTokensCommand implements RemoteApiCommand {
}
final ImmutableSet<LrpTokenEntity> tokensToSave = tokensToSaveBuilder.build();
// Wrap in a retrier to deal with transient 404 errors (thrown as RemoteApiExceptions).
retrier.callWithRetry(new Callable<Void>() {
@Override
public Void call() throws Exception {
saveTokens(tokensToSave);
return null;
}}, RemoteApiException.class);
retrier.callWithRetry(
() -> {
saveTokens(tokensToSave);
return null;
},
RemoteApiException.class);
} while (line != null);
}
@ -196,21 +194,19 @@ public class CreateLrpTokensCommand implements RemoteApiCommand {
private ImmutableSet<String> generateTokens(int count) {
final ImmutableSet<String> candidates =
ImmutableSet.copyOf(TokenUtils.createTokens(LRP, stringGenerator, count));
ImmutableSet<Key<LrpTokenEntity>> existingTokenKeys = FluentIterable.from(candidates)
.transform(new Function<String, Key<LrpTokenEntity>>() {
@Override
public Key<LrpTokenEntity> apply(String input) {
return Key.create(LrpTokenEntity.class, input);
}})
.toSet();
ImmutableSet<String> existingTokenStrings = FluentIterable
.from(ofy().load().keys(existingTokenKeys).values())
.transform(new Function<LrpTokenEntity, String>() {
@Override
public String apply(LrpTokenEntity input) {
return input.getToken();
}})
.toSet();
ImmutableSet<Key<LrpTokenEntity>> existingTokenKeys =
candidates
.stream()
.map(input -> Key.create(LrpTokenEntity.class, input))
.collect(toImmutableSet());
ImmutableSet<String> existingTokenStrings =
ofy()
.load()
.keys(existingTokenKeys)
.values()
.stream()
.map(LrpTokenEntity::getToken)
.collect(toImmutableSet());
return ImmutableSet.copyOf(difference(candidates, existingTokenStrings));
}
}

View file

@ -18,19 +18,17 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Predicates.isNull;
import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static google.registry.util.DomainNameUtils.canonicalizeDomainName;
import static google.registry.util.RegistrarUtils.normalizeRegistrarName;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static org.joda.time.DateTimeZone.UTC;
import com.beust.jcommander.Parameter;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import google.registry.model.billing.RegistrarBillingUtils;
import google.registry.model.registrar.Registrar;
@ -376,8 +374,9 @@ abstract class CreateOrUpdateRegistrarCommand extends MutatingCommand {
builder.setBillingMethod(billingMethod);
}
List<Object> streetAddressFields = Arrays.asList(street, city, state, zip, countryCode);
checkArgument(Iterables.any(streetAddressFields, isNull())
== Iterables.all(streetAddressFields, isNull()),
checkArgument(
streetAddressFields.stream().anyMatch(isNull())
== streetAddressFields.stream().allMatch(isNull()),
"Must specify all fields of address");
if (street != null) {
// We always set the localized address for now. That should be safe to do since it supports
@ -432,15 +431,11 @@ abstract class CreateOrUpdateRegistrarCommand extends MutatingCommand {
// Check if registrar has billing account IDs for the currency of the TLDs that it is
// allowed to register.
ImmutableSet<CurrencyUnit> tldCurrencies =
FluentIterable.from(newRegistrar.getAllowedTlds())
.transform(
new Function<String, CurrencyUnit>() {
@Override
public CurrencyUnit apply(String tld) {
return Registry.get(tld).getCurrency();
}
})
.toSet();
newRegistrar
.getAllowedTlds()
.stream()
.map(tld -> Registry.get(tld).getCurrency())
.collect(toImmutableSet());
Set<CurrencyUnit> currenciesWithoutBillingAccountId =
newRegistrar.getBillingAccountMap() == null
? tldCurrencies

View file

@ -17,22 +17,22 @@ package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.emptyToNull;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.collect.Lists.newArrayList;
import static google.registry.model.registrar.Registrar.State.ACTIVE;
import static google.registry.tools.RegistryToolEnvironment.PRODUCTION;
import static google.registry.tools.RegistryToolEnvironment.SANDBOX;
import static google.registry.tools.RegistryToolEnvironment.UNITTEST;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import static google.registry.util.RegistrarUtils.normalizeClientId;
import static java.util.stream.Collectors.toCollection;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import google.registry.model.registrar.Registrar;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
@ -82,15 +82,9 @@ final class CreateRegistrarCommand extends CreateOrUpdateRegistrarCommand
checkState(
!Registrar.loadByClientId(clientId).isPresent(), "Registrar %s already exists", clientId);
List<Registrar> collisions =
newArrayList(
filter(
Registrar.loadAll(),
new Predicate<Registrar>() {
@Override
public boolean apply(Registrar registrar) {
return normalizeClientId(registrar.getClientId()).equals(clientId);
}
}));
Streams.stream(Registrar.loadAll())
.filter(registrar -> normalizeClientId(registrar.getClientId()).equals(clientId))
.collect(toCollection(ArrayList::new));
if (!collisions.isEmpty()) {
throw new IllegalArgumentException(String.format(
"The registrar client identifier %s normalizes identically to existing registrar %s",

View file

@ -14,13 +14,11 @@
package google.registry.tools;
import static com.google.common.collect.Iterables.transform;
import static google.registry.util.PreconditionsUtils.checkArgumentPresent;
import static java.util.stream.Collectors.joining;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.MediaType;
import google.registry.model.registrar.Registrar;
@ -64,11 +62,7 @@ public class CreateRegistrarGroupsCommand extends ConfirmingCommand
protected String prompt() {
return String.format(
"Create registrar contact groups for registrar(s) %s?",
Joiner.on(", ").join(transform(registrars, new Function<Registrar, String>() {
@Override
public String apply(Registrar registrar) {
return registrar.getRegistrarName();
}})));
registrars.stream().map(Registrar::getRegistrarName).collect(joining(", ")));
}
/** Calls the server endpoint to create groups for the specified registrar client id. */

Some files were not shown because too many files have changed in this diff Show more