Skip to content

Instantly share code, notes, and snippets.

Avatar
💙
Kotlin all the things

Simon Wirtz s1monw1

💙
Kotlin all the things
View GitHub Profile
View dto.kt
data class PageableDTO(val offset: Int, val limit: Int, val sortBy: String? = null, val ascending: Boolean? = null)
fun PageableDTO.injectDefaults() =
copy(sortBy = if (sortBy?.isNotEmpty() == true) sortBy else "created_ts", ascending = ascending ?: true)
fun main(args: Array<String>) {
val pageableDTO = PageableDTO(1, 2)
val withDefaults = pageableDTO.injectDefaults()
}
@s1monw1
s1monw1 / hello.kt
Created Apr 11, 2018
Kotlin Hello World
View hello.kt
fun main(args: Array<String>) {
println("Hello Kotlin World")
}
View bintray-build.gradle.kts
import com.github.jengelman.gradle.plugins.shadow.ShadowApplicationPlugin
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import com.jfrog.bintray.gradle.BintrayPlugin
import org.jetbrains.kotlin.gradle.dsl.Coroutines
import org.gradle.api.publish.maven.MavenPom
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import com.jfrog.bintray.gradle.BintrayExtension
val kotlinVersion = plugins.getPlugin(KotlinPluginWrapper::class.java).kotlinPluginVersion
View LazyContextAware.kt
class LazyContextAware<out T>(private val initializer: (Any?) -> T) : ReadOnlyProperty<Any?, T> {
private object UNINITIALIZED_VALUE
private var prop: Any? = UNINITIALIZED_VALUE
@Suppress("UNCHECKED_CAST")
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
return if (prop == UNINITIALIZED_VALUE) {
synchronized(this) {
return if (prop == UNINITIALIZED_VALUE) {
@s1monw1
s1monw1 / build.grade.kts
Created Dec 1, 2017
Blog Post Example Script
View build.grade.kts
import org.gradle.kotlin.dsl.extra
import org.jetbrains.kotlin.gradle.dsl.Coroutines
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
val kotlinVersion = plugins.getPlugin(KotlinPluginWrapper::class.java).kotlinPluginVersion
val kotlinCoroutinesVersion = "0.19.3"
val vertxVersion = "3.5.0"
val nexusRepo = "http://x.x.x.x:8080/nexus/content/repositories/releases"
View build.gradle
buildscript {
ext.kotlin_version = '1.1.60'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
View SingleExpFun.java
public class SingleExpFun {
private BooleanSupplier trueOrFalse = new Random()::nextBoolean;
private boolean getNext(){
return trueOrFalse.getAsBoolean();
}
}
View sum.kt
val total = employees.sumBy { it.salary }
View Sum.java
int total = employees.stream()
.collect(Collectors.summingInt(Employee::getSalary)));
View map.kt
val list = people.map { it.name }
You can’t perform that action at this time.