io.github.databob.PrimitiveGenerator.kt Maven / Gradle / Ivy
package io.github.databob
import java.lang.reflect.Type
import java.math.BigDecimal
import java.math.BigInteger
import java.util.*
class PrimitiveGenerator : Generator {
private val lookup: Map Any> = mapOf(
"string" to { d -> UUID.randomUUID().toString() },
"java.lang.String" to { d -> UUID.randomUUID().toString() },
"short" to { d -> Random().nextInt(Short.MAX_VALUE.toInt()).toShort() },
"java.lang.Short" to { d -> Random().nextInt(Short.MAX_VALUE.toInt()).toShort() },
"char" to { d -> Random().nextInt(256).toChar() },
"java.lang.Character" to { d -> Random().nextInt(256).toChar() },
"double" to { d -> Random().nextDouble() * Int.MAX_VALUE },
"java.lang.Double" to { d -> Random().nextDouble() * Int.MAX_VALUE },
"boolean" to { d -> d.mk().toss() },
"java.lang.Boolean" to { d -> d.mk().toss() },
"int" to { d -> d.mk().toInt() },
"java.lang.Integer" to { d -> d.mk().toInt() },
"long" to { d -> d.mk().toLong() },
"java.lang.Long" to { d -> d.mk().toLong() },
"byte" to { d -> d.mk().toByte() },
"java.lang.Byte" to { d -> d.mk().toByte() },
"float" to { d -> d.mk().toFloat() },
"java.lang.Float" to { d -> d.mk().toFloat() },
"java.math.BigInteger" to { d -> BigInteger.valueOf(d.mk()) },
"java.math.BigDecimal" to { d -> BigDecimal.valueOf(d.mk()) }
)
override fun mk(type: Type, databob: Databob): Any? = lookup[type.typeName]?.invoke(databob)
}