This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.Builder; | |
@Builder | |
class Person { | |
private final String name; | |
private final String surname; | |
private final int age; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(args: Array<String>) { | |
measureTime { | |
Person.create { | |
name { "Peter" } | |
surname { "Slesarew" } | |
age { 28 } | |
} | |
} | |
// OR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val withTitle: Boolean = true | |
// More concise | |
Person.create { | |
name { if (withTitle) "Mr. Peter" else "Peter" } | |
surname { "Slesarew" } | |
age { 28 } | |
} | |
// It does not even compile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CopyPrinter implements Copy, Print { | |
private Copy copier; | |
private Print printer; | |
public CopyPrinter(Copy copier, Print printer) { | |
this.copier = copier; | |
this.printer = printer; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
OlderNewer