I am writing a small library where I have some interface
providing a method where the return value should be in a specified range. How can I explicitly forbid users of my library who implement this method to return a value not in this range?
Something like this:
//Library
interface FavoriteNumber {
//returned value must lie between 0 and 10
double whatsYourFavoriteNumberBetweenZeroAndTen();
}
...
//Classes implemented by user of library
class ILikePi implements FavoriteNumber {
@Override
public double whatsYourFavoriteNumberBetweenZeroAndTen() {
return 3.141; //Should be allowed
}
}
...
class AnswerToLifeTheUniverseAndEverything implements FavoriteNumber {
@Override
public double whatsYourFavoriteNumberBetweenZeroAndTen() {
return 42; //Should be forbidden
}
}
I think I could write something like
class DoubleBetweenZeroAndTen {
private final double value;
DoubleBetweenZeroAndTen(double value) {
if (value < 0 || value > 10) {
throw new IllegalArgumentException("value must be between 0 and 10");
}
this.value = value;
}
double toDouble() {
return this.value;
}
}
and return this instead of the double
but this doesn't feel good enough, since it is a double
between 0 and 10 that you want to work with thereafter, and not a DoubleBetweenZeroAndTen
.
If it is not possible to forbid this explicitly, what is the best way to ensure the user won't violate it? (Right now, I have a notice in the javadoc.)