/** * Constructs an iterator across a sequence of the given size with the given initial position. * That is, the first call to {@link #nextIndex()} will return {@code position}, and the first * call to {@link #next()} will return the element at that index, if available. Calls to {@link * #previous()} can retrieve the preceding {@code position} elements. * * @throws IndexOutOfBoundsException if {@code position} is negative or is greater than {@code * size} * @throws IllegalArgumentException if {@code size} is negative */ protected AbstractIndexedListIterator(int size, int position) { checkPositionIndex(position, size); this.size = size; this.position = position; }
/** * Returns a list iterator containing the elements in the specified range of {@code array} in * order, starting at the specified index. * * <p>The {@code Iterable} equivalent of this method is {@code * Arrays.asList(array).subList(offset, offset + length).listIterator(index)}. */ 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); }
@Override public int indexIn(CharSequence sequence, int start) { int length = sequence.length(); checkPositionIndex(start, length); return (start == length) ? -1 : start; }
@Override public int indexIn(CharSequence sequence, int start) { int length = sequence.length(); checkPositionIndex(start, length); return -1; }
@Override public void add(int index, V element) { checkPositionIndex(index, 0); throw new IllegalArgumentException("Key does not satisfy predicate: " + key); }
private int reversePosition(int index) { int size = size(); checkPositionIndex(index, size); return size - index; }
@CanIgnoreReturnValue @Override public boolean addAll(int index, Collection<? extends V> elements) { checkNotNull(elements); checkPositionIndex(index, 0); throw new IllegalArgumentException("Key does not satisfy predicate: " + key); }
/** * Returns a new {@link ByteArrayDataInput} instance to read from the {@code bytes} array, * starting at the given position. * * @throws IndexOutOfBoundsException if {@code start} is negative or greater than the length of * the array */ public static ByteArrayDataInput newDataInput(byte[] bytes, int start) { checkPositionIndex(start, bytes.length); return newDataInput(new ByteArrayInputStream(bytes, start, bytes.length - start)); }
/** * Ensures that {@code index} specifies a valid <i>position</i> in an array, list or string of * size {@code size}. A position index may range from zero to {@code size}, inclusive. * * @param index a user-supplied index identifying a position in an array, list or string * @param size the size of that array, list or string * @return the value of {@code index} * @throws IndexOutOfBoundsException if {@code index} is negative or is greater than {@code size} * @throws IllegalArgumentException if {@code size} is negative */ @CanIgnoreReturnValue public static int checkPositionIndex(int index, int size) { return checkPositionIndex(index, size, "index"); }
@Override public Type getType(int field) { checkPositionIndex(field, delegateFieldCount + appendedTypes.size()); if (field < delegateFieldCount) { return delegate.getType(field); } else { return appendedTypes.get(field - delegateFieldCount); } }
@Override public long getLong(int field) { checkPositionIndex(field, delegateFieldCount + appendedTypes.size()); if (field < delegateFieldCount) { return delegate.getLong(field); } else { return (Long) appendedValues.get(field - delegateFieldCount); } }
@Override public boolean isNull(int field) { checkPositionIndex(field, delegateFieldCount + appendedTypes.size()); if (field < delegateFieldCount) { return delegate.isNull(field); } else { return appendedValues.get(field - delegateFieldCount) == null; } }
public PagesWindowIndex(PagesIndex pagesIndex, int start, int end) { requireNonNull(pagesIndex, "pagesIndex is null"); checkPositionIndex(start, pagesIndex.getPositionCount(), "start"); checkPositionIndex(end, pagesIndex.getPositionCount(), "end"); checkArgument(start < end, "start must be before end"); this.pagesIndex = pagesIndex; this.start = start; this.size = end - start; }
@Override public boolean isNull(int field) { checkState(!closed); checkPositionIndex(field, types.size()); return currentCursor.isNull(field); }
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_badSize() { try { Preconditions.checkPositionIndex(1, -1); fail(); } catch (IllegalArgumentException expected) { // don't care what the message text is, as this is an invalid usage of // the Preconditions class, unlike all the other exceptions it throws } }
public void testCheckPositionIndex_negative() { try { Preconditions.checkPositionIndex(-1, 1); fail(); } catch (IndexOutOfBoundsException expected) { assertThat(expected).hasMessageThat().isEqualTo("index (-1) must not be negative"); } }
public void testCheckPositionIndex_tooHigh() { try { Preconditions.checkPositionIndex(2, 1); fail(); } catch (IndexOutOfBoundsException expected) { assertThat(expected) .hasMessageThat() .isEqualTo("index (2) must not be greater than size (1)"); } }
public void testCheckPositionIndex_withDesc_negative() { try { Preconditions.checkPositionIndex(-1, 1, "foo"); fail(); } catch (IndexOutOfBoundsException expected) { assertThat(expected).hasMessageThat().isEqualTo("foo (-1) must not be negative"); } }
public void testCheckPositionIndex_withDesc_tooHigh() { try { Preconditions.checkPositionIndex(2, 1, "foo"); fail(); } catch (IndexOutOfBoundsException expected) { assertThat(expected).hasMessageThat().isEqualTo("foo (2) must not be greater than size (1)"); } }