From a288f8db7e6187b047baba472d71b44edfa42bb1 Mon Sep 17 00:00:00 2001 From: Azhwani <13301425+azhwani@users.noreply.github.com> Date: Mon, 12 Dec 2022 08:53:16 +0100 Subject: [PATCH] BAEL-5844: Convert Anonymous Class into Lambda in Java (#13088) --- .../anonymousclass/EmailSenderService.java | 10 ++++++ .../com/baeldung/anonymousclass/Sender.java | 7 ++++ .../anonymousclass/SenderService.java | 7 ++++ .../anonymousclass/SmsSenderService.java | 10 ++++++ ...AnonymousClassToLambdaIntegrationTest.java | 34 +++++++++++++++++++ 5 files changed, 68 insertions(+) create mode 100644 core-java-modules/core-java-lambdas/src/main/java/com/baeldung/anonymousclass/EmailSenderService.java create mode 100644 core-java-modules/core-java-lambdas/src/main/java/com/baeldung/anonymousclass/Sender.java create mode 100644 core-java-modules/core-java-lambdas/src/main/java/com/baeldung/anonymousclass/SenderService.java create mode 100644 core-java-modules/core-java-lambdas/src/main/java/com/baeldung/anonymousclass/SmsSenderService.java create mode 100644 core-java-modules/core-java-lambdas/src/test/java/com/baeldung/anonymousclass/AnonymousClassToLambdaIntegrationTest.java diff --git a/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/anonymousclass/EmailSenderService.java b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/anonymousclass/EmailSenderService.java new file mode 100644 index 0000000000..f0c41b1cc9 --- /dev/null +++ b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/anonymousclass/EmailSenderService.java @@ -0,0 +1,10 @@ +package com.baeldung.anonymousclass; + +public class EmailSenderService implements SenderService { + + @Override + public String callSender(Sender sender) { + return sender.send("Email Notification"); + } + +} diff --git a/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/anonymousclass/Sender.java b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/anonymousclass/Sender.java new file mode 100644 index 0000000000..9e59960c14 --- /dev/null +++ b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/anonymousclass/Sender.java @@ -0,0 +1,7 @@ +package com.baeldung.anonymousclass; + +public interface Sender { + + String send(final String message); + +} diff --git a/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/anonymousclass/SenderService.java b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/anonymousclass/SenderService.java new file mode 100644 index 0000000000..cbb0977f89 --- /dev/null +++ b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/anonymousclass/SenderService.java @@ -0,0 +1,7 @@ +package com.baeldung.anonymousclass; + +public interface SenderService { + + String callSender(Sender sender); + +} diff --git a/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/anonymousclass/SmsSenderService.java b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/anonymousclass/SmsSenderService.java new file mode 100644 index 0000000000..9c84664ec4 --- /dev/null +++ b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/anonymousclass/SmsSenderService.java @@ -0,0 +1,10 @@ +package com.baeldung.anonymousclass; + +public class SmsSenderService implements SenderService { + + @Override + public String callSender(Sender sender) { + return sender.send("SMS Notification"); + } + +} diff --git a/core-java-modules/core-java-lambdas/src/test/java/com/baeldung/anonymousclass/AnonymousClassToLambdaIntegrationTest.java b/core-java-modules/core-java-lambdas/src/test/java/com/baeldung/anonymousclass/AnonymousClassToLambdaIntegrationTest.java new file mode 100644 index 0000000000..b329e003f4 --- /dev/null +++ b/core-java-modules/core-java-lambdas/src/test/java/com/baeldung/anonymousclass/AnonymousClassToLambdaIntegrationTest.java @@ -0,0 +1,34 @@ +package com.baeldung.anonymousclass; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class AnonymousClassToLambdaIntegrationTest { + + @Test + public void whenPassingAnonymousClass_thenSuccess() { + final SenderService emailSenderService = new EmailSenderService(); + + final String emailNotif = emailSenderService.callSender(new Sender() { + @Override + public String send(String message) { + return message; + } + }); + + assertEquals(emailNotif, "Email Notification"); + } + + @Test + public void whenPassingLambdaExpression_thenSuccess() { + final SenderService smsSenderService = new SmsSenderService(); + + final String smsNotif = smsSenderService.callSender((String message) -> { + return message; + }); + + assertEquals(smsNotif, "SMS Notification"); + } + +}