forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* BAEL-806 lazy article code * BAEL-806 remove comment
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
kotlin/src/main/java/com/baeldung/lazy/ClassWithHeavyInitialization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.baeldung.lazy; | ||
|
||
public class ClassWithHeavyInitialization { | ||
private ClassWithHeavyInitialization() { | ||
} | ||
|
||
private static class LazyHolder { | ||
public static final ClassWithHeavyInitialization INSTANCE = new ClassWithHeavyInitialization(); | ||
} | ||
|
||
public static ClassWithHeavyInitialization getInstance() { | ||
return LazyHolder.INSTANCE; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
kotlin/src/test/java/com/baeldung/kotlin/LazyJavaUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.baeldung.kotlin; | ||
|
||
|
||
import com.baeldung.lazy.ClassWithHeavyInitialization; | ||
import org.junit.Test; | ||
|
||
import static junit.framework.TestCase.assertTrue; | ||
|
||
public class LazyJavaUnitTest { | ||
|
||
@Test | ||
public void giveHeavyClass_whenInitLazy_thenShouldReturnInstanceOnFirstCall() { | ||
//when | ||
ClassWithHeavyInitialization classWithHeavyInitialization = ClassWithHeavyInitialization.getInstance(); | ||
ClassWithHeavyInitialization classWithHeavyInitialization2 = ClassWithHeavyInitialization.getInstance(); | ||
|
||
//then | ||
assertTrue(classWithHeavyInitialization == classWithHeavyInitialization2); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
kotlin/src/test/kotlin/com/baeldung/kotlin/LazyUnitTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.baeldung.kotlin | ||
|
||
import org.junit.Test | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.TimeUnit | ||
import java.util.concurrent.atomic.AtomicInteger | ||
import kotlin.test.assertEquals | ||
|
||
class LazyUnitTest { | ||
@Test | ||
fun givenLazyValue_whenGetIt_thenShouldInitializeItOnlyOnce() { | ||
//given | ||
val numberOfInitializations: AtomicInteger = AtomicInteger() | ||
val lazyValue: ClassWithHeavyInitialization by lazy { | ||
numberOfInitializations.incrementAndGet() | ||
ClassWithHeavyInitialization() | ||
} | ||
//when | ||
println(lazyValue) | ||
println(lazyValue) | ||
|
||
//then | ||
assertEquals(numberOfInitializations.get(), 1) | ||
} | ||
|
||
@Test | ||
fun givenLazyValue_whenGetItUsingPublication_thenCouldInitializeItMoreThanOnce() { | ||
//given | ||
val numberOfInitializations: AtomicInteger = AtomicInteger() | ||
val lazyValue: ClassWithHeavyInitialization by lazy(LazyThreadSafetyMode.PUBLICATION) { | ||
numberOfInitializations.incrementAndGet() | ||
ClassWithHeavyInitialization() | ||
} | ||
val executorService = Executors.newFixedThreadPool(2) | ||
val countDownLatch = CountDownLatch(1) | ||
//when | ||
executorService.submit { countDownLatch.await(); println(lazyValue) } | ||
executorService.submit { countDownLatch.await(); println(lazyValue) } | ||
countDownLatch.countDown() | ||
|
||
//then | ||
executorService.awaitTermination(1, TimeUnit.SECONDS) | ||
executorService.shutdown() | ||
assertEquals(numberOfInitializations.get(), 2) | ||
} | ||
|
||
class ClassWithHeavyInitialization { | ||
|
||
} | ||
|
||
|
||
lateinit var a: String | ||
@Test | ||
fun givenLateInitProperty_whenAccessItAfterInit_thenPass() { | ||
//when | ||
a = "it" | ||
println(a) | ||
|
||
//then not throw | ||
} | ||
|
||
@Test(expected = UninitializedPropertyAccessException::class) | ||
fun givenLateInitProperty_whenAccessItWithoutInit_thenThrow() { | ||
//when | ||
println(a) | ||
} | ||
} |