Tired of runtime errors from invalid integer values? With conventional Java code, we often rely on fragile
runtime checks and comments to enforce simple constraints:
/**
* Sets the month
*
* @param month the month number (1 = January, ..., 12 = December)
* @throws IllegalArgumentException if month is not between 1 and 12
*/
public void setMonth(int month) {
if (month < 1 || month > 12) {
throw new IllegalArgumentException("Month must be between 1 and 12.");
}
this.month = month;
}
What if the compiler could guarantee this logic for you? Introducing Concrete Numbers, a
groundbreaking Java library that brings a new level of compile-time safety to your numeric logic. Using
Concrete Numbers, the previous method can be redefined, eliminating the need for runtime checks.
/**
* Sets the month
*
* @param month the month number (One = January, ..., Twelve = December)
*/
public <K extends AtLeastOne & AtMostTwelve>
void setMonth(K month) {
this.month = month.intValue();
}
Concrete Numbers leverages advanced generics and a sophisticated type hierarchy to enforce
integer constraints directly within the type system. Define and validate integer values and ranges at compile time,
eliminating a significant class of runtime bugs and making your code demonstrably more robust.
Key Benefits
Unparalleled Compile-Time Safety: Catch invalid integer values and range violations before your code
even runs. No more IllegalArgumentException at runtime. The compiler becomes your
first line of defense against common numeric errors.
Robust APIs: Explicitly define exact integer values such as MinusOne,
Zero or Fourteen, or precise ranges such as
AtLeastFive & AtMostSixteen directly in your type signatures. This enhances code clarity and
self-documentation, making your contracts unambiguous.
Reduced Runtime Checks: With compile-time validation, you can eliminate many redundant runtime checks,
leading to cleaner code and potentially better performance.
Enhanced Readability: Type signatures become more expressive, conveying the intent and constraints of your
integer-based logic directly. This simplifies code review, reduces cognitive load, and makes your codebase easier to evolve.
Stronger Type Guarantees: Pushing the limits of Java generics, Concrete Numbers allows you to express
fine-grained integer constraints, providing guarantees about your numeric data that are traditionally difficult to achieve
in Java.
Ideal For
Domain Modeling: Encode numeric rules in business logic — ages, indices, dimensions.
API Development: Build expressive, defensive interfaces that can't be misused.
Configuration Safety: Ensure values fall within valid ranges before runtime.