Skip to content

Instantly share code, notes, and snippets.

View abohomol's full-sized avatar

Anton Bohomol abohomol

  • San Francisco Bay Area
View GitHub Profile
interface CommunicationChannel {
@Throws(IOException::class)
fun connect(credentials: Credentials)
fun disconnect()
fun getChannelDescription(): Description
}
fun List<String>.filterValid(): List<String>
@JvmName("filterValidInt")
fun List<Int>.filterValid(): List<Int>
...
@JvmStatic fun newIntent(context: Context): Intent {
return Intent(context, SomeActivity::class.java)
}
/* Now you can omit Companion reference in Java: */
Intent intent = SomeActivity.newIntent(context)
class SomeActivity : Activity() {
...
companion object {
fun newIntent(context: Context): Intent {
return Intent(context, SomeActivity::class.java)
}
}
}
@file:JvmName("StringUtils")
/* Method declared in StringExtensions.kt: */
public inline fun String.capitalize(): String {
...
}
/* Will be translated into the following call in Java: */
String lowercase = ...
String capitalized = StringExtensionsKt.capitalize(lowercase)
@ExperimentalContracts
fun notNull(reference: Any?) {
contract {
returns() implies (reference != null)
}
if (reference == null) throw NullPointerException()
}
/* Now you can use the methid as follows: */
fun getLength(instance: Any): Int {
return if (instance is String) {
instance.length
} else if (instance is Text) {
instance.size()
} else {
0
}
}
public static <T> T checkNotNull(T reference) {
if (reference == null) {
throw new NullPointerException();
}
return reference;
}
launch(UI) {
val service = ...
val profile = service.getProfile() // background thread
view.showUserName(profile.name) // UI thread
finalizeOnboarding() // background thread
}