Skip to content

Instantly share code, notes, and snippets.

View sliskiCode's full-sized avatar
:octocat:

Piotr Ślesarew sliskiCode

:octocat:
View GitHub Profile
@sliskiCode
sliskiCode / Person.java
Created October 21, 2016 10:23
Builders in Kotlin. Gist 1
import lombok.Builder;
@Builder
class Person {
private final String name;
private final String surname;
private final int age;
}
@sliskiCode
sliskiCode / Person.kt
Created October 21, 2016 13:01
Builders in Kotlin. Gist 2
class Person(val name: String, val surname: String, val age: Int) {
companion object {
class Builder {
private var name: String = ""
private var surname: String = ""
private var age: Int = 0
fun name(name: String): Builder {
this.name = name
return this
@sliskiCode
sliskiCode / Person.kt
Created October 21, 2016 13:35
Builders in Kotlin. Gist 3
class Person private constructor(val name: String, val surname: String, val age: Int) {
private constructor(builder: Builder) : this(builder.name, builder.surname, builder.age)
companion object {
fun create(init: Builder.() -> Unit) = Builder(init).build()
}
class Builder private constructor() {
@sliskiCode
sliskiCode / TimeTest.kt
Created October 24, 2016 09:16
Builders in Kotlin. Gist 4
fun main(args: Array<String>) {
measureTime {
Person.create {
name { "Peter" }
surname { "Slesarew" }
age { 28 }
}
}
// OR
@sliskiCode
sliskiCode / Person.kt
Created October 25, 2016 08:43
Builders in Kotlin. Gist 5
val withTitle: Boolean = true
// More concise
Person.create {
name { if (withTitle) "Mr. Peter" else "Peter" }
surname { "Slesarew" }
age { 28 }
}
// It does not even compile
@sliskiCode
sliskiCode / Person.kt
Last active October 25, 2016 09:16
Builders in Kotlin. Gist 6
class Person(val name: String, val surname: String, val age: Int = 0)
Person(name = "Peter", surname = "Slesarew") // <- name, surname are mandatory
Person(name = "Peter") // <- it does not compile
Person(surname = "Slesarew") // <- it does not compile
Person(age = 28) // <- it does not compile
@sliskiCode
sliskiCode / CopyPrinter.java
Created October 28, 2016 12:37
Zero boilerplate delegation in Kotlin 1
class CopyPrinter implements Copy, Print {
private Copy copier;
private Print printer;
public CopyPrinter(Copy copier, Print printer) {
this.copier = copier;
this.printer = printer;
}
@sliskiCode
sliskiCode / CopyPrinter.kt
Last active October 28, 2016 14:04
Zero boilerplate delegation in Kotlin 2
class CopyPrinter(copier: Copy, printer: Print) : Copy by copier, Print by printer
interface Copy {
fun copy(page: Page): Page
}
interface Print {
fun print(page: Page)
}
@sliskiCode
sliskiCode / CopyPrinter.java
Last active October 29, 2016 10:57
Zero boilerplate delegation in Kotlin 3
public final class CopyPrinter implements Copy, Print {
// $FF: synthetic field
private final Copy $$delegate_0;
// $FF: synthetic field
private final Print $$delegate_1;
public CopyPrinter(@NotNull Copy copier, @NotNull Print printer) {
Intrinsics.checkParameterIsNotNull(copier, "copier");
Intrinsics.checkParameterIsNotNull(printer, "printer");
super();
@sliskiCode
sliskiCode / Extensions.kt
Last active November 4, 2016 15:20
Deep dive in Kotlin extensions 1
import org.junit.Assert.assertEquals
import org.junit.Test
fun String.removeWhitespaces() = replace(" ", "")
class StringKtTest {
@Test fun removesWhitespacesFromString() {
val tested = "Piotr Slesarew"
val actual = tested.removeWhitespaces()