Record Builder With Invariant Checks

Expose a fluent builder over an immutable record that validates invariants once at build time so invalid instances cannot exist.

JavaConfig Management

Code

snippet
import java.time.Duration;

public class RetryPolicy {

    public record Policy(int maxAttempts, Duration initialDelay, double backoffMultiplier) {
        public Policy {
            // 紧凑构造器:非法实例无法存在
            if (maxAttempts < 1 || maxAttempts > 10) {
                throw new IllegalArgumentException("maxAttempts must be 1..10: " + maxAttempts);
            }
            if (initialDelay.isNegative() || initialDelay.isZero()) {
                throw new IllegalArgumentException("initialDelay must be positive");
            }
            if (backoffMultiplier < 1.0) {
                throw new IllegalArgumentException("backoffMultiplier must be >= 1.0");
            }
        }

        public static Builder builder() {
            return new Builder();
        }

        public static final class Builder {
            private int maxAttempts = 3;
            private Duration initialDelay = Duration.ofMillis(200);
            private double backoffMultiplier = 2.0;

            public Builder maxAttempts(int value) {
                this.maxAttempts = value;
                return this;
            }

            public Builder initialDelay(Duration value) {
                this.initialDelay = value;
                return this;
            }

            public Builder backoffMultiplier(double value) {
                this.backoffMultiplier = value;
                return this;
            }

            public Policy build() {
                return new Policy(maxAttempts, initialDelay, backoffMultiplier);
            }
        }
    }

    public static void main(String[] args) {
        RetryPolicy.Policy policy = RetryPolicy.Policy.builder()
                .maxAttempts(5)
                .initialDelay(Duration.ofMillis(100))
                .build();
        System.out.println(policy);

        try {
            RetryPolicy.Policy.builder().maxAttempts(0).build();
        } catch (IllegalArgumentException e) {
            System.out.println("rejected: " + e.getMessage());
        }
    }
}

How it works

Record Builder With Invariant Checks reduces to the classes RetryPolicy, Policy and Builder and the functions Policy, builder, maxAttempts and initialDelay.

The failure path is explicit — IllegalArgumentException is raised when the guard condition holds.

Using it

The signature you will reach for first is public record Policy(int maxAttempts, Duration initialDelay, double backoffMultiplier)

The demo output is labeled (rejected:), so every line in the printed output names the step that produced it.

Execute the file and main walks through builder, maxAttempts, initialDelay and build, in that order.

Key statements

throw new IllegalArgumentException("maxAttempts must be 1..10: " + maxAttempts);
throw new IllegalArgumentException("backoffMultiplier must be >= 1.0");
throw new IllegalArgumentException("initialDelay must be positive");

Interactive Tools for This Topic

FAQ

How does the code behave when input is bad?
It throws `IllegalArgumentException` before any real work runs — handle it at the call site or let it propagate deliberately.
What does Record Builder With Invariant Checks print when run?
The script runs standalone; its 1 labeled outputs include `rejected:`.