Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Data classes in Kotlin

Data classes in Kotlin

5-minute talk about data classes in Kotlin

Harri Kirik

April 02, 2018
Tweet

More Decks by Harri Kirik

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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