Wednesday, July 10, 2019

Kotlin in Action: Chapter 4 Exercises

Alright, so just like last week, I'm posting a Java to Kotlin conversion exercise where the Java code will be posted now and the Kotlin code will be posted in a follow up post. After reading through chapter 4 of Kotlin in Action, you should be prepared to complete the exercise.

Exercise 1: Json to Pojo and Back Again

Taking advantage of the Gson library, we'll convert some json into JsonObjects, which we will then take and convert into User pojos, run some simple tests, and then convert the pojos back to JsonObjects again. (I know that Gson supports converting json directly to pojos, but for the sake of the exercise, we'll ignore that and instead code it by hand.)
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.HashSet;

public class JavaExample3 {

  public static void main(String[] args) {
    String johnJson = "{\n" +
                      "  \"username\": \"john123\",\n" +
                      "  \"firstName\": \"John\",\n" +
                      "  \"lastName\": \"Smith\",\n" +
                      "  \"email\": \"john@smith.com\"\n" +
                      "}";
    String janeJson = "{\n" +
                      "  \"username\": \"janedoe5\",\n" +
                      "  \"firstName\": \"Jane\",\n" +
                      "  \"lastName\": \"Doe\",\n" +
                      "  \"email\": \"jane.doe@gmail.com\"\n" +
                      "}";

    JsonParser parser = new JsonParser();
    JsonObject johnJsonObject = parser.parse(johnJson).getAsJsonObject();
    JsonObject janeJsonObject = parser.parse(janeJson).getAsJsonObject();

    User john1 = User.fromJson(johnJsonObject);
    User john2 = User.fromJson(johnJsonObject);
    User jane = User.fromJson(janeJsonObject);

    System.out.println(john1);
    System.out.println(john2);
    System.out.println(jane);

    System.out.println("john1 = john2: " + john1.equals(john2));
    System.out.println("john1 = jane: " + john1.equals(jane));

    HashSet<User> usersSet = new HashSet<>();
    usersSet.add(john1);
    usersSet.add(john2);
    usersSet.add(jane);

    System.out.println("HashSet size (expected 2): " + usersSet.size());

    JsonObject johnFinal = john1.toJson();
    JsonObject janeFinal = jane.toJson();

    System.out.println(johnFinal);
    System.out.println(janeFinal);
  }
}

import com.google.gson.JsonObject;

import java.util.Objects;

public class User {

  public static User fromJson(JsonObject jsonObject) {
    return new User(jsonObject.get("username").getAsString(),
                    jsonObject.get("firstName").getAsString(),
                    jsonObject.get("lastName").getAsString(),
                    jsonObject.get("email").getAsString());
  }

  private final String username;
  private final String firstName;
  private final String lastName;
  private final String email;

  public User(String username,
              String firstName,
              String lastName,
              String email) {
    this.username = username;
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
  }

  public String getUsername() {
    return username;
  }

  public String getFirstName() {
    return firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public String getEmail() {
    return email;
  }

  @Override
  public String toString() {
    return "User(username=" + username +
        ", firstName=" + firstName +
        ", lastName=" + lastName +
        ", email=" + email + ")";
  }

  @Override
  public boolean equals(Object obj) {
    if (obj instanceof User) {
      User other = (User) obj;
      return equals(username, other.username) &&
          equals(firstName, other.firstName) &&
          equals(lastName, other.lastName) &&
          equals(email, other.email);
    }
    return false;
  }

  @Override
  public int hashCode() {
    return Objects.hash(username, firstName, lastName, email);
  }

  private boolean equals(String str1, String str2) {
    return (str1 == null && str2 == null) ||
        (str1 != null && str1.equals(str2));
  }

  public JsonObject toJson() {
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("username", username);
    jsonObject.addProperty("firstName", firstName);
    jsonObject.addProperty("lastName", lastName);
    jsonObject.addProperty("email", email);
    return jsonObject;
  }
}
Answers can be found here.