-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KTLN-622: Implement code for the article differentiating Kotlin Any v…
…s * in generics. (#1142) * KTLN-622: Implement code for the article differentiating Kotlin Any vs * in generics. * KTLN-622: Implement code for the article differentiating Kotlin Any vs * in generics. * KTLN-622: Implement code for the article differentiating Kotlin Any vs * in generics. * KTLN-622: Implement code for the article differentiating Kotlin Any vs * in generics. * KTLN-622: Implement code for the article differentiating Kotlin Any vs * in generics.
- Loading branch information
Showing
8 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
### Relevant Articles | ||
- [Convert a Data Class to ByteBuffer in Kotlin](https://www.baeldung.com/kotlin/convert-data-class-to-bytebuffer) | ||
|
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,2 @@ | ||
### Relevant Articles | ||
- Difference between "*" and "Any" in Kotlin generics - Link to be updated |
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,37 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 | ||
http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>core-kotlin-12</artifactId> | ||
<name>core-kotlin-12</name> | ||
<packaging>jar</packaging> | ||
|
||
<parent> | ||
<groupId>com.baeldung</groupId> | ||
<artifactId>core-kotlin-modules</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<build> | ||
<sourceDirectory>src/main/kotlin</sourceDirectory> | ||
<testSourceDirectory>src/test/kotlin</testSourceDirectory> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.jetbrains.kotlin</groupId> | ||
<artifactId>kotlin-maven-plugin</artifactId> | ||
<version>${kotlin.version}</version> | ||
<executions> | ||
<execution> | ||
<id>compile</id> | ||
<phase>compile</phase> | ||
<goals> | ||
<goal>compile</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
8 changes: 8 additions & 0 deletions
8
core-kotlin-modules/core-kotlin-12/src/main/kotlin/com/baeldung/casting/Candy.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,8 @@ | ||
package com.baeldung.casting | ||
|
||
sealed class Candy(val name: String) { | ||
override fun toString() = name | ||
} | ||
|
||
object ChocolateBar: Candy("Chocolate Bar") | ||
object Lollipop : Candy("Lollipop") |
7 changes: 7 additions & 0 deletions
7
core-kotlin-modules/core-kotlin-12/src/main/kotlin/com/baeldung/casting/SpookyTrinket.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,7 @@ | ||
package com.baeldung.casting | ||
|
||
sealed class SpookyTrinket(val name: String) { | ||
override fun toString() = name | ||
} | ||
object FakeSpider : SpookyTrinket("Fake Spider") | ||
object VampireFang : SpookyTrinket("Vampire Fang") |
35 changes: 35 additions & 0 deletions
35
core-kotlin-modules/core-kotlin-12/src/main/kotlin/com/baeldung/casting/TreatDispenser.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,35 @@ | ||
package com.baeldung.casting | ||
|
||
import kotlin.collections.MutableList | ||
import kotlin.collections.mutableListOf | ||
import kotlin.collections.firstOrNull | ||
|
||
class TreatDispenser<T>(private val treats: MutableList<T> = mutableListOf()) { | ||
|
||
// Dispense the first treat | ||
fun dispenseTreat(): T? { | ||
return if (treats.isNotEmpty()) treats.removeFirst() else null | ||
} | ||
|
||
// Peek at the next treat without removing it | ||
fun peekNextTreat(): T? { | ||
return treats.firstOrNull() | ||
} | ||
|
||
// Add a treat to the dispenser | ||
fun addTreat(treat: T) { | ||
treats.add(treat) | ||
} | ||
} | ||
|
||
// Function using * (Star Projection) | ||
fun peekAtNextTreat(dispenser: TreatDispenser<*>) { | ||
val nextTreat = dispenser.peekNextTreat() | ||
println("The next treat is: $nextTreat") | ||
} | ||
|
||
// Function using Any (restricted to non-nullable types) | ||
fun peekAtNextTreatAny(dispenser: TreatDispenser<Any>) { | ||
val nextTreat = dispenser.peekNextTreat() | ||
println("The next treat is: $nextTreat") | ||
} |
65 changes: 65 additions & 0 deletions
65
...lin-modules/core-kotlin-12/src/test/kotlin/com/baeldung/casting/TreatDispenserUnitTest.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,65 @@ | ||
package com.baeldung.casting | ||
|
||
import org.junit.Test | ||
import org.junit.jupiter.api.Assertions.* | ||
|
||
class TreatDispenserUnitTest { | ||
|
||
@Test | ||
fun `dispenseTreat should return the first treat`() { | ||
|
||
val dispenser = TreatDispenser<Candy>() | ||
dispenser.addTreat(ChocolateBar) | ||
dispenser.addTreat(Lollipop) | ||
|
||
assertEquals(ChocolateBar, dispenser.dispenseTreat()) | ||
assertEquals(Lollipop, dispenser.dispenseTreat()) | ||
assertNull(dispenser.dispenseTreat()) | ||
} | ||
|
||
@Test | ||
fun `peekNextTreat should show the next treat without removing it`() { | ||
val dispenser = TreatDispenser<Candy>() | ||
dispenser.addTreat(Lollipop) | ||
|
||
assertEquals(Lollipop, dispenser.peekNextTreat()) | ||
assertEquals(Lollipop, dispenser.peekNextTreat()) | ||
} | ||
|
||
@Test | ||
fun `peekAtNextTreat using star projection works for all types`() { | ||
val candyDispenser = TreatDispenser<Candy>() | ||
candyDispenser.addTreat(ChocolateBar) | ||
|
||
val trinketDispenser = TreatDispenser<SpookyTrinket>() | ||
trinketDispenser.addTreat(VampireFang) | ||
trinketDispenser.addTreat(FakeSpider) | ||
|
||
// Test with Candy dispenser | ||
assertDoesNotThrow { | ||
peekAtNextTreat(candyDispenser) | ||
} | ||
|
||
// Test with Trinket dispenser | ||
assertDoesNotThrow { | ||
peekAtNextTreat(trinketDispenser) | ||
} | ||
} | ||
|
||
@Test | ||
fun `peekAtNextTreatAny fails for non-Any dispensers`() { | ||
val candyDispenser = TreatDispenser<Candy>() | ||
candyDispenser.addTreat(ChocolateBar) | ||
|
||
// This would fail type checking, hence commented: | ||
// peekAtNextTreatAny(candyDispenser) // Error: Type mismatch | ||
|
||
val anyDispenser = TreatDispenser<Any>() | ||
anyDispenser.addTreat("Surprise Treat") | ||
|
||
assertDoesNotThrow { | ||
peekAtNextTreatAny(anyDispenser) | ||
} | ||
} | ||
|
||
} |
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