universal: pretty print, more JVM<3 type erasure fun

This commit is contained in:
Sofus Mortensen 2017-01-11 23:44:11 +01:00
parent abb361f207
commit 8ac7d7c189

View File

@ -68,40 +68,38 @@ private class PrettyPrint(arr : Arrangement) {
} }
} }
fun prettyPrintPerBoolean(per: Perceivable<Boolean>) { fun prettyPrint(per: Perceivable<Boolean>, x: Boolean? = null) {
when (per) { when (per) {
is Const -> { is Const -> print("\"${per.value}\"")
print("\"${per.value}\"")
}
is PerceivableOr -> { is PerceivableOr -> {
prettyPrintPerBoolean(per.left) prettyPrint(per.left)
print(" or ") print(" or ")
prettyPrintPerBoolean(per.right) prettyPrint(per.right)
} }
is PerceivableAnd -> { is PerceivableAnd -> {
prettyPrintPerBoolean(per.left) prettyPrint(per.left)
print(" and ") print(" and ")
prettyPrintPerBoolean(per.right) prettyPrint(per.right)
} }
is TimePerceivable -> { is TimePerceivable -> {
when (per.cmp) { when (per.cmp) {
Comparison.GT, Comparison.GTE -> { Comparison.GT, Comparison.GTE -> {
print("after(") print("after(")
prettyPrintPerInstant(per.instant) prettyPrint(per.instant)
print(")") print(")")
} }
Comparison.LT, Comparison.LTE -> { Comparison.LT, Comparison.LTE -> {
print("before(") print("before(")
prettyPrintPerInstant(per.instant) prettyPrint(per.instant)
print(")") print(")")
} }
} }
} }
is PerceivableComparison<*> -> { is PerceivableComparison<*> -> {
when (per.type) { when (per.type) {
BigDecimal::class.java -> prettyPrintPerBD(per.left as Perceivable<BigDecimal>) BigDecimal::class.java -> prettyPrint(per.left as Perceivable<BigDecimal>)
Instant::class.java -> prettyPrintPerInstant(per.left as Perceivable<Instant>) Instant::class.java -> prettyPrint(per.left as Perceivable<Instant>)
Boolean::class.java -> prettyPrintPerBoolean(per.left as Perceivable<Boolean>) Boolean::class.java -> prettyPrint(per.left as Perceivable<Boolean>)
} }
when (per.cmp) { when (per.cmp) {
Comparison.GT -> print(" > ") Comparison.GT -> print(" > ")
@ -110,40 +108,30 @@ private class PrettyPrint(arr : Arrangement) {
Comparison.LTE -> print(" <= ") Comparison.LTE -> print(" <= ")
} }
when (per.type) { when (per.type) {
BigDecimal::class.java -> prettyPrintPerBD(per.right as Perceivable<BigDecimal>) BigDecimal::class.java -> prettyPrint(per.right as Perceivable<BigDecimal>)
Instant::class.java -> prettyPrintPerInstant(per.right as Perceivable<Instant>) Instant::class.java -> prettyPrint(per.right as Perceivable<Instant>)
Boolean::class.java -> prettyPrintPerBoolean(per.right as Perceivable<Boolean>) Boolean::class.java -> prettyPrint(per.right as Perceivable<Boolean>)
} }
} }
is TerminalEvent -> { is TerminalEvent -> print("TerminalEvent(${partyMap[per.reference.owningKey]}, \"${per.source}\")")
print("TerminalEvent(${partyMap[per.reference.owningKey]}, \"${per.source}\")") is ActorPerceivable -> print("signedBy(${partyMap[per.actor.owningKey]})")
}
is ActorPerceivable -> {
print("signedBy(${partyMap[per.actor.owningKey]})")
}
else -> print(per) else -> print(per)
} }
} }
fun prettyPrintPerInstant(per: Perceivable<Instant>) { fun prettyPrint(per: Perceivable<Instant>, x: Instant? = null) {
when (per) { when (per) {
is Const -> { is Const -> print("\"${per.value}\"")
print("\"${per.value}\"") is StartDate -> print("startDate")
} is EndDate -> print("endDate")
is StartDate -> {
print("startDate")
}
is EndDate -> {
print("endDate")
}
else -> print(per) else -> print(per)
} }
} }
fun prettyPrintPerBD(per: Perceivable<BigDecimal>) { fun prettyPrint(per: Perceivable<BigDecimal>, x: BigDecimal? = null) {
when (per) { when (per) {
is PerceivableOperation<BigDecimal> -> { is PerceivableOperation<BigDecimal> -> {
prettyPrintPerBD(per.left) prettyPrint(per.left)
when (per.op) { when (per.op) {
Operation.PLUS -> print(" + ") Operation.PLUS -> print(" + ")
Operation.MINUS -> print(" - ") Operation.MINUS -> print(" - ")
@ -151,30 +139,26 @@ private class PrettyPrint(arr : Arrangement) {
Operation.TIMES -> print(" * ") Operation.TIMES -> print(" * ")
else -> print(per.op) else -> print(per.op)
} }
prettyPrintPerBD(per.right) prettyPrint(per.right)
} }
is UnaryPlus -> { is UnaryPlus -> {
print("(") print("(")
prettyPrintPerBD(per.arg) prettyPrint(per.arg)
print(".).plus()") print(".).plus()")
} }
is Const -> { is Const -> print(per.value)
print(per.value)
}
is Interest -> { is Interest -> {
print("Interest(") print("Interest(")
prettyPrintPerBD(per.amount) prettyPrint(per.amount)
print(", \"${per.dayCountConvention}\", ") print(", \"${per.dayCountConvention}\", ")
prettyPrintPerBD(per.amount) prettyPrint(per.amount)
print(", ") print(", ")
prettyPrintPerInstant(per.start) prettyPrint(per.start)
print(", ") print(", ")
prettyPrintPerInstant(per.end) prettyPrint(per.end)
print(")") print(")")
} }
is CurrencyCross -> { is CurrencyCross -> print("${per.foreign}/${per.domestic}")
print("${per.foreign}/${per.domestic}")
}
else -> println(per) else -> println(per)
} }
} }
@ -195,12 +179,10 @@ private class PrettyPrint(arr : Arrangement) {
prettyPrint(it) prettyPrint(it)
} }
} }
is Continuation -> { is Continuation -> println("next()")
println("next()")
}
is Obligation -> { is Obligation -> {
print("${partyMap[arr.from.owningKey]}.gives( ${partyMap[arr.to.owningKey]}, ") print("${partyMap[arr.from.owningKey]}.gives( ${partyMap[arr.to.owningKey]}, ")
prettyPrintPerBD(arr.amount) prettyPrint(arr.amount)
println(", ${arr.currency})") println(", ${arr.currency})")
} }
is Actions -> { is Actions -> {
@ -208,7 +190,7 @@ private class PrettyPrint(arr : Arrangement) {
indent { indent {
for ((name, condition, arrangement) in arr.actions) { for ((name, condition, arrangement) in arr.actions) {
print("\"$name\".givenThat(") print("\"$name\".givenThat(")
prettyPrintPerBoolean(condition) prettyPrint(condition)
println(") {") println(") {")
indent { indent {
prettyPrint(arrangement) prettyPrint(arrangement)