Reputation: 5393
Is it somehow possible to make when
String
comparison case insensitive by default?
when (subtype.toLowerCase()) {
MessagingClient.RTC_SUBTYPE.sdp.toString().toLowerCase() -> onSDPMessageReceived(topic, sender, data!!)
MessagingClient.RTC_SUBTYPE.bye.toString().toLowerCase() -> onRTCBYEMessageReceived(topic, sender)
MessagingClient.RTC_SUBTYPE.negotiationOffer.toString().toLowerCase() -> onNegotiationOfferMessageReceived(sender, data!!)
}
This has too much repetetive code! Also note that MessagingClient.RTC_SUBTYPE
is enum class
and subtype
on the first line is received from some client, so we must treat it accordingly.
Upvotes: 6
Views: 4078
Reputation: 17
var s1 =editText.text.toString()
var s2=textView_.text
var s3 =s1.equals(s2 ,true)
Upvotes: -3
Reputation: 41608
I would convert subtype
to MessagingClient.RTC_SUBTYPE
like so:
val subtypeEnum = MessagingClient.RTC_SUBTYPE.values().firstOrNull {
it.name.equals(subtype, ignoreCase = true)
} ?: throw IllegalArgumentException("${subtype} no value of MessagingClient.RTC_SUBTYPE match")
And then use it in when
when (subtypeEnm) {
MessagingClient.RTC_SUBTYPE.sdp -> onSDPMessageReceived(topic, sender, data!!)
MessagingClient.RTC_SUBTYPE.bye -> onRTCBYEMessageReceived(topic, sender)
MessagingClient.RTC_SUBTYPE.negotiationOffer -> onNegotiationOfferMessageReceived(sender, data!!)
}
Upvotes: 9