Issue
Is there a way this method could be modified into a lambda function?
public static <T> void checkResponse(Response<T> response, String errorMessage, Map<String,String> values) throws IOException {
if (!response.isSuccessful()) {
String message = StringSubstitutor.replace(errorMessage, values);
logger.error(message);
throw new RuntimeException(message, null);
}
}
Solution
First, define an interface with the appropriate parameters, then you can use a lambda to implement it.
@FunctionalInterface
interface ResponseHandler<T> {
void handleResponse(Response<T> response, String errorMessage, Map<String,String> values);
}
// ...
ResponseHandler handler = (response, errorMessage, values) -> {
if (!response.isSuccessful()) {
String message = StringSubstitutor.replace(errorMessage, values);
logger.error(message);
throw new RuntimeException(message, null);
}
};
// Call it:
handler.handleResponse(...);
Answered By - Unmitigated
Answer Checked By - Candace Johnson (JavaFixing Volunteer)