An enum models a fixed set of meaningful named values instead of leaving domain semantics inside serialized strings. In Java, it gives the type system a direct representation of those allowed choices. section 3.3.3 section 8.3
Modeling a curing method
A sample identifier need not remain a string such as "UV-1". Split its meaning into domain types: `enum CuringMethod { HEAT, AIR, UV }`, then store a `CuringMethod` together with a nonnegative sample number in `SampleId`. Grouping measurements by `m.sampleId().method()` now expresses the domain directly, rather than parsing string internals. section 3.3.3 section 3.3.3
Restricting rule attributes
A rule represented as `new Equals("county", "US")` accepts misspelled or nonexistent attributes. Replacing the field with `Attribute` and defining `enum Attribute { REGION, COUNTRY, SECTOR, SEGMENT, CHANNEL }` narrows the model to known account attributes. section 8.3 section 8.3
Use an enum when:
- A domain concept has a fixed, named set of values. section 3.3.3
- A wide-open string would permit typos or values that do not exist in the model. section 8.3
- Business-rule data should refer to known attributes rather than serialized labels. section 8.3
Listing 3.27 Capturing the meaning of Sample ID
record NonNegativeInt(int value){/*...*/} #A
enum CuringMethod {HEAT, AIR, UV} #B
record SampleId(
CuringMethod method, #C
NonNegativeInt sampleNum #C
) {
SampleId {
Objects.requireNonNull(method); #D
Objects.requireNonNull(sampleNum); #D
}
} Inspect the complete typed SampleId model, including the enum, constrained number, and null checks. section 3.3.3
























