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

@ -19,8 +19,6 @@ import static google.registry.model.ofy.ObjectifyService.ofy;
import com.google.appengine.api.users.User;
import com.google.common.base.Splitter;
import com.googlecode.objectify.VoidWork;
import com.googlecode.objectify.Work;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import google.registry.model.ImmutableObject;
@ -54,24 +52,13 @@ public class GaeUserIdConverter extends ImmutableObject {
try {
// Perform these operations in a transactionless context to avoid enlisting in some outer
// transaction (if any).
ofy().doTransactionless(new VoidWork() {
@Override
public void vrun() {
ofy().saveWithoutBackup().entity(gaeUserIdConverter).now();
}});
ofy().doTransactionless(() -> ofy().saveWithoutBackup().entity(gaeUserIdConverter).now());
// The read must be done in its own transaction to avoid reading from the session cache.
return ofy().transactNew(new Work<String>() {
@Override
public String run() {
return ofy().load().entity(gaeUserIdConverter).safe().user.getUserId();
}});
return ofy()
.transactNew(() -> ofy().load().entity(gaeUserIdConverter).safe().user.getUserId());
} finally {
ofy().doTransactionless(new VoidWork() {
@Override
public void vrun() {
ofy().deleteWithoutBackup().entity(gaeUserIdConverter).now();
}});
ofy().doTransactionless(() -> ofy().deleteWithoutBackup().entity(gaeUserIdConverter).now());
}
}
}