From d7e857f427ed9378d92dc250aa83c6672911a28e Mon Sep 17 00:00:00 2001 From: Olu <56642018+olu-damilare@users.noreply.github.com> Date: Fri, 27 Jan 2023 21:37:47 +0100 Subject: [PATCH] BAEL 5932 - Callback Functions in Java (#13335) --- .../core-java-functional/pom.xml | 26 +++++++++++++++++++ .../callbackfunctions/ConsumerCallback.java | 17 ++++++++++++ .../callbackfunctions/EventListener.java | 12 +++++++++ .../AsynchronousEventConsumer.java | 21 +++++++++++++++ .../AsynchronousEventListenerImpl.java | 19 ++++++++++++++ .../synchronous/SynchronousEventConsumer.java | 22 ++++++++++++++++ .../SynchronousEventListenerImpl.java | 18 +++++++++++++ .../AsynchronousCallbackUnitTest.java | 22 ++++++++++++++++ .../ConsumerCallbackUnitTest.java | 20 ++++++++++++++ .../SynchronousCallbackUnitTest.java | 22 ++++++++++++++++ 10 files changed, 199 insertions(+) create mode 100644 core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/ConsumerCallback.java create mode 100644 core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/EventListener.java create mode 100644 core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/asynchronous/AsynchronousEventConsumer.java create mode 100644 core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/asynchronous/AsynchronousEventListenerImpl.java create mode 100644 core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/synchronous/SynchronousEventConsumer.java create mode 100644 core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/synchronous/SynchronousEventListenerImpl.java create mode 100644 core-java-modules/core-java-functional/src/test/java/com/baeldung/callbackfunctions/AsynchronousCallbackUnitTest.java create mode 100644 core-java-modules/core-java-functional/src/test/java/com/baeldung/callbackfunctions/ConsumerCallbackUnitTest.java create mode 100644 core-java-modules/core-java-functional/src/test/java/com/baeldung/callbackfunctions/SynchronousCallbackUnitTest.java diff --git a/core-java-modules/core-java-functional/pom.xml b/core-java-modules/core-java-functional/pom.xml index 52d74035a5..9ad47f8133 100644 --- a/core-java-modules/core-java-functional/pom.xml +++ b/core-java-modules/core-java-functional/pom.xml @@ -14,4 +14,30 @@ 0.0.1-SNAPSHOT + + + org.mockito + mockito-inline + ${mockito-inline.version} + test + + + + + core-java-functional + + + src/main/resources + true + + + + + + 3.8.0 + 3.22.0 + 3.12.0 + 0.10.4 + + \ No newline at end of file diff --git a/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/ConsumerCallback.java b/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/ConsumerCallback.java new file mode 100644 index 0000000000..f3086b8cef --- /dev/null +++ b/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/ConsumerCallback.java @@ -0,0 +1,17 @@ +package com.baeldung.callbackfunctions; + +import java.util.function.Consumer; + +public class ConsumerCallback { + public void getAge(int initialAge, Consumer callback) { + callback.accept(initialAge); + } + + public void increaseAge(int initialAge, int ageDifference, Consumer callback) { + System.out.println("===== Increase age ===="); + + int newAge = initialAge + ageDifference; + callback.accept(newAge); + + } +} diff --git a/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/EventListener.java b/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/EventListener.java new file mode 100644 index 0000000000..dceba7a931 --- /dev/null +++ b/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/EventListener.java @@ -0,0 +1,12 @@ +package com.baeldung.callbackfunctions; + +public interface EventListener { + + String onTrigger(); + + void respondToTrigger(); +} + + + + diff --git a/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/asynchronous/AsynchronousEventConsumer.java b/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/asynchronous/AsynchronousEventConsumer.java new file mode 100644 index 0000000000..d2f3732679 --- /dev/null +++ b/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/asynchronous/AsynchronousEventConsumer.java @@ -0,0 +1,21 @@ +package com.baeldung.callbackfunctions.asynchronous; + +import com.baeldung.callbackfunctions.EventListener; + +public class AsynchronousEventConsumer{ + + private EventListener listener; + + public AsynchronousEventConsumer(EventListener listener) { + this.listener = listener; + } + + public void doAsynchronousOperation() + { + System.out.println("Performing operation in Asynchronous Task"); + + new Thread(() -> listener.onTrigger()).start(); + } + + +} diff --git a/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/asynchronous/AsynchronousEventListenerImpl.java b/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/asynchronous/AsynchronousEventListenerImpl.java new file mode 100644 index 0000000000..a6e43797f5 --- /dev/null +++ b/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/asynchronous/AsynchronousEventListenerImpl.java @@ -0,0 +1,19 @@ +package com.baeldung.callbackfunctions.asynchronous; + +import com.baeldung.callbackfunctions.EventListener; + +public class AsynchronousEventListenerImpl implements EventListener { + + @Override + public String onTrigger() + { + respondToTrigger(); + return "Asynchronously running callback function"; + } + + @Override + public void respondToTrigger(){ + System.out.println("This is a side effect of the asynchronous trigger."); + } + +} diff --git a/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/synchronous/SynchronousEventConsumer.java b/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/synchronous/SynchronousEventConsumer.java new file mode 100644 index 0000000000..9b06151854 --- /dev/null +++ b/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/synchronous/SynchronousEventConsumer.java @@ -0,0 +1,22 @@ +package com.baeldung.callbackfunctions.synchronous; + +import com.baeldung.callbackfunctions.EventListener; + +public class SynchronousEventConsumer { + + private final EventListener eventListener; + + public SynchronousEventConsumer(EventListener listener) + { + this.eventListener = listener; + } + + public String doSynchronousOperation() + { + System.out.println("Performing callback before synchronous Task"); + + return eventListener.onTrigger(); + } + + +} diff --git a/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/synchronous/SynchronousEventListenerImpl.java b/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/synchronous/SynchronousEventListenerImpl.java new file mode 100644 index 0000000000..156f7857c3 --- /dev/null +++ b/core-java-modules/core-java-functional/src/main/java/com/baeldung/callbackfunctions/synchronous/SynchronousEventListenerImpl.java @@ -0,0 +1,18 @@ +package com.baeldung.callbackfunctions.synchronous; + +import com.baeldung.callbackfunctions.EventListener; + +public class SynchronousEventListenerImpl implements EventListener { + + @Override + public String onTrigger() + { + return "Synchronously running callback function"; + } + + @Override + public void respondToTrigger(){ + System.out.println("Response to trigger"); + } + +} diff --git a/core-java-modules/core-java-functional/src/test/java/com/baeldung/callbackfunctions/AsynchronousCallbackUnitTest.java b/core-java-modules/core-java-functional/src/test/java/com/baeldung/callbackfunctions/AsynchronousCallbackUnitTest.java new file mode 100644 index 0000000000..494d7365d3 --- /dev/null +++ b/core-java-modules/core-java-functional/src/test/java/com/baeldung/callbackfunctions/AsynchronousCallbackUnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.callbackfunctions; + +import org.junit.Test; +import org.mockito.Mockito; +import com.baeldung.callbackfunctions.EventListener; +import com.baeldung.callbackfunctions.asynchronous.AsynchronousEventConsumer; +import com.baeldung.callbackfunctions.asynchronous.AsynchronousEventListenerImpl; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +public class AsynchronousCallbackUnitTest { + + @Test + public void whenCallbackIsInvokedAsynchronously_shouldRunAsynchronousOperation(){ + EventListener listener = Mockito.mock(AsynchronousEventListenerImpl.class); + AsynchronousEventConsumer asynchronousEventListenerConsumer = new AsynchronousEventConsumer(listener); + asynchronousEventListenerConsumer.doAsynchronousOperation(); + + verify(listener, times(1)).onTrigger(); + } +} diff --git a/core-java-modules/core-java-functional/src/test/java/com/baeldung/callbackfunctions/ConsumerCallbackUnitTest.java b/core-java-modules/core-java-functional/src/test/java/com/baeldung/callbackfunctions/ConsumerCallbackUnitTest.java new file mode 100644 index 0000000000..ba09a7e50c --- /dev/null +++ b/core-java-modules/core-java-functional/src/test/java/com/baeldung/callbackfunctions/ConsumerCallbackUnitTest.java @@ -0,0 +1,20 @@ +package com.baeldung.callbackfunctions; + +import org.junit.jupiter.api.Test; +import com.baeldung.callbackfunctions.ConsumerCallback; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ConsumerCallbackUnitTest { + + @Test + public void whenIncreasingInitialAgeByGivenValueThroughCallback_shouldIncreaseAge(){ + ConsumerCallback consumerCallback = new ConsumerCallback(); + consumerCallback.getAge(20, (initialAge) -> { + int ageDifference = 10; + consumerCallback.increaseAge(initialAge, ageDifference, (newAge) -> { + assertEquals(initialAge + ageDifference, newAge); + }); + }); + } +} diff --git a/core-java-modules/core-java-functional/src/test/java/com/baeldung/callbackfunctions/SynchronousCallbackUnitTest.java b/core-java-modules/core-java-functional/src/test/java/com/baeldung/callbackfunctions/SynchronousCallbackUnitTest.java new file mode 100644 index 0000000000..0c270524b7 --- /dev/null +++ b/core-java-modules/core-java-functional/src/test/java/com/baeldung/callbackfunctions/SynchronousCallbackUnitTest.java @@ -0,0 +1,22 @@ +package callbackFunctions; + +import org.junit.jupiter.api.Test; +import com.baeldung.callbackfunctions.EventListener; +import com.baeldung.callbackfunctions.synchronous.SynchronousEventConsumer; +import com.baeldung.callbackfunctions.synchronous.SynchronousEventListenerImpl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class SynchronousCallbackUnitTest { + + @Test + public void whenCallbackIsInvokedSynchronously_shouldRunSynchronousOperation(){ + EventListener listener = new SynchronousEventListenerImpl(); + SynchronousEventConsumer synchronousEventConsumer = new SynchronousEventConsumer(listener); + String result = synchronousEventConsumer.doSynchronousOperation(); + + assertNotNull(result); + assertEquals("Synchronously running callback function", result); + } +}