Exercise: Make DeckConnector comparable
Change the DeckConnector
class:
- Implement a
compareTo
method so it first compares values based on the deckName
property; if they are equal, then compare them based on the state
property ordinal. - Implement
equals
and hashCode
methods so that two DeckConnector
instances are equal if their deckName
and state
properties are equal.
class DeckConnector(
val deckName: String
) : Comparable<DeckConnector> {
var state: ConnectionState = ConnectionState.Initial
override fun compareTo(other: DeckConnector): Int {
TODO("Not yet implemented")
}
enum class ConnectionState {
Initial,
Connected,
Disconnected
}
}
This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in effective/design/DeckConnector.kt. You can find there starting code and unit tests.
Once you are done with the exercise, you can check your solution here.
import org.junit.Test
import java.util.*
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
class DeckConnector(
val deckName: String
) : Comparable<DeckConnector> {
var state: ConnectionState = ConnectionState.Initial
override fun compareTo(other: DeckConnector): Int {
TODO("Not yet implemented")
}
enum class ConnectionState {
Initial,
Connected,
Disconnected
}
}
class DeckConnectorTest {
@Test
fun `should be equal when deck name and state are equal`() {
val deckConnector1 = DeckConnector("deck1")
val deckConnector2 = DeckConnector("deck1")
assertEquals(deckConnector1, deckConnector2)
}
@Test
fun `should not equal when deck name or state are different`() {
val deckConnector1 = DeckConnector("deck1")
val deckConnector2 = DeckConnector("deck1")
val deckConnector3 = DeckConnector("deck2")
assertNotEquals(deckConnector1, deckConnector3)
assertEquals(deckConnector1, deckConnector2)
deckConnector1.state = DeckConnector.ConnectionState.Connected
assertNotEquals(deckConnector1, deckConnector2)
deckConnector2.state = DeckConnector.ConnectionState.Connected
assertEquals(deckConnector1, deckConnector2)
}
@Test
fun `should have equal hash code for equal objects`() {
val deckConnector1 = DeckConnector("deck1")
val deckConnector2 = DeckConnector("deck1")
val deckConnector3 = DeckConnector("deck2")
assertNotEquals(deckConnector1.hashCode(), deckConnector3.hashCode())
assertEquals(deckConnector1.hashCode(), deckConnector2.hashCode())
deckConnector1.state = DeckConnector.ConnectionState.Connected
assertNotEquals(deckConnector1.hashCode(), deckConnector2.hashCode())
deckConnector2.state = DeckConnector.ConnectionState.Connected
assertEquals(deckConnector1.hashCode(), deckConnector2.hashCode())
}
@Test
fun `should compare objects by deck name and state`() {
val deck1 = DeckConnector("deck1").apply { state = DeckConnector.ConnectionState.Connected }
val deck2 = DeckConnector("deck2").apply { state = DeckConnector.ConnectionState.Connected }
val deck3 = DeckConnector("deck2").apply { state = DeckConnector.ConnectionState.Disconnected }
val sortedDecks = listOf(
deck1,
DeckConnector("deck1").apply { state = DeckConnector.ConnectionState.Disconnected },
deck2,
deck3,
DeckConnector("deck3").apply { state = DeckConnector.ConnectionState.Connected },
DeckConnector("deck3").apply { state = DeckConnector.ConnectionState.Disconnected }
)
assertEquals(sortedDecks, sortedDecks.shuffled().sorted())
assert(deck1 < deck2)
assert(deck2 < deck3)
assert(deck1 < deck3)
assert(deck2 > deck1)
assert(deck3 > deck2)
assert(deck3 > deck1)
assert(deck1 <= deck1)
assert(deck2 <= deck2)
assert(deck3 <= deck3)
}
}
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.