Is there an easy way/library to check and adjust parameters to stay inside the lists boundaries?
Here a long sample:
if (fromIndex < 0) {
fromIndex = 0;
}
if (fromIndex > list.size() - 1) {
fromIndex = list.size() - 1;
}
if (toIndex < 0) {
toIndex = 0;
}
if (toIndex > list.size() - 1) {
toIndex = list.size() - 1;
}
list.subList(fromIndex, toIndex);
I know i could move list.size() - 1
to a variable and do an extract-method on the index checks to remove redundant code. But it still seems a bit verbose for this simple task.