@Override public ImmutableList<E> subList(int fromIndex, int toIndex) { Preconditions.checkPositionIndexes(fromIndex, toIndex, 1); return (fromIndex == toIndex) ? ImmutableList.<E>of() : this; }
@Override public Writer append(CharSequence csq, int start, int end) { checkPositionIndexes(start, end, csq.length()); return this; }
@Override void encodeTo(Appendable target, byte[] bytes, int off, int len) throws IOException { checkNotNull(target); checkPositionIndexes(off, off + len, bytes.length); for (int i = 0; i < len; ++i) { int b = bytes[off + i] & 0xFF; target.append(encoding[b]); target.append(encoding[b | 0x100]); } }
/** * Sorts the elements of {@code array} between {@code fromIndex} inclusive and {@code toIndex} * exclusive in descending order. * * @since 23.1 */ public static void sortDescending(long[] array, int fromIndex, int toIndex) { checkNotNull(array); checkPositionIndexes(fromIndex, toIndex, array.length); Arrays.sort(array, fromIndex, toIndex); reverse(array, fromIndex, toIndex); }
@Override public Hasher putBytes(byte[] bytes, int off, int len) { checkPositionIndexes(off, off + len, bytes.length); update(bytes, off, len); return this; }
public void testCheckPositionIndexes_ok() { Preconditions.checkPositionIndexes(0, 0, 0); Preconditions.checkPositionIndexes(0, 0, 1); Preconditions.checkPositionIndexes(0, 1, 1); Preconditions.checkPositionIndexes(1, 1, 1); }
@Override public Hasher putBytes(byte[] bytes, int off, int len) { Preconditions.checkPositionIndexes(off, off + len, bytes.length); for (int i = 0; i < len; i++) { putByte(bytes[off + i]); } return this; }
/** * Sorts the elements of {@code array} between {@code fromIndex} inclusive and {@code toIndex} * exclusive in descending order. * * @since 23.1 */ public static void sortDescending(char[] array, int fromIndex, int toIndex) { checkNotNull(array); checkPositionIndexes(fromIndex, toIndex, array.length); Arrays.sort(array, fromIndex, toIndex); reverse(array, fromIndex, toIndex); }
/** * Sorts the elements of {@code array} between {@code fromIndex} inclusive and {@code toIndex} * exclusive in descending order. * * @since 23.1 */ public static void sortDescending(short[] array, int fromIndex, int toIndex) { checkNotNull(array); checkPositionIndexes(fromIndex, toIndex, array.length); Arrays.sort(array, fromIndex, toIndex); reverse(array, fromIndex, toIndex); }
@Override public List<Integer> subList(int fromIndex, int toIndex) { int size = size(); checkPositionIndexes(fromIndex, toIndex, size); if (fromIndex == toIndex) { return Collections.emptyList(); } return new IntArrayAsList(array, start + fromIndex, start + toIndex); }
@Override public List<Short> subList(int fromIndex, int toIndex) { int size = size(); checkPositionIndexes(fromIndex, toIndex, size); if (fromIndex == toIndex) { return Collections.emptyList(); } return new ShortArrayAsList(array, start + fromIndex, start + toIndex); }
@Override public List<Character> subList(int fromIndex, int toIndex) { int size = size(); checkPositionIndexes(fromIndex, toIndex, size); if (fromIndex == toIndex) { return Collections.emptyList(); } return new CharArrayAsList(array, start + fromIndex, start + toIndex); }
@Override public List<Float> subList(int fromIndex, int toIndex) { int size = size(); checkPositionIndexes(fromIndex, toIndex, size); if (fromIndex == toIndex) { return Collections.emptyList(); } return new FloatArrayAsList(array, start + fromIndex, start + toIndex); }
@Override public List<Long> subList(int fromIndex, int toIndex) { int size = size(); checkPositionIndexes(fromIndex, toIndex, size); if (fromIndex == toIndex) { return Collections.emptyList(); } return new LongArrayAsList(array, start + fromIndex, start + toIndex); }
@Override public List<Boolean> subList(int fromIndex, int toIndex) { int size = size(); checkPositionIndexes(fromIndex, toIndex, size); if (fromIndex == toIndex) { return Collections.emptyList(); } return new BooleanArrayAsList(array, start + fromIndex, start + toIndex); }
@Override public HashCode hashBytes(byte[] input, int off, int len) { checkPositionIndexes(off, off + len, input.length); return HashCode.fromLong(fingerprint(input, off, len)); }
ImmutableSortedMultiset<E> getSubMultiset(int from, int to) { checkPositionIndexes(from, to, length); if (from == to) { return emptyMultiset(comparator()); } else if (from == 0 && to == length) { return this; } else { RegularImmutableSortedSet<E> subElementSet = elementSet.getSubSet(from, to); return new RegularImmutableSortedMultiset<E>( subElementSet, cumulativeCounts, offset + from, to - from); } }
public void testCheckPositionIndex_startNegative() { try { Preconditions.checkPositionIndexes(-1, 1, 1); fail(); } catch (IndexOutOfBoundsException expected) { assertThat(expected).hasMessageThat().isEqualTo("start index (-1) must not be negative"); } }
public void testCheckPositionIndexes_endTooHigh() { try { Preconditions.checkPositionIndexes(0, 2, 1); fail(); } catch (IndexOutOfBoundsException expected) { assertThat(expected) .hasMessageThat() .isEqualTo("end index (2) must not be greater than size (1)"); } }
public void testCheckPositionIndexes_reversed() { try { Preconditions.checkPositionIndexes(1, 0, 1); fail(); } catch (IndexOutOfBoundsException expected) { assertThat(expected) .hasMessageThat() .isEqualTo("end index (0) must not be less than start index (1)"); } }