-
Notifications
You must be signed in to change notification settings - Fork 15
/
Implementation.kt
71 lines (55 loc) · 1.47 KB
/
Implementation.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package co.zsmb.villagedsl.simple.dsl2
import co.zsmb.villagedsl.simple.models.House
import co.zsmb.villagedsl.simple.models.Person
import co.zsmb.villagedsl.simple.models.Village
@DslMarker
annotation class SimpleDSL2
@SimpleDSL2
class HouseBuilder {
private val people = mutableListOf<Person>()
fun build(): House {
return House(people)
}
@SimpleDSL2
operator fun Person.unaryPlus() {
people += this
}
@SimpleDSL2
operator fun Person.unaryMinus() {
people += this
}
}
@SimpleDSL2
class VillageBuilder {
private val houses = mutableListOf<House>()
@SimpleDSL2
operator fun House.unaryPlus() {
houses += this
}
@SimpleDSL2
fun house(setup: HouseBuilder.() -> Unit = {}) {
val houseBuilder = HouseBuilder()
houseBuilder.setup()
houses += houseBuilder.build()
}
fun build(): Village {
return Village(houses)
}
/**
* This method shadows the [co.zsmb.villagedsl.simple.dsl2.village] method when inside the scope
* of a [VillageBuilder], so that villages can't be nested.
*/
@Suppress("UNUSED_PARAMETER")
@Deprecated(
level = DeprecationLevel.ERROR,
message = "Villages can't be nested."
)
fun village(param: () -> Unit = {}) {
}
}
@SimpleDSL2
fun village(setup: VillageBuilder.() -> Unit): Village {
val villageBuilder = VillageBuilder()
villageBuilder.setup()
return villageBuilder.build()
}