Skip to content

Latest commit

 

History

History
71 lines (54 loc) · 1.45 KB

File metadata and controls

71 lines (54 loc) · 1.45 KB

Overview

  1. Example of "idiomatic", Lombok, Java 8-17 POJO
  2. See also, detailed explanation
  3. See also, Jackson ready lombok POJO example
  4. See also, core Java POJO examples

POJO Example

/**
 * TODO: add a BRIEF description of purpose
 * TODO: if this POJO aligns with a DB table, mention that here
 */
@Value
@lombok.Builder(
    builderClassName = "Builder",
    toBuilder = true)
public class Employee {

  boolean active;

  /**
   * [16, 100]
   */
  int age;

  /**
   * [a-zA-Z-]{2,64}
   * eg. "Wil"
   */
  String name;

  LocalDate startDate;

  private Employee(
    Boolean active,  // <-- non-primitive allows you to default the value (see below)
    int age,
    String name,
    LocalDate startDate) {

    this.active = Optional.ofNullable(active).orElse(true);
    this.age = age;
    this.name = name;
    this.startDate = startDate;

    // -- Validations
    checkArgument(age <= 100, "age is too high: " + age);
    checkArgument(age >= 16, "age is too low: " + age);

    checkArgument(isNotBlank(name), "name is required");

    requireNonNull(startDate, "startDate is required and missing.");

    //TODO: other validations go here
  }
}

Usage Example

    Employee wil = Employee.newBuilder()
      .age(35)
      .name("Wil C")
      .startDate(LocalDate.of(2020, 01, 31))
      .build();

    // NOTE: wil.active is defaulted to true