import lombok.NonNull; public class WithExample { private @NonNull final String name; private final int age; public WithExample(String name, int age) { if (name == null) throw new NullPointerException(); this.name = name; this.age = age; } protected WithExample withName(@NonNull String name) { if (name == null) throw new java.lang.NullPointerException("name"); return this.name == name ? this : new WithExample(name, age); } public WithExample withAge(int age) { return this.age == age ? this : new WithExample(name, age); } }