Add Runnable overrides to ease use of Java 8 language features

Runnable and Callable are both @FunctionalInterfaces. The difference is
that Callable requires a return value whereas Runnable does not, so in
situations where we don't care about a return value, rather than having to
add an unnecessary 'return null;' at the end of the lambda, we can simply
use a non-returning Runnable instead.

Unfortunately, owing to legacy reasons, Runnable is not declared to throw
checked exceptions whereas Callable is, so in situations where checked
exceptions are thrown we still need to have a 'return null;' call at the
end.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=172935400
This commit is contained in:
mcilwain 2017-10-20 14:37:48 -07:00 committed by jianglai
parent d577a281b8
commit 1790914058
13 changed files with 141 additions and 134 deletions

View file

@ -153,25 +153,27 @@ public class RestoreCommitLogsAction implements Runnable {
try {
deleteResult.now();
} catch (Exception e) {
retry(() -> deleteAsync(manifest.getDeletions()).now());
retrier.callWithRetry(
() -> deleteAsync(manifest.getDeletions()).now(), RuntimeException.class);
}
return manifest;
}
private void saveRaw(final List<Entity> entitiesToSave) {
private void saveRaw(List<Entity> entitiesToSave) {
if (dryRun) {
logger.infofmt("Would have saved entities: %s", entitiesToSave);
return;
}
retry(() -> datastoreService.put(entitiesToSave));
retrier.callWithRetry(() -> datastoreService.put(entitiesToSave), RuntimeException.class);
}
private void saveOfy(final Iterable<? extends ImmutableObject> objectsToSave) {
private void saveOfy(Iterable<? extends ImmutableObject> objectsToSave) {
if (dryRun) {
logger.infofmt("Would have saved entities: %s", objectsToSave);
return;
}
retry(() -> ofy().saveWithoutBackup().entities(objectsToSave).now());
retrier.callWithRetry(
() -> ofy().saveWithoutBackup().entities(objectsToSave).now(), RuntimeException.class);
}
private Result<?> deleteAsync(Set<Key<?>> keysToDelete) {
@ -183,13 +185,4 @@ public class RestoreCommitLogsAction implements Runnable {
: ofy().deleteWithoutBackup().keys(keysToDelete);
}
/** Retrier for saves and deletes, since we can't proceed with any failures. */
private void retry(final Runnable runnable) {
retrier.callWithRetry(
() -> {
runnable.run();
return null;
},
RuntimeException.class);
}
}