Initial commit for lazy lambda article
This commit is contained in:
parent
7c1065b494
commit
7135047ba1
|
@ -22,9 +22,7 @@
|
|||
<target>21</target>
|
||||
<!-- Needed due to a bug with JDK 21, described here: https://issues.apache.org/jira/browse/MCOMPILER-546?page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel&focusedCommentId=17767513 -->
|
||||
<debug>false</debug>
|
||||
<compilerArgs>
|
||||
<arg>--enable-preview</arg>
|
||||
</compilerArgs>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
|
@ -38,6 +36,15 @@
|
|||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>5.7.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>21</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>21</maven.compiler.target.version>
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.lazylambda;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class LambdaSupplier<T> {
|
||||
|
||||
private final Supplier<T> expensiveData;
|
||||
|
||||
public LambdaSupplier(Supplier<T> expensiveData) {
|
||||
this.expensiveData = expensiveData;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return expensiveData.get();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.lazylambda;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class LazyLambdaSupplier<T> {
|
||||
|
||||
private final Supplier<T> expensiveData;
|
||||
|
||||
private T data;
|
||||
|
||||
public LazyLambdaSupplier(Supplier<T> expensiveData) {
|
||||
this.expensiveData = expensiveData;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
if (data != null) {
|
||||
return data;
|
||||
}
|
||||
return data = expensiveData.get();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.lazylambda;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class LazyLambdaThreadSafeSupplier<T> {
|
||||
|
||||
private final Supplier<T> expensiveData;
|
||||
|
||||
private final AtomicReference<T> data;
|
||||
|
||||
public LazyLambdaThreadSafeSupplier(Supplier<T> expensiveData) {
|
||||
this.expensiveData = expensiveData;
|
||||
data = new AtomicReference<>();
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
if (data.get() == null) {
|
||||
synchronized (data) {
|
||||
if (data.get() == null) {
|
||||
data.set(expensiveData.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return data.get();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.lazylambda;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
public class LambdaSupplierUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCalledMultipleTimes_thenShouldBeCalledMultipleTimes() {
|
||||
@SuppressWarnings("unchecked")
|
||||
Supplier<String> mockedExpensiveFunction = Mockito.mock(Supplier.class);
|
||||
Mockito.when(mockedExpensiveFunction.get()).thenReturn("expensive call");
|
||||
LambdaSupplier<String> testee = new LambdaSupplier<>(mockedExpensiveFunction);
|
||||
Mockito.verify(mockedExpensiveFunction, Mockito.never()).get();
|
||||
testee.getData();
|
||||
testee.getData();
|
||||
testee.getData();
|
||||
testee.getData();
|
||||
Mockito.verify(mockedExpensiveFunction, Mockito.times(4)).get();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
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<String> mockedExpensiveFunction = Mockito.mock(Supplier.class);
|
||||
Mockito.when(mockedExpensiveFunction.get()).thenReturn("expensive call");
|
||||
LazyLambdaSupplier<String> 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();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
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<String> mockedExpensiveFunction = Mockito.mock(Supplier.class);
|
||||
Mockito.when(mockedExpensiveFunction.get()).thenReturn("expensive call");
|
||||
LazyLambdaThreadSafeSupplier<String> 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();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue