ITEM 49: CHECK PARAMETERS FOR VALIDITY
229
In Java 9, a range-checking facility was added to
java.util.Objects
. This
facility consists of three methods:
checkFromIndexSize
,
checkFromToIndex
, and
checkIndex
. This facility is not as flexible as the null-checking method. It doesn’t
let you specify your own exception detail message, and it is designed solely for
use on list and array indices. It does not handle closed ranges (which contain both
of their endpoints). But if it does what you need, it’s a useful convenience.
For an unexported method, you, as the package author, control the
circumstances under which the method is called, so you can and should ensure
that only valid parameter values are ever passed in. Therefore, nonpublic methods
can check their parameters using
assertions,
as shown below:
Do'stlaringiz bilan baham: