Last active
September 29, 2021 18:12
-
-
Save JH108/9854512e64362f48a696df714cc5cf28 to your computer and use it in GitHub Desktop.
Random Kotlin
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
import kotlinx.coroutines.* | |
import kotlinx.coroutines.flow.* | |
import kotlin.properties.Delegates | |
fun separator(title: String?) { | |
println() | |
if (title != null) { | |
val truncatedTitle = if (title.length > 80) title.substring(0, 78) else title | |
val beforeAfter = (80 - truncatedTitle.length) / 2 | |
println("-".repeat(beforeAfter) + truncatedTitle + "-".repeat(beforeAfter)) | |
} | |
println("-".repeat(80)) | |
} | |
enum class Divisions { | |
Denver, | |
Midland, | |
SanAntonio | |
} | |
enum class States { | |
Texas, | |
Colorado, | |
Wyoming, | |
NewMexico | |
} | |
enum class Counties { | |
// TX | |
Harris, | |
Montgomery, | |
Brazoria, | |
// CO | |
ElPaso, | |
Denver, | |
CastleRock, | |
// NM | |
Colfax, | |
Cibola, | |
Hidalgo, | |
// WY | |
Teton, | |
Natrona, | |
Washakie | |
} | |
class DivisionSelector(val initialDivision: Divisions?, val observer: (Divisions?) -> Unit) { | |
var currentDivision: Divisions? by Delegates.observable(initialDivision) {_, _, newValue -> observer(newValue)} | |
fun printDivision() { | |
println("Current Division: $currentDivision") | |
} | |
} | |
class StateSelector(var division: Divisions?, val observer: (States?) -> Unit) { | |
private val denverStates = listOf(States.Colorado, States.Wyoming) | |
private val midlandStates = listOf(States.Texas, States.NewMexico) | |
private val sanAntonioStates = listOf(States.Texas) | |
var currentState: States? by Delegates.observable(null) {_, _, newValue -> | |
if (currentStates.contains(newValue).not() && newValue != null) println("State: $newValue is not in the accepted list!") | |
else observer(newValue) | |
} | |
val currentStates get() = when (division) { | |
Divisions.Denver -> denverStates | |
Divisions.Midland -> midlandStates | |
Divisions.SanAntonio -> sanAntonioStates | |
else -> emptyList() | |
} | |
fun printState() { | |
println("Current State: $currentState") | |
} | |
fun printStates() { | |
println("Available States: $currentStates") | |
} | |
} | |
class CountySelector(var state: States?) { | |
private val texasCounties = listOf(Counties.Harris, Counties.Montgomery, Counties.Brazoria) | |
private val coloradoCounties = listOf(Counties.ElPaso, Counties.Denver, Counties.CastleRock) | |
private val wyomingCounties = listOf(Counties.Teton, Counties.Natrona, Counties.Washakie) | |
private val newMexicoCounties = listOf(Counties.ElPaso, Counties.Denver, Counties.CastleRock) | |
var currentCounty: Counties? = null | |
set(value) { | |
if (currentCounties.contains(value).not() && value != null) println("County: $value is not in the accepted list!") | |
else field = value | |
} | |
val currentCounties get() = when (state) { | |
States.Texas -> texasCounties | |
States.Colorado -> coloradoCounties | |
States.Wyoming -> wyomingCounties | |
States.NewMexico -> newMexicoCounties | |
else -> emptyList() | |
} | |
fun printCounty() { | |
println("Current County: $currentCounty") | |
} | |
fun printCounties() { | |
println("Available Counties: $currentCounties") | |
} | |
} | |
fun printStatus(county: CountySelector, state: StateSelector, division: DivisionSelector) { | |
division.printDivision() | |
state.printState() | |
county.printCounty() | |
state.printStates() | |
county.printCounties() | |
} | |
fun main() = runBlocking<Unit> { | |
val county = CountySelector(null) | |
val state = StateSelector(null) { nextState -> | |
county.state = nextState | |
county.currentCounty = null | |
} | |
val division = DivisionSelector(null) { nextDivision -> | |
state.division = nextDivision | |
state.currentState = null | |
} | |
separator("Starting State") | |
printStatus(county, state, division) | |
separator("Division is Denver") | |
division.currentDivision = Divisions.Denver | |
printStatus(county, state, division) | |
separator("State is Wyoming") | |
state.currentState = States.Wyoming | |
printStatus(county, state, division) | |
separator("County is Teton") | |
county.currentCounty = Counties.Teton | |
printStatus(county, state, division) | |
separator("Division is reset") | |
division.currentDivision = null | |
printStatus(county, state, division) | |
separator("Division is San Antonio") | |
division.currentDivision = Divisions.SanAntonio | |
printStatus(county, state, division) | |
separator("Cannot Set State to Wyoming") | |
state.currentState = States.Wyoming | |
printStatus(county, state, division) | |
separator("State is Texas") | |
state.currentState = States.Texas | |
printStatus(county, state, division) | |
separator("Cannot Set County to Teton") | |
county.currentCounty = Counties.Teton | |
printStatus(county, state, division) | |
separator("County is Brazoria") | |
county.currentCounty = Counties.Brazoria | |
printStatus(county, state, division) | |
separator("Division is Midland") | |
division.currentDivision = Divisions.Midland | |
printStatus(county, state, division) | |
} |
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
import kotlinx.coroutines.* | |
import kotlinx.coroutines.flow.* | |
import kotlinx.coroutines.channels.* | |
import kotlinx.coroutines.selects.* | |
import kotlin.properties.Delegates | |
fun separator() { | |
println("-".repeat(80)) | |
} | |
enum class States { | |
Texas, | |
Colorado | |
} | |
enum class Counties { | |
Harris, | |
Montgomery, | |
Brazoria, | |
ElPaso, | |
Denver, | |
CastleRock | |
} | |
class StateSelector(val initialState: States, val observer: (States, States) -> Unit) { | |
var currentState: States by Delegates.observable(initialState) {_, oldValue, newValue -> | |
observer(oldValue, newValue) | |
} | |
} | |
class CountySelector(var state: States) { | |
private val texas = listOf( | |
Counties.Harris, | |
Counties.Montgomery, | |
Counties.Brazoria | |
) | |
private val colorado = listOf( | |
Counties.ElPaso, | |
Counties.Denver, | |
Counties.CastleRock | |
) | |
val currentCounties: List<Counties> get() = when (state) { | |
States.Texas -> texas | |
States.Colorado -> colorado | |
} | |
} | |
fun main() = runBlocking<Unit> { | |
val initialState = States.Texas | |
val counties = CountySelector(initialState) | |
val state = StateSelector(initialState) { _, nextState -> | |
counties.state = nextState | |
} | |
separator() | |
println(counties.currentCounties) | |
state.currentState = States.Colorado | |
separator() | |
println(counties.currentCounties) | |
state.currentState = States.Texas | |
separator() | |
println(counties.currentCounties) | |
} |
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
data class Thing( | |
val name: String | |
) { | |
var value: Int by Delegates.observable(0) { _, _, newValue -> | |
when { | |
newValue % 5 == 0 && newValue % 3 == 0 -> someSecretName = "FizzBuzz" | |
newValue % 3 == 0 -> someSecretName = "Fizz" | |
newValue % 5 == 0 -> someSecretName = "Buzz" | |
else -> someSecretName = "Ha, not valid!" | |
} | |
} | |
private var someSecretName: String = "" | |
set(value) { | |
if (value != "Ha, not valid!") | |
println("$name says: $value") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment