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-2376 Inline classes in kotlin (eugenp#5842)
* BAEL-2376 Inline classes in kotlin * BAEL-2376 Move classes
- Loading branch information
1 parent
de3479c
commit d8e34dc
Showing
4 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
core-kotlin/src/main/kotlin/com/baeldung/inline/classes/CircleRadius.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,14 @@ | ||
package com.baeldung.inline.classes | ||
|
||
interface Drawable { | ||
fun draw() | ||
} | ||
|
||
inline class CircleRadius(private val circleRadius : Double) : Drawable { | ||
val diameterOfCircle get() = 2 * circleRadius | ||
fun areaOfCircle() = 3.14 * circleRadius * circleRadius | ||
|
||
override fun draw() { | ||
println("Draw my circle") | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
core-kotlin/src/main/kotlin/com/baeldung/inline/classes/InlineDoubleWrapper.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,3 @@ | ||
package com.baeldung.inline.classes | ||
|
||
inline class InlineDoubleWrapper(val doubleValue : Double) |
19 changes: 19 additions & 0 deletions
19
core-kotlin/src/test/kotlin/com/baeldung/inline/classes/CircleRadiusTest.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,19 @@ | ||
package com.baeldung.inline.classes | ||
|
||
import org.junit.Test | ||
import kotlin.test.assertEquals | ||
|
||
class CircleRadiusTest { | ||
|
||
@Test | ||
fun givenRadius_ThenDiameterIsCorrectlyCalculated() { | ||
val radius = CircleRadius(5.0) | ||
assertEquals(10.0, radius.diameterOfCircle) | ||
} | ||
|
||
@Test | ||
fun givenRadius_ThenAreaIsCorrectlyCalculated() { | ||
val radius = CircleRadius(5.0) | ||
assertEquals(78.5, radius.areaOfCircle()) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
core-kotlin/src/test/kotlin/com/baeldung/inline/classes/InlineDoubleWrapperTest.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,13 @@ | ||
package com.baeldung.inline.classes | ||
|
||
import org.junit.Test | ||
import kotlin.test.assertEquals | ||
|
||
class InlineDoubleWrapperTest { | ||
|
||
@Test | ||
fun whenInclineClassIsUsed_ThenPropertyIsReadCorrectly() { | ||
val piDoubleValue = InlineDoubleWrapper(3.14) | ||
assertEquals(3.14, piDoubleValue.doubleValue) | ||
} | ||
} |