Reputation: 73
I have some problems with mirgating my application from Java to Kotlin. I have this class in java, which works great:
public final class BooleanUtils extends org.apache.commons.lang3.BooleanUtils {
private BooleanUtils() {
}
/**
* Converts a String to Boolean.
* @param str the String to check
* @return true/false
*/
public static boolean toBoolean(final String str) {
return org.apache.commons.lang3.BooleanUtils.toBoolean(str) || "1".equals(str);
}
}
and after auto-transform in IntelijiIdea, it began to look like this
import org.apache.commons.lang3.BooleanUtils
object BooleanUtils : BooleanUtils() {
/**
* Converts a String to Boolean.
* @param str the String to check
* @return true/false
*/
fun toBoolean(str: String): Boolean {
return BooleanUtils.toBoolean(str) || "1" == str
}
}
But compiler tells me, that
Accidental override: The following declarations have the same JVM signature (toBoolean(Ljava/lang/String;)Z): fun toBoolean(p0: String?): Boolean defined in my.util.BooleanUtils fun toBoolean(str: String?): Boolean defined in my.util.BooleanUtils
How could I override fun toBoolean(str: String), to make it work like in Java?
Upvotes: 0
Views: 1169
Reputation: 308763
I'd prefer an extension to the String class:
fun String.boolean(): Boolean =
return when(this.toLowerCase()) {
"y", "yes", "t", "true", "1" -> true
else -> false
}
Upvotes: 1