Skip to content
This repository has been archived by the owner on Aug 5, 2021. It is now read-only.

add tasks for objects and companion objects #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/i_introduction/_13_Object_And_Companion_Object/n13Objects.kt
Original file line number Diff line number Diff line change
@@ -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
}


Original file line number Diff line number Diff line change
@@ -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()
}
12 changes: 12 additions & 0 deletions src/util/LinksToDocumentation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ fun doc11() {}
*/
fun doc12() {}

/**
* @see <a href="https://kotlinlang.org/docs/reference/classes.html#constructors">Class constructors</a>,
* <a href="https://kotlinlang.org/docs/reference/object-declarations.html#object-declarations">Object declarations</a>
*/
fun doc13() {}

/**
* @see <a href="https://kotlinlang.org/docs/reference/object-declarations.html#object-declarations">Object declarations</a>,
* <a href="https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#static-fields">Static fields in java integrations</a>
*/
fun doc14() {}

/**
* @see <a href="https://kotlinlang.org/docs/reference/operator-overloading.html">Operator overloading</a>
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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()))
}
}
Original file line number Diff line number Diff line change
@@ -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())))
}
}