Building on the foundation of clean record validation
Java Records transformed how we handle immutable data in Java, but they left us with one challenge:
creating builder patterns for complex object construction. While Records are perfect for simple data
holders, real-world applications often need the flexibility of the builder pattern.
In
my ?
Record Companion integrates seamlessly with it.
Add the @ValidCheck annotation alongside @Builder:
@Builder
@ValidCheck
public record Product(String name, BigDecimal price, int quantity) {
public Product {
// Clean validation using ValidCheck
ProductCheck.check()
.checkName(name, StringValidator::notNullOrEmpty)
.checkPrice(price, value -> value
.notNull()
.satisfies(p -> p.compareTo(BigDecimal.ZERO) > 0, "must be positive"))
.checkQuantity(quantity, value -> value
.isNonNegative()
.max(1000))
.validate();
}
}
Now you have both builder pattern AND clean validation:
// Builder pattern with automatic validation
Product product = ProductBuilder.builder()
.name("Laptop")
.price(new BigDecimal("999.99"))
.quantity(5)
.build(); // Validation happens automatically
// Functional updates with validation
Product discounted = ProductBuilder.with(product, updater ->
updater.price(new BigDecimal("799.99"))
); // Re-validates with new price
Getting Started
Add Record Companion to your project:
Maven:
<dependency>
<groupId>io.github.record-companion</groupId>
<artifactId>record-companion-processor</artifactId>
<version>0.1.0</version>
<scope>provided</scope>
</dependency>
Gradle:
annotationProcessor 'io.github.record-companion:record-companion-processor:0.1.0'
compileOnly 'io.github.record-companion:record-companion-processor:0.1.0'
That's it. No configuration files, no complex setup.
Alternatives
If you're looking for more features,
What's your experience with builder patterns in Java? Have you tried Record Companion? Share your
thoughts in the comments below!
Building better Java, one record at a time.
SOCIAL SHARE CARD GENERATOR