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

@ -119,7 +119,7 @@ public class Retrier implements Serializable {
};
/**
* Retries a unit of work in the face of transient errors.
* Retries a unit of work in the face of transient errors and returns the result.
*
* <p>Retrying is done a fixed number of times, with exponential backoff, if the exception that is
* thrown is on a whitelist of retryable errors. If the error is not on the whitelist, or if the
@ -143,8 +143,20 @@ public class Retrier implements Serializable {
moreRetryableErrors);
}
/** Retries a unit of work in the face of transient errors. */
@SafeVarargs
public final void callWithRetry(
VoidCallable callable,
Class<? extends Throwable> retryableError,
Class<? extends Throwable>... moreRetryableErrors) {
callWithRetry(
callable.asCallable(),
retryableError,
moreRetryableErrors);
}
/**
* Retries a unit of work in the face of transient errors.
* Retries a unit of work in the face of transient errors and returns the result.
*
* <p>Retrying is done a fixed number of times, with exponential backoff, if the exception that is
* thrown is on a whitelist of retryable errors. If the error is not on the whitelist, or if the

View file

@ -0,0 +1,34 @@
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.util;
import java.util.concurrent.Callable;
/**
* A functional interface for a version of {@link Callable} that returns no value.
*/
@FunctionalInterface
public interface VoidCallable {
void call() throws Exception;
/** Returns the VoidCallable as a {@link Callable} that returns null. */
public default Callable<Void> asCallable() {
return () -> {
call();
return null;
};
}
}