Source:
private Future<Optional<String>> sendEmail(EmailNotificationType emailNotificationType, Charge charge, GatewayAccountEntity gatewayAccountEntity, HashMap<String, String> personalisation) { boolean isEmailEnabled = ofNullable(gatewayAccountEntity.getEmailNotifications().get(emailNotificationType)) .map(EmailNotificationEntity::isEnabled) .orElse(false); if (!emailNotifyGloballyEnabled || !isEmailEnabled || gatewayAccountEntity.getEmailCollectionMode().equals(OFF) || gatewayAccountEntity.getEmailCollectionMode().equals(OPTIONAL) && ofNullable(charge.getEmail()).isEmpty()) { return CompletableFuture.completedFuture(Optional.empty()); } if (charge.getEmail() == null || charge.getEmail().isEmpty()) { logger.warn("Cannot send email for charge_external_id = because the charge does not have an email address", charge.getExternalId()); return CompletableFuture.completedFuture(Optional.empty()); } Stopwatch responseTimeStopwatch = Stopwatch.createStarted(); return executorService.submit(() -> { try { NotifyClientSettings notifyClientSettings = getNotifyClientSettings(emailNotificationType, gatewayAccountEntity); logger.info("Sending email, charge_external_id={}", emailNotificationType, charge.getExternalId()); SendEmailResponse response = notifyClientSettings.getClient() .sendEmail(notifyClientSettings.getTemplateId(), charge.getEmail(), personalisation, null); return Optional.of(response.getNotificationId().toString()); } catch (NotificationClientException e) { logger.error("Failed to send " + emailNotificationType + " email - charge_external_id=" + charge.getExternalId(), e); metricRegistry.counter("notify-operations.failures").inc(); return Optional.empty(); } finally { responseTimeStopwatch.stop(); metricRegistry.histogram("notify-operations.response_time").update(responseTimeStopwatch.elapsed(TimeUnit.MILLISECONDS)); } }); }

Target:
private Future<Optional<String>> sendEmail(EmailNotificationType emailNotificationType, Charge charge, GatewayAccountEntity gatewayAccountEntity, HashMap<String, String> personalisation) { boolean isEmailEnabled = ofNullable(gatewayAccountEntity.getEmailNotifications().get(emailNotificationType)) .map(EmailNotificationEntity::isEnabled) .orElse(false); if (!emailNotifyGloballyEnabled || !isEmailEnabled || gatewayAccountEntity.getEmailCollectionMode().equals(OFF) || gatewayAccountEntity.getEmailCollectionMode().equals(OPTIONAL) && ofNullable(charge.getEmail()).isEmpty()) { return CompletableFuture.completedFuture(Optional.empty()); } if (charge.getEmail() == null) { logger.warn("Cannot send email for charge_external_id = because the charge does not have an email address", charge.getExternalId()); return CompletableFuture.completedFuture(Optional.empty()); } Stopwatch responseTimeStopwatch = Stopwatch.createStarted(); return executorService.submit(() -> { try { NotifyClientSettings notifyClientSettings = getNotifyClientSettings(emailNotificationType, gatewayAccountEntity); logger.info("Sending email, charge_external_id={}", emailNotificationType, charge.getExternalId()); SendEmailResponse response = notifyClientSettings.getClient() .sendEmail(notifyClientSettings.getTemplateId(), charge.getEmail(), personalisation, null); return Optional.of(response.getNotificationId().toString()); } catch (NotificationClientException e) { logger.error("Failed to send " + emailNotificationType + " email - charge_external_id=" + charge.getExternalId(), e); metricRegistry.counter("notify-operations.failures").inc(); return Optional.empty(); } finally { responseTimeStopwatch.stop(); metricRegistry.histogram("notify-operations.response_time").update(responseTimeStopwatch.elapsed(TimeUnit.MILLISECONDS)); } }); }