A Range annotation declares explicit lower and upper bounds for a numeric value or parameter. Used on an index parameter, it documents the valid interval and can help static analyzers flag invalid calls before an IndexOutOfBoundsException occurs. section 5.2
Annotating a nonnegative index
Use the JetBrains annotation package to express a nonnegative int parameter with an explicit range: @Range(from = 0, to = Integer.MAX_VALUE) int index This is equivalent to a @NonNegative annotation for an int parameter. A static analyzer may warn when a caller supplies a negative argument, but callers should still have their bounds checked immediately. section 5.2
Use range annotations to:
- Document permitted numeric bounds directly on parameters. section 5.2
- Aid static analysis during development rather than relying only on runtime failures. section 5.2
- Keep immediate runtime bounds checks when accepting indexes from clients. section 5.2
