Slide 1

Slide 1 text

designing & developing for mobile Demoday, 02/04/2018 Harri Kirik, engineer Kotlin: Data classes

Slide 2

Slide 2 text

Kotlin KOTLIN: DATA CLASSES #kotlin

Slide 3

Slide 3 text

Classes to hold data KOTLIN: DATA CLASSES #dataclasses

Slide 4

Slide 4 text

Usually in Java .. KOTLIN: DATA CLASSES #kotlin

Slide 5

Slide 5 text

KOTLIN: DATA CLASSES public class Person { private String customerEmail; private String customerPhoneNr; private String customerSurname; private String customerSurnameReading; private String customerGivenName; private String customerGivenNameReading; private String customerCompany; private String customerCompanyDepartment; public Person() { } public void setCustomerEmail(String customerEmail) { this.customerEmail = customerEmail; } public void setCustomerPhoneNr(String customerPhoneNr) { this.customerPhoneNr = customerPhoneNr; } #demoday

Slide 6

Slide 6 text

KOTLIN: DATA CLASSES public void setCustomerSurname(String customerSurname) { this.customerSurname = customerSurname; } public void setCustomerSurnameReading(String customerSurnameReading) { this.customerSurnameReading = customerSurnameReading; } public void setCustomerGivenName(String customerGivenName) { this.customerGivenName = customerGivenName; } public void setCustomerGivenNameReading(String customerGivenNameReading) { this.customerGivenNameReading = customerGivenNameReading; } public void setCustomerCompany(String customerCompany) { this.customerCompany = customerCompany; } public void setCustomerCompanyDepartment(String customerCompanyDepartment) { this.customerCompanyDepartment = customerCompanyDepartment; } #demoday

Slide 7

Slide 7 text

KOTLIN: DATA CLASSES @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; if (customerEmail != null ? !customerEmail.equals(person.customerEmail) : person.customerEmail != null) return false; if (customerPhoneNr != null ? !customerPhoneNr.equals(person.customerPhoneNr) : person.customerPhoneNr != null) return false; if (customerSurname != null ? !customerSurname.equals(person.customerSurname) : person.customerSurname != null) return false; if (customerSurnameReading != null ? !customerSurnameReading.equals(person.customerSurnameReading) : person.customerSurnameReading != null) return false; if (customerGivenName != null ? !customerGivenName.equals(person.customerGivenName) : person.customerGivenName != null) return false; if (customerGivenNameReading != null ? !customerGivenNameReading.equals(person.customerGivenNameReading) : person.customerGivenNameReading != null) return false; if (customerCompany != null ? !customerCompany.equals(person.customerCompany) : person.customerCompany != null) return false; return customerCompanyDepartment != null ? customerCompanyDepartment.equals(person.customerCompanyDepartment) : person.customerCompanyDepartment == null; } #demoday

Slide 8

Slide 8 text

KOTLIN: DATA CLASSES @Override public int hashCode() { int result = customerEmail != null ? customerEmail.hashCode() : 0; result = 31 * result + (customerPhoneNr != null ? customerPhoneNr.hashCode() : 0); result = 31 * result + (customerSurname != null ? customerSurname.hashCode() : 0); result = 31 * result + (customerSurnameReading != null ? customerSurnameReading.hashCode() : 0); result = 31 * result + (customerGivenName != null ? customerGivenName.hashCode() : 0); result = 31 * result + (customerGivenNameReading != null ? customerGivenNameReading.hashCode() : 0); result = 31 * result + (customerCompany != null ? customerCompany.hashCode() : 0); result = 31 * result + (customerCompanyDepartment != null ? customerCompanyDepartment.hashCode() : 0); return result; } #demoday

Slide 9

Slide 9 text

KOTLIN: DATA CLASSES @Override public String toString() { return "Person{" + "customerEmail='" + customerEmail + '\'' + ", customerPhoneNr='" + customerPhoneNr + '\'' + ", customerSurname='" + customerSurname + '\'' + ", customerSurnameReading='" + customerSurnameReading + '\'' + ", customerGivenName='" + customerGivenName + '\'' + ", customerGivenNameReading='" + customerGivenNameReading + '\'' + ", customerCompany='" + customerCompany + '\'' + ", customerCompanyDepartment='" + customerCompanyDepartment + '\'' + '}'; } } #demoday

Slide 10

Slide 10 text

Easy to generate, but tedious KOTLIN: DATA CLASSES #dataclasses

Slide 11

Slide 11 text

Kotlin can do better KOTLIN: DATA CLASSES #dataclasses

Slide 12

Slide 12 text

KOTLIN: DATA CLASSES data class Person(val customerEmail: String, val customerPhoneNr: String, val customerSurname: String, val customerSurnameReading: String, val customerGivenName: String, val customerGivenNameReading: String, val customerCompany: String, val customerCompanyDepartment: String) #demoday

Slide 13

Slide 13 text

Autogenerated: 1. equals()/hashCode() pair; 2. toString() 3. componentN() functions 4. copy() function KOTLIN: DATA CLASSES #dataclasses

Slide 14

Slide 14 text

conponentN() functions KOTLIN: DATA CLASSES #dataclasses

Slide 15

Slide 15 text

KOTLIN: DATA CLASSES val person = Person( "[email protected]", // <- getComponent1() "+37256240131", //<- getComponent2() "Kirik", //<- getComponent3() "Kirik", //<- getComponent4() "Harri", //<- getComponent5() "Harri", //<- getComponent6() "Mobi Lab", //<- getComponent7() "Development"); //<- getComponent1() val (_, _,surname, _, givenName) = person // (C1), (C2), C3, C4 #demoday

Slide 16

Slide 16 text

copy() function KOTLIN: DATA CLASSES #dataclasses

Slide 17

Slide 17 text

KOTLIN: DATA CLASSES val person = Person("[email protected]", "+37256240131", "Kirik", "Kirik", "Harri", "Harri", "Mobi Lab", "Development"); val personFired = person.copy( customerEmail = "[email protected]", customerCompany = "N/A", customerCompanyDepartment = "N/A") #demoday

Slide 18

Slide 18 text

http:// lab.mobi designing & developing for mobile thanks Questions?