From 8b03018712be112b43365ba9c59fd5d71558c877 Mon Sep 17 00:00:00 2001 From: Michal Bowzyk Date: Sun, 3 Jun 2018 22:42:54 +0200 Subject: [PATCH] add tasks for objects and companion objects --- .../n13Objects.kt | 28 +++++++++++++++++++ .../n14CompanionObject.kt | 24 ++++++++++++++++ src/util/LinksToDocumentation.kt | 12 ++++++++ .../n13ObjectsKtTest.kt | 22 +++++++++++++++ .../n14CompanionObjectKtTest.kt | 22 +++++++++++++++ 5 files changed, 108 insertions(+) create mode 100644 src/i_introduction/_13_Object_And_Companion_Object/n13Objects.kt create mode 100644 src/i_introduction/_13_Object_And_Companion_Object/n14CompanionObject.kt create mode 100644 test/i_introduction/_13_Object_And_Companion_Object/n13ObjectsKtTest.kt create mode 100644 test/i_introduction/_13_Object_And_Companion_Object/n14CompanionObjectKtTest.kt diff --git a/src/i_introduction/_13_Object_And_Companion_Object/n13Objects.kt b/src/i_introduction/_13_Object_And_Companion_Object/n13Objects.kt new file mode 100644 index 000000000..408474ff6 --- /dev/null +++ b/src/i_introduction/_13_Object_And_Companion_Object/n13Objects.kt @@ -0,0 +1,28 @@ +package i_introduction._13_Object_And_Companion_Object + +import util.doc13 + +fun todoTask13(): Nothing = util.TODO( + """ + Task 13. + Kotlin supports creating thread-safe singletons, which can be useful in various situations. + You can declare them using object keyword. + When You want to use declared object, You don't need to add initializer. + `val example = ObjectExample` + + Add `init { }` block, where You will increment `counterForCreation` property, and + implement getter for `counterForGetter`, which should return it and increment by one. + """, + documentation = doc13() +) + +object ObjectExample { + var counterForCreation = 0 + private set + + var counterForGetter = 0 + get() = todoTask13() + private set +} + + diff --git a/src/i_introduction/_13_Object_And_Companion_Object/n14CompanionObject.kt b/src/i_introduction/_13_Object_And_Companion_Object/n14CompanionObject.kt new file mode 100644 index 000000000..99c2138c1 --- /dev/null +++ b/src/i_introduction/_13_Object_And_Companion_Object/n14CompanionObject.kt @@ -0,0 +1,24 @@ +package i_introduction._13_Object_And_Companion_Object + +import util.doc14 + +fun todoTask14(): Nothing = util.TODO( + """ + Task 14. + Similar behaviour have companion object. + It needs to be declared inside class and can implement interfaces or extends classes. + They can be use ie. for fabric method. + When integrating with Java code, sometimes We need to declare static functions, which We don't have in Kotlin. + Then We can use annotation `@JvmStatic`, but it can be use only inside objects on properties + or in companion objects on both functions ad properties. + + Add companion object inside class below with property. + Make function `getCounter()` return it and increment by one. + """, + documentation = doc14() +) + +class ExampleForCompanionObject { + + fun getCounter(): Int = todoTask14() +} diff --git a/src/util/LinksToDocumentation.kt b/src/util/LinksToDocumentation.kt index 4794940c1..9b015b67d 100644 --- a/src/util/LinksToDocumentation.kt +++ b/src/util/LinksToDocumentation.kt @@ -61,6 +61,18 @@ fun doc11() {} */ fun doc12() {} +/** + * @see Class constructors, + * Object declarations + */ +fun doc13() {} + +/** + * @see Object declarations, + * Static fields in java integrations + */ +fun doc14() {} + /** * @see Operator overloading */ diff --git a/test/i_introduction/_13_Object_And_Companion_Object/n13ObjectsKtTest.kt b/test/i_introduction/_13_Object_And_Companion_Object/n13ObjectsKtTest.kt new file mode 100644 index 000000000..fc2bb4b2d --- /dev/null +++ b/test/i_introduction/_13_Object_And_Companion_Object/n13ObjectsKtTest.kt @@ -0,0 +1,22 @@ +package i_introduction._13_Object_And_Companion_Object + +import org.junit.Assert.assertThat +import org.junit.Test +import org.hamcrest.core.Is.`is` as isEqualTo + +class n13ObjectsKtTest { + + @Test fun checkNumberOfInitialisations() { + val objectExample = ObjectExample + + assertThat(objectExample.counterForCreation, isEqualTo(1)) + assertThat(objectExample.counterForGetter, isEqualTo(0)) + assertThat(objectExample.counterForGetter, isEqualTo(1)) + + val secondInit = ObjectExample + assertThat(secondInit.counterForCreation, isEqualTo(1)) + assertThat(secondInit.counterForGetter, isEqualTo(2)) + + assertThat(objectExample.hashCode(), isEqualTo(secondInit.hashCode())) + } +} \ No newline at end of file diff --git a/test/i_introduction/_13_Object_And_Companion_Object/n14CompanionObjectKtTest.kt b/test/i_introduction/_13_Object_And_Companion_Object/n14CompanionObjectKtTest.kt new file mode 100644 index 000000000..b08e86555 --- /dev/null +++ b/test/i_introduction/_13_Object_And_Companion_Object/n14CompanionObjectKtTest.kt @@ -0,0 +1,22 @@ +package i_introduction._13_Object_And_Companion_Object + +import org.hamcrest.core.IsNot.not +import org.hamcrest.core.Is.`is` as isEqualTo +import org.junit.Assert.assertThat +import org.junit.Test + +class n14CompanionObjectKtTest { + + @Test + fun checkNumberOfInitialisations() { + val objectExample = ExampleForCompanionObject() + + assertThat(objectExample.getCounter(), isEqualTo(0)) + assertThat(objectExample.getCounter(), isEqualTo(1)) + + val secondInit = ExampleForCompanionObject() + assertThat(secondInit.getCounter(), isEqualTo(2)) + + assertThat(objectExample.hashCode(), not(isEqualTo(secondInit.hashCode()))) + } +} \ No newline at end of file