Skip to content

Commit 1b9353c

Browse files
tomekl007pivovarit
authored andcommitted
Bael 806 lazy (eugenp#2248)
* BAEL-806 lazy article code * BAEL-806 remove comment
1 parent 54c6928 commit 1b9353c

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.lazy;
2+
3+
public class ClassWithHeavyInitialization {
4+
private ClassWithHeavyInitialization() {
5+
}
6+
7+
private static class LazyHolder {
8+
public static final ClassWithHeavyInitialization INSTANCE = new ClassWithHeavyInitialization();
9+
}
10+
11+
public static ClassWithHeavyInitialization getInstance() {
12+
return LazyHolder.INSTANCE;
13+
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.kotlin;
2+
3+
4+
import com.baeldung.lazy.ClassWithHeavyInitialization;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertTrue;
8+
9+
public class LazyJavaUnitTest {
10+
11+
@Test
12+
public void giveHeavyClass_whenInitLazy_thenShouldReturnInstanceOnFirstCall() {
13+
//when
14+
ClassWithHeavyInitialization classWithHeavyInitialization = ClassWithHeavyInitialization.getInstance();
15+
ClassWithHeavyInitialization classWithHeavyInitialization2 = ClassWithHeavyInitialization.getInstance();
16+
17+
//then
18+
assertTrue(classWithHeavyInitialization == classWithHeavyInitialization2);
19+
}
20+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.kotlin
2+
3+
import org.junit.Test
4+
import java.util.concurrent.CountDownLatch
5+
import java.util.concurrent.Executors
6+
import java.util.concurrent.TimeUnit
7+
import java.util.concurrent.atomic.AtomicInteger
8+
import kotlin.test.assertEquals
9+
10+
class LazyUnitTest {
11+
@Test
12+
fun givenLazyValue_whenGetIt_thenShouldInitializeItOnlyOnce() {
13+
//given
14+
val numberOfInitializations: AtomicInteger = AtomicInteger()
15+
val lazyValue: ClassWithHeavyInitialization by lazy {
16+
numberOfInitializations.incrementAndGet()
17+
ClassWithHeavyInitialization()
18+
}
19+
//when
20+
println(lazyValue)
21+
println(lazyValue)
22+
23+
//then
24+
assertEquals(numberOfInitializations.get(), 1)
25+
}
26+
27+
@Test
28+
fun givenLazyValue_whenGetItUsingPublication_thenCouldInitializeItMoreThanOnce() {
29+
//given
30+
val numberOfInitializations: AtomicInteger = AtomicInteger()
31+
val lazyValue: ClassWithHeavyInitialization by lazy(LazyThreadSafetyMode.PUBLICATION) {
32+
numberOfInitializations.incrementAndGet()
33+
ClassWithHeavyInitialization()
34+
}
35+
val executorService = Executors.newFixedThreadPool(2)
36+
val countDownLatch = CountDownLatch(1)
37+
//when
38+
executorService.submit { countDownLatch.await(); println(lazyValue) }
39+
executorService.submit { countDownLatch.await(); println(lazyValue) }
40+
countDownLatch.countDown()
41+
42+
//then
43+
executorService.awaitTermination(1, TimeUnit.SECONDS)
44+
executorService.shutdown()
45+
assertEquals(numberOfInitializations.get(), 2)
46+
}
47+
48+
class ClassWithHeavyInitialization {
49+
50+
}
51+
52+
53+
lateinit var a: String
54+
@Test
55+
fun givenLateInitProperty_whenAccessItAfterInit_thenPass() {
56+
//when
57+
a = "it"
58+
println(a)
59+
60+
//then not throw
61+
}
62+
63+
@Test(expected = UninitializedPropertyAccessException::class)
64+
fun givenLateInitProperty_whenAccessItWithoutInit_thenThrow() {
65+
//when
66+
println(a)
67+
}
68+
}

0 commit comments

Comments
 (0)