Commit 20895484 authored by Trisha Gee's avatar Trisha Gee
Browse files

[appB] More examples records. Also, resurrected the Customer data class (old)...

[appB] More examples records. Also, resurrected the Customer data class (old) which somehow got replaced with a record.
parent eb38b63c
Loading
Loading
Loading
Loading
+2 −2
Changes for src/main/java/appB/Customer.java: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
package appB;

public record Customer(int id, String name) {
}
// This requires at least Java 16. Recommend using Java 17, as it's the current Long Term Support release.
public record Customer(int id, String name) { }
+12 −3
Changes for src/main/java/appB/CustomerWithValidation.java: 12 added lines, 3 removed lines.
Original line number Diff line number Diff line
package appB;

import java.util.Objects;

public record CustomerWithValidation(int id, String name) {
  public CustomerWithValidation {
    if (id < 0) {
@@ -7,11 +9,18 @@ public record CustomerWithValidation(int id, String name) {
    }
  }

  public int id() {
    return id;
  @Override
  public boolean equals(Object o) {
    return id == ((CustomerWithValidation) o).id;
  }

  private void checkEverythingIsOk() {
  @Override
  public int hashCode() {
    return Objects.hash(id);
  }

  private boolean isValidName(String name) {
    // some implementation
    return true;
  }
}
+45 −2
Changes for src/main/java/appB/old/Customer.java: 45 added lines, 2 removed lines.
Original line number Diff line number Diff line
package appB.old;

// This requires at least Java 16. Recommend using Java 17, as it's the current Long Term Support release.
public record Customer(int id, String name) { }
import java.util.Objects;

public class Customer {
  private final int id;
  private final String name;

  public Customer(int id, String name) {
    this.id = id;
    this.name = name;
  }

  public int getId() {
    return id;
  }

  public String getName() {
    return name;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }
    if (obj == null || obj.getClass() != this.getClass()) {
      return false;
    }
    var that = (Customer) obj;
    return this.id == that.id &&
           Objects.equals(this.name, that.name);
  }

  @Override
  public int hashCode() {
    return Objects.hash(id, name);
  }

  @Override
  public String toString() {
    return "Customer{" +
           "id=" + id +
           ", name='" + name + '\'' +
           '}';
  }
}