diff --git a/core-java-modules/core-java-21/src/test/java/com/baeldung/lazylambda/LazyLambdaSupplierUnitTest.java b/core-java-modules/core-java-21/src/test/java/com/baeldung/lazylambda/LazyLambdaSupplierUnitTest.java deleted file mode 100644 index 4f110dfba7..0000000000 --- a/core-java-modules/core-java-21/src/test/java/com/baeldung/lazylambda/LazyLambdaSupplierUnitTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.baeldung.lazylambda; - -import java.util.function.Supplier; - -import org.junit.Test; -import org.mockito.Mockito; - -public class LazyLambdaSupplierUnitTest { - - @Test - public void whenCalledMultipleTimes_thenShouldBeCalledOnlyOnce() { - @SuppressWarnings("unchecked") Supplier mockedExpensiveFunction = Mockito.mock(Supplier.class); - Mockito.when(mockedExpensiveFunction.get()) - .thenReturn("expensive call"); - LazyLambdaSupplier testee = new LazyLambdaSupplier<>(mockedExpensiveFunction); - Mockito.verify(mockedExpensiveFunction, Mockito.never()) - .get(); - testee.getData(); - testee.getData(); - testee.getData(); - testee.getData(); - Mockito.verify(mockedExpensiveFunction, Mockito.times(1)) - .get(); - } - -} diff --git a/core-java-modules/core-java-21/src/test/java/com/baeldung/lazylambda/LazyLambdaThreadSafeSupplierUnitTest.java b/core-java-modules/core-java-21/src/test/java/com/baeldung/lazylambda/LazyLambdaThreadSafeSupplierUnitTest.java deleted file mode 100644 index 7870b5a111..0000000000 --- a/core-java-modules/core-java-21/src/test/java/com/baeldung/lazylambda/LazyLambdaThreadSafeSupplierUnitTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.baeldung.lazylambda; - -import java.util.function.Supplier; - -import org.junit.Test; -import org.mockito.Mockito; - -public class LazyLambdaThreadSafeSupplierUnitTest { - - @Test - public void whenCalledMultipleTimes_thenShouldBeCalledOnlyOnce() { - @SuppressWarnings("unchecked") Supplier mockedExpensiveFunction = Mockito.mock(Supplier.class); - Mockito.when(mockedExpensiveFunction.get()) - .thenReturn("expensive call"); - LazyLambdaThreadSafeSupplier testee = new LazyLambdaThreadSafeSupplier<>(mockedExpensiveFunction); - Mockito.verify(mockedExpensiveFunction, Mockito.never()) - .get(); - testee.getData(); - testee.getData(); - testee.getData(); - testee.getData(); - Mockito.verify(mockedExpensiveFunction, Mockito.times(1)) - .get(); - } - -} diff --git a/core-java-modules/core-java-21/src/main/java/com/baeldung/lazylambda/LambdaSupplier.java b/core-java-modules/core-java-function/src/main/java/com/baeldung/lazylambda/LambdaSupplier.java similarity index 85% rename from core-java-modules/core-java-21/src/main/java/com/baeldung/lazylambda/LambdaSupplier.java rename to core-java-modules/core-java-function/src/main/java/com/baeldung/lazylambda/LambdaSupplier.java index a9c4c78edd..36cd1b31ae 100644 --- a/core-java-modules/core-java-21/src/main/java/com/baeldung/lazylambda/LambdaSupplier.java +++ b/core-java-modules/core-java-function/src/main/java/com/baeldung/lazylambda/LambdaSupplier.java @@ -4,7 +4,7 @@ import java.util.function.Supplier; public class LambdaSupplier { - private final Supplier expensiveData; + protected final Supplier expensiveData; public LambdaSupplier(Supplier expensiveData) { this.expensiveData = expensiveData; diff --git a/core-java-modules/core-java-21/src/main/java/com/baeldung/lazylambda/LazyLambdaSupplier.java b/core-java-modules/core-java-function/src/main/java/com/baeldung/lazylambda/LazyLambdaSupplier.java similarity index 70% rename from core-java-modules/core-java-21/src/main/java/com/baeldung/lazylambda/LazyLambdaSupplier.java rename to core-java-modules/core-java-function/src/main/java/com/baeldung/lazylambda/LazyLambdaSupplier.java index 484fadbe90..dce3cc4997 100644 --- a/core-java-modules/core-java-21/src/main/java/com/baeldung/lazylambda/LazyLambdaSupplier.java +++ b/core-java-modules/core-java-function/src/main/java/com/baeldung/lazylambda/LazyLambdaSupplier.java @@ -2,16 +2,15 @@ package com.baeldung.lazylambda; import java.util.function.Supplier; -public class LazyLambdaSupplier { - - private final Supplier expensiveData; +public class LazyLambdaSupplier extends LambdaSupplier { private T data; public LazyLambdaSupplier(Supplier expensiveData) { - this.expensiveData = expensiveData; + super(expensiveData); } + @Override public T getData() { if (data != null) { return data; diff --git a/core-java-modules/core-java-21/src/main/java/com/baeldung/lazylambda/LazyLambdaThreadSafeSupplier.java b/core-java-modules/core-java-function/src/main/java/com/baeldung/lazylambda/LazyLambdaThreadSafeSupplier.java similarity index 79% rename from core-java-modules/core-java-21/src/main/java/com/baeldung/lazylambda/LazyLambdaThreadSafeSupplier.java rename to core-java-modules/core-java-function/src/main/java/com/baeldung/lazylambda/LazyLambdaThreadSafeSupplier.java index 27180ced7d..32eec0a5d8 100644 --- a/core-java-modules/core-java-21/src/main/java/com/baeldung/lazylambda/LazyLambdaThreadSafeSupplier.java +++ b/core-java-modules/core-java-function/src/main/java/com/baeldung/lazylambda/LazyLambdaThreadSafeSupplier.java @@ -3,14 +3,12 @@ package com.baeldung.lazylambda; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; -public class LazyLambdaThreadSafeSupplier { - - private final Supplier expensiveData; +public class LazyLambdaThreadSafeSupplier extends LambdaSupplier { private final AtomicReference data; public LazyLambdaThreadSafeSupplier(Supplier expensiveData) { - this.expensiveData = expensiveData; + super(expensiveData); data = new AtomicReference<>(); } @@ -22,7 +20,6 @@ public class LazyLambdaThreadSafeSupplier { } } } - return data.get(); } diff --git a/core-java-modules/core-java-21/src/test/java/com/baeldung/lazylambda/LambdaSupplierUnitTest.java b/core-java-modules/core-java-function/src/test/java/com/baeldung/lazylambda/LambdaSupplierUnitTest.java similarity index 85% rename from core-java-modules/core-java-21/src/test/java/com/baeldung/lazylambda/LambdaSupplierUnitTest.java rename to core-java-modules/core-java-function/src/test/java/com/baeldung/lazylambda/LambdaSupplierUnitTest.java index 8b705286c2..f0a500e084 100644 --- a/core-java-modules/core-java-21/src/test/java/com/baeldung/lazylambda/LambdaSupplierUnitTest.java +++ b/core-java-modules/core-java-function/src/test/java/com/baeldung/lazylambda/LambdaSupplierUnitTest.java @@ -17,9 +17,7 @@ public class LambdaSupplierUnitTest { .get(); testee.getData(); testee.getData(); - testee.getData(); - testee.getData(); - Mockito.verify(mockedExpensiveFunction, Mockito.times(4)) + Mockito.verify(mockedExpensiveFunction, Mockito.times(2)) .get(); } diff --git a/core-java-modules/core-java-function/src/test/java/com/baeldung/lazylambda/LazyLambdaSupplierUnitTest.java b/core-java-modules/core-java-function/src/test/java/com/baeldung/lazylambda/LazyLambdaSupplierUnitTest.java new file mode 100644 index 0000000000..1371983a3d --- /dev/null +++ b/core-java-modules/core-java-function/src/test/java/com/baeldung/lazylambda/LazyLambdaSupplierUnitTest.java @@ -0,0 +1,51 @@ +package com.baeldung.lazylambda; + +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.stubbing.Answer; + +public class LazyLambdaSupplierUnitTest { + + @Test + public void whenCalledMultipleTimes_thenShouldBeCalledOnlyOnce() { + @SuppressWarnings("unchecked") Supplier mockedExpensiveFunction = Mockito.mock(Supplier.class); + Mockito.when(mockedExpensiveFunction.get()) + .thenReturn("expensive call"); + LazyLambdaSupplier testee = new LazyLambdaSupplier<>(mockedExpensiveFunction); + Mockito.verify(mockedExpensiveFunction, Mockito.never()) + .get(); + testee.getData(); + testee.getData(); + Mockito.verify(mockedExpensiveFunction, Mockito.times(1)) + .get(); + } + + @Test + public void whenCalledMultipleTimesConcurrently_thenShouldBeCalledMultipleTimes() throws InterruptedException { + @SuppressWarnings("unchecked") Supplier mockedExpensiveFunction = Mockito.mock(Supplier.class); + Mockito.when(mockedExpensiveFunction.get()) + .thenAnswer((Answer) invocation -> { + Thread.sleep(1000L); + return "Late response!"; + }); + LazyLambdaSupplier testee = new LazyLambdaSupplier<>(mockedExpensiveFunction); + Mockito.verify(mockedExpensiveFunction, Mockito.never()) + .get(); + + ExecutorService executorService = Executors.newFixedThreadPool(4); + executorService.invokeAll(List.of(testee::getData, testee::getData)); + executorService.shutdown(); + if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) { + executorService.shutdownNow(); + } + + Mockito.verify(mockedExpensiveFunction, Mockito.times(2)) + .get(); + } +} diff --git a/core-java-modules/core-java-function/src/test/java/com/baeldung/lazylambda/LazyLambdaThreadSafeSupplierUnitTest.java b/core-java-modules/core-java-function/src/test/java/com/baeldung/lazylambda/LazyLambdaThreadSafeSupplierUnitTest.java new file mode 100644 index 0000000000..83bd49aa7e --- /dev/null +++ b/core-java-modules/core-java-function/src/test/java/com/baeldung/lazylambda/LazyLambdaThreadSafeSupplierUnitTest.java @@ -0,0 +1,52 @@ +package com.baeldung.lazylambda; + +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.stubbing.Answer; + +public class LazyLambdaThreadSafeSupplierUnitTest { + + @Test + public void whenCalledMultipleTimes_thenShouldBeCalledOnlyOnce() { + @SuppressWarnings("unchecked") Supplier mockedExpensiveFunction = Mockito.mock(Supplier.class); + Mockito.when(mockedExpensiveFunction.get()) + .thenReturn("expensive call"); + LazyLambdaThreadSafeSupplier testee = new LazyLambdaThreadSafeSupplier<>(mockedExpensiveFunction); + Mockito.verify(mockedExpensiveFunction, Mockito.never()) + .get(); + testee.getData(); + testee.getData(); + Mockito.verify(mockedExpensiveFunction, Mockito.times(1)) + .get(); + } + + @Test + public void whenCalledMultipleTimesConcurrently_thenShouldBeCalledOnlyOnce() throws InterruptedException { + @SuppressWarnings("unchecked") Supplier mockedExpensiveFunction = Mockito.mock(Supplier.class); + Mockito.when(mockedExpensiveFunction.get()) + .thenAnswer((Answer) invocation -> { + Thread.sleep(1000L); + return "Late response!"; + }); + LazyLambdaThreadSafeSupplier testee = new LazyLambdaThreadSafeSupplier<>(mockedExpensiveFunction); + Mockito.verify(mockedExpensiveFunction, Mockito.never()) + .get(); + + ExecutorService executorService = Executors.newFixedThreadPool(4); + executorService.invokeAll(List.of(testee::getData, testee::getData)); + executorService.shutdown(); + if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) { + executorService.shutdownNow(); + } + + Mockito.verify(mockedExpensiveFunction, Mockito.times(1)) + .get(); + } + +}