Skip to content

Commit 74e3e7f

Browse files
earth001jzheaux
authored andcommitted
Kotlin Nested and Inner Classes Code SampleCode
Issue: BAEL-1759
1 parent 5eea4fc commit 74e3e7f

3 files changed

Lines changed: 127 additions & 24 deletions

File tree

‎core-kotlin/pom.xml‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,22 @@
106106
<artifactId>klaxon</artifactId>
107107
<version>${klaxon.version}</version>
108108
</dependency>
109-
<dependency>
110-
<groupId>io.ktor</groupId>
111-
<artifactId>ktor-server-netty</artifactId>
112-
<version>${ktor.io.version}</version>
113-
</dependency>
114-
<dependency>
115-
<groupId>io.ktor</groupId>
116-
<artifactId>ktor-gson</artifactId>
117-
<version>${ktor.io.version}</version>
118-
</dependency>
119-
<dependency>
120-
<groupId>ch.qos.logback</groupId>
121-
<artifactId>logback-classic</artifactId>
122-
<version>1.2.1</version>
123-
<scope>test</scope>
124-
</dependency>
109+
<dependency>
110+
<groupId>io.ktor</groupId>
111+
<artifactId>ktor-server-netty</artifactId>
112+
<version>${ktor.io.version}</version>
113+
</dependency>
114+
<dependency>
115+
<groupId>io.ktor</groupId>
116+
<artifactId>ktor-gson</artifactId>
117+
<version>${ktor.io.version}</version>
118+
</dependency>
119+
<dependency>
120+
<groupId>ch.qos.logback</groupId>
121+
<artifactId>logback-classic</artifactId>
122+
<version>1.2.1</version>
123+
<scope>test</scope>
124+
</dependency>
125125
</dependencies>
126126

127127
<build>
@@ -166,13 +166,13 @@
166166
<target>${java.version}</target>
167167
</configuration>
168168
<executions>
169-
<!-- Replacing default-compile as it is treated specially
169+
<!-- Replacing default-compile as it is treated specially
170170
by maven -->
171171
<execution>
172172
<id>default-compile</id>
173173
<phase>none</phase>
174174
</execution>
175-
<!-- Replacing default-testCompile as it is treated specially
175+
<!-- Replacing default-testCompile as it is treated specially
176176
by maven -->
177177
<execution>
178178
<id>default-testCompile</id>
@@ -224,20 +224,20 @@
224224

225225
<properties>
226226
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
227-
<kotlin-maven-plugin.version>1.2.51</kotlin-maven-plugin.version>
228-
<kotlin-test-junit.version>1.2.51</kotlin-test-junit.version>
229-
<kotlin-stdlib.version>1.2.51</kotlin-stdlib.version>
230-
<kotlin-reflect.version>1.2.51</kotlin-reflect.version>
227+
<kotlin-maven-plugin.version>1.2.60</kotlin-maven-plugin.version>
228+
<kotlin-test-junit.version>1.2.60</kotlin-test-junit.version>
229+
<kotlin-stdlib.version>1.2.60</kotlin-stdlib.version>
230+
<kotlin-reflect.version>1.2.60</kotlin-reflect.version>
231231
<kotlinx.version>0.22.5</kotlinx.version>
232232
<ktor.io.version>0.9.2</ktor.io.version>
233233
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
234234
<kodein.version>4.1.0</kodein.version>
235235
<klaxon.version>3.0.4</klaxon.version>
236236
<khttp.version>0.1.0</khttp.version>
237237
<commons-math3.version>3.6.1</commons-math3.version>
238-
<junit.platform.version>1.0.0</junit.platform.version>
238+
<junit.platform.version>1.1.1</junit.platform.version>
239239
<junit.vintage.version>5.2.0</junit.vintage.version>
240240
<assertj.version>3.10.0</assertj.version>
241241
</properties>
242242

243-
</project>
243+
</project>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.baeldung.nested
2+
3+
import org.slf4j.Logger
4+
import org.slf4j.LoggerFactory
5+
6+
class Computer(val model: String) {
7+
8+
companion object {
9+
const val originCountry = "China"
10+
fun getBuiltDate(): String {
11+
return "2018-05-23"
12+
}
13+
14+
val log: Logger = LoggerFactory.getLogger(Computer::class.java)
15+
}
16+
17+
//Nested class
18+
class MotherBoard(val manufacturer: String) {
19+
fun getInfo() = "Made by $manufacturer installed in $originCountry - ${getBuiltDate()}"
20+
}
21+
22+
//Inner class
23+
inner class HardDisk(val sizeInGb: Int) {
24+
fun getInfo() = "Installed on ${this@Computer} with $sizeInGb GB"
25+
}
26+
27+
interface Switcher {
28+
fun on(): String
29+
}
30+
31+
interface Protector {
32+
fun smart()
33+
}
34+
35+
fun powerOn(): String {
36+
//Local class
37+
var defaultColor = "Blue"
38+
39+
class Led(val color: String) {
40+
fun blink(): String {
41+
return "blinking $color"
42+
}
43+
44+
fun changeDefaultPowerOnColor() {
45+
defaultColor = "Violet"
46+
}
47+
}
48+
49+
val powerLed = Led("Green")
50+
log.debug("defaultColor is $defaultColor")
51+
powerLed.changeDefaultPowerOnColor()
52+
log.debug("defaultColor changed inside Led class to $defaultColor")
53+
//Anonymous object
54+
val powerSwitch = object : Switcher, Protector {
55+
override fun on(): String {
56+
return powerLed.blink()
57+
}
58+
59+
override fun smart() {
60+
log.debug("Smart protection is implemented")
61+
}
62+
63+
fun changeDefaultPowerOnColor() {
64+
defaultColor = "Yellow"
65+
}
66+
}
67+
powerSwitch.changeDefaultPowerOnColor()
68+
log.debug("defaultColor changed inside powerSwitch anonymous object to $defaultColor")
69+
return powerSwitch.on()
70+
}
71+
72+
override fun toString(): String {
73+
return "Computer(model=$model)"
74+
}
75+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.nested
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.junit.jupiter.api.Test
5+
6+
class ComputerUnitTest {
7+
8+
@Test
9+
fun givenComputer_whenPowerOn_thenBlink() {
10+
val computer = Computer("Desktop")
11+
12+
assertThat(computer.powerOn()).isEqualTo("blinking Green")
13+
}
14+
15+
@Test
16+
fun givenMotherboard_whenGetInfo_thenGetInstalledAndBuiltDetails() {
17+
val motherBoard = Computer.MotherBoard("MotherBoard Inc.")
18+
19+
assertThat(motherBoard.getInfo()).isEqualTo("Made by MotherBoard Inc. installed in China - 2018-05-23")
20+
}
21+
22+
@Test
23+
fun givenHardDisk_whenGetInfo_thenGetComputerModelAndDiskSizeInGb() {
24+
val hardDisk = Computer("Desktop").HardDisk(1000)
25+
26+
assertThat(hardDisk.getInfo()).isEqualTo("Installed on Computer(model=Desktop) with 1000 GB")
27+
}
28+
}

0 commit comments

Comments
 (0)