I have this class:
data class BluetoothCommand(
val characteristic: DeviceUUIDs.Characteristic,
override val code: Int,
val commandArgument: CommandArgument<*>,
val description: String,
private val successResponse: BluetoothSuccessResponse = BluetoothSuccessResponse.Default,
val errorResponse: BluetoothErrorResponse = BluetoothErrorResponse.Default,
val msgSize: Int? = null,
val hidden: Boolean = false,
val debug: Boolean = false
) : WithCode {
constructor(
characteristic: DeviceUUIDs.Characteristic,
code: WithCode,
commandArgument: CommandArgument<*>,
description: String,
successResponse: BluetoothSuccessResponse = BluetoothSuccessResponse.Default,
errorResponse: BluetoothErrorResponse = BluetoothErrorResponse.Default,
msgSize: Int? = null,
hidden: Boolean = false,
debug: Boolean = false
) : this(characteristic, code.code, commandArgument, description, successResponse, errorResponse, msgSize, hidden, debug)
fun successResponse(): BluetoothSuccessResponse {
return msgSize?.let { size ->
if (successResponse is BluetoothSuccessResponse.Bytes) {
BluetoothSuccessResponse.Bytes { msg, args ->
if (msg.size != size) errorSnackBar("Malformed message: it has length ${msg.size} bytes, expected $msgSize bytes!")
else with(successResponse.bytesBluetoothResponse) { processData(msg, args) }
}
} else null
} ?: successResponse
}
}
I would like msgSize
to be either an Int
or a lambda taking an Int
and returning a Boolean
. Normally, I would simply declare two functions with different signatures, but in this case, I’ve already done that to have code
be either an Int
or a WithCode
object. If I did the same thing again, I would end up with 4 versions of the same constructor. I could create a new object which takes an Int
or a (Int) -> Boolean
and use that, but I have a big list of BluetoothCommand
s hard coded and I’m trying to make the process of writing said list as easy as possible: having a bunch of new objects representing a simple Int
would be a bit annoying. What can I do?