public void testArrayPosition(final String[] strArray, final int indexNumber)
{
final int index = checkPositionIndex(indexNumber, strArray.length, "String array index number");
}
public void givenArrayAndMsg_whenCheckPositionEvalsFalse_throwsException() {
int[] numbers = { 1, 2, 3, 4, 5 };
String message = "Please check the bound of an array and retry";
assertThatThrownBy(
() -> Preconditions.checkPositionIndex(6, numbers.length - 1, message))
.isInstanceOf(IndexOutOfBoundsException.class)
.hasMessageStartingWith(message).hasNoCause();
}
public static int validatePosIndexAndGetValue(int[] array, int pos) {
checkPositionIndex(pos, array.length);
return array[pos -1];
}
public void testCheckPositionIndex_ok() {
assertEquals(0, Preconditions.checkPositionIndex(0, 0));
assertEquals(0, Preconditions.checkPositionIndex(0, 1));
assertEquals(1, Preconditions.checkPositionIndex(1, 1));
}
public void testCheckPositionIndex_withDesc_negative() {
try {
Preconditions.checkPositionIndex(-1, 1, "foo");
fail();
} catch (IndexOutOfBoundsException expected) {
assertThat(expected).hasMessage("foo (-1) must not be negative");
}
}
static <T> UnmodifiableListIterator<T> forArray(
final T[] array, final int offset, int length, int index) {
checkArgument(length >= 0);
int end = offset + length;
// Technically we should give a slightly more descriptive error on overflow
Preconditions.checkPositionIndexes(offset, end, array.length);
Preconditions.checkPositionIndex(index, length);
if (length == 0) {
return emptyListIterator();
}
return new ArrayItr<T>(array, offset, length, index);
}
public boolean setPage(int page) {
Preconditions.checkPositionIndex(page, messages.size());
BakedMessage message = messages.get(page);
if (sentMessage != null) {
message.update(sentMessage);
}
this.page = page;
this.lastUpdate = System.currentTimeMillis();
return true;
}
public float getDimension(int index) {
Preconditions.checkPositionIndex(index, this.size());
return dimensions[index];
}
public TimeSeriesPoint getPoint(int index) {
Preconditions.checkPositionIndex(index, points.size());
return points.get(index);
}
int getEntryAt(int pos) {
Preconditions.checkPositionIndex(pos, entries.length,
"Invalid position for AclEntry");
return entries[pos];
}