Skip to content

Commit

Permalink
Bael 806 lazy (eugenp#2248)
Browse files Browse the repository at this point in the history
* BAEL-806 lazy article code

* BAEL-806 remove comment
  • Loading branch information
tomekl007 authored and pivovarit committed Jul 10, 2017
1 parent 54c6928 commit 1b9353c
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
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 kotlin/src/test/java/com/baeldung/kotlin/LazyJavaUnitTest.java
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 kotlin/src/test/kotlin/com/baeldung/kotlin/LazyUnitTest.kt
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)
}
}

0 comments on commit 1b9353c

Please sign in to comment.