Java Clean Architecture Masterclass

Java Clean Architecture Masterclass28-29 May

Join

CI Sonatype Central License

imcover.png

Full documentation at immutables.org

Record Builder

@Value.Builder
record Person(String name, int age, String email) {}

// Use the generated builder
Person person = new PersonBuilder()
    .name("Alice")
    .age(30)
    .email("alice@example.com")
    .build();

More fancy example having copy-with methods generated, and style withUnaryOperator="with*"

@Value.Builder
record Person(String name, int age) implements WithPerson {
  // Extend the generated PersonBuilder
  static class Builder extends PersonBuilder {}
}

// Use your custom builder
var person = new Person.Builder()
    .name("Bob")
    .age(18)
    .build();

person = person.withName("Bobby!")
    .withAge(age -> age + 3);

Immutable class

Minimal, classical style

@Value.Immutable
interface Book {
  String isbn();
  String title();
  List<String> authors();
}

ImmutableBook book = ImmutableBook.builder()
    .isbn("978-1-56619-909-4")
    .title("The Elements of Style")
    .addAuthors("William Strunk Jr.", "E.B. White.")
    .build();

"sandwich" style, with nested builder and extending With* interface

// Define abstract value type using interface, abstract class or annotation
@Value.Immutable
public interface ValueObject extends WithValueObject {
  // WithValueObject is not yet generated, We extend With* to inherit `with*` method signatures
  String name();
  List<Integer> counts();
  Optional<String> description();

  class Builder extends ImmutableValueObject.Builder {}
  // ImmutableValueObject.Builder will be generated and
  // our builder will inherit and reexport methods as its own.
  // Static nested Builder will inherit all the public method
  // signatures of ImmutableValueObject.Builder
} 

// Use generated immutable implementation and builder
var value = new ValueObject.Builder()
    .name("Nameless")
    .description("present")
    .addCounts(1)
    .addCounts(2)
    .build();

value = value.withName("Doe");

//fetch values via accessors
List<Integer> counts = v.counts();
Optional<String> description = v.description();

Changelog

See releases tab for release history. Archived changelog for earlier releases.

Join libs.tech

...and unlock some superpowers

GitHub

We won't share your data with anyone else.