public AbstractExpectationProvider(IExpectationRegion region) { super(); Preconditions.checkPositionIndex(region.getOffset(), region.getDocument().length()); Preconditions.checkPositionIndex( region.getOffset() + region.getLength(), region.getDocument().length()); this.region = region; }
public void testCheckPositionIndex_withDesc_negative() { try { Preconditions.checkPositionIndex(-1, 1, "foo"); fail(); } catch (IndexOutOfBoundsException expected) { assertThat(expected).hasMessage("foo (-1) must not be negative"); } }
public void testCheckPositionIndex_withDesc_tooHigh() { try { Preconditions.checkPositionIndex(2, 1, "foo"); fail(); } catch (IndexOutOfBoundsException expected) { assertThat(expected).hasMessage("foo (2) must not be greater than size (1)"); } }
public ListIterator<E> listIterator(final int start) { Preconditions.checkPositionIndex(start, size); return new ListIterator<E>() { int index = start; public boolean hasNext() { return index < size; } public boolean hasPrevious() { return index > 0; } public int nextIndex() { return index; } public int previousIndex() { return index - 1; } public E next() { E result; try { result = get(index); } catch (IndexOutOfBoundsException rethrown) { throw new NoSuchElementException(); } index++; return result; } public E previous() { E result; try { result = get(index - 1); } catch (IndexOutOfBoundsException rethrown) { throw new NoSuchElementException(); } index--; return result; } public void set(E o) { throw new UnsupportedOperationException(); } public void add(E o) { throw new UnsupportedOperationException(); } public void remove() { throw new UnsupportedOperationException(); } }; }
/** * Returns the highest intensity value. Returns 0 if the list has no data points. * * @return a {@link java.lang.Float} object. * @param intensityValues an array of float. * @param size a {@link java.lang.Integer} object. * @param mzValues an array of double. * @param mzRange a {@link com.google.common.collect.Range} object. */ public static @Nonnull Float getMaxIntensity( @Nonnull double mzValues[], @Nonnull float intensityValues[], @Nonnull Integer size, @Nonnull Range<Double> mzRange) { // Parameter check Preconditions.checkNotNull(mzValues); Preconditions.checkNotNull(intensityValues); Preconditions.checkNotNull(size); Preconditions.checkPositionIndex(size, mzValues.length); Preconditions.checkPositionIndex(size, intensityValues.length); Preconditions.checkNotNull(mzRange); Integer topIndex = getBasePeakIndex(mzValues, intensityValues, size, mzRange); if (topIndex == null) return 0f; return intensityValues[topIndex]; }
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 } }
/** * Returns the index of the first matching character in a character sequence, starting from a * given position, or {@code -1} if no character matches after that position. * * <p>The default implementation iterates over the sequence in forward order, beginning at {@code * start}, calling {@link #matches} for each character. * * @param sequence the character sequence to examine * @param start the first index to examine; must be nonnegative and no greater than {@code * sequence.length()} * @return the index of the first matching character, guaranteed to be no less than {@code start}, * or {@code -1} if no character matches * @throws IndexOutOfBoundsException if start is negative or greater than {@code * sequence.length()} */ public int indexIn(CharSequence sequence, int start) { int length = sequence.length(); Preconditions.checkPositionIndex(start, length); for (int i = start; i < length; i++) { if (matches(sequence.charAt(i))) { return i; } } return -1; }
/** * Calculates the total ion current (=sum of all intensity values) * * @return a {@link java.lang.Float} object. * @param intensityValues an array of float. * @param size a {@link java.lang.Integer} object. * @param mzValues an array of double. * @param mzRange a {@link com.google.common.collect.Range} object. */ public static @Nonnull Float getTIC( @Nonnull double mzValues[], @Nonnull float intensityValues[], @Nonnull Integer size, @Nonnull Range<Double> mzRange) { // Parameter check Preconditions.checkNotNull(mzValues); Preconditions.checkNotNull(intensityValues); Preconditions.checkNotNull(size); Preconditions.checkPositionIndex(size, mzValues.length); Preconditions.checkPositionIndex(size, intensityValues.length); Preconditions.checkNotNull(mzRange); float tic = 0f; for (int i = 0; i < size; i++) { if (mzRange.contains(mzValues[i])) tic += intensityValues[i]; } return tic; }
/** * Returns the index of the highest intensity value. Returns null if the list has no data points * or if no data point was found within the mz range. * * @param mzRange a {@link com.google.common.collect.Range} object. * @return a {@link java.lang.Integer} object. * @param mzValues an array of double. * @param intensityValues an array of float. * @param size a {@link java.lang.Integer} object. */ public static @Nullable Integer getBasePeakIndex( @Nonnull double mzValues[], @Nonnull float intensityValues[], @Nonnull Integer size, @Nonnull Range<Double> mzRange) { // Parameter check Preconditions.checkNotNull(intensityValues); Preconditions.checkNotNull(mzValues); Preconditions.checkNotNull(size); Preconditions.checkPositionIndex(size, mzValues.length); Preconditions.checkPositionIndex(size, intensityValues.length); Preconditions.checkNotNull(mzRange); Integer topIndex = null; for (int i = 0; i < size; i++) { if ((topIndex == null || intensityValues[i] > intensityValues[topIndex]) && mzRange.contains(mzValues[i])) topIndex = i; } return topIndex; }
@SuppressWarnings("null") public static @Nonnull String msSpectrumToString( @Nonnull double mzValues[], @Nonnull float intensityValues[], @Nonnull Integer size) { // Parameter check Preconditions.checkNotNull(mzValues); Preconditions.checkNotNull(intensityValues); Preconditions.checkNotNull(size); Preconditions.checkPositionIndex(size, mzValues.length); Preconditions.checkPositionIndex(size, intensityValues.length); StringBuilder b = new StringBuilder(); for (int i = 0; i < size; i++) { b.append(mzValues[i]); b.append(" "); b.append(intensityValues[i]); if (i < size - 1) b.append("\n"); } return b.toString(); }
static <K, V> RegularImmutableBiMap<K, V> fromEntryArray(int n, Entry<K, V>[] entryArray) { checkPositionIndex(n, entryArray.length); int tableSize = Hashing.closedTableSize(n, MAX_LOAD_FACTOR); int mask = tableSize - 1; ImmutableMapEntry<K, V>[] keyTable = createEntryArray(tableSize); ImmutableMapEntry<K, V>[] valueTable = createEntryArray(tableSize); Entry<K, V>[] entries; if (n == entryArray.length) { entries = entryArray; } else { entries = createEntryArray(n); } int hashCode = 0; for (int i = 0; i < n; i++) { @SuppressWarnings("unchecked") Entry<K, V> entry = entryArray[i]; K key = entry.getKey(); V value = entry.getValue(); checkEntryNotNull(key, value); int keyHash = key.hashCode(); int valueHash = value.hashCode(); int keyBucket = Hashing.smear(keyHash) & mask; int valueBucket = Hashing.smear(valueHash) & mask; ImmutableMapEntry<K, V> nextInKeyBucket = keyTable[keyBucket]; checkNoConflictInKeyBucket(key, entry, nextInKeyBucket); ImmutableMapEntry<K, V> nextInValueBucket = valueTable[valueBucket]; checkNoConflictInValueBucket(value, entry, nextInValueBucket); ImmutableMapEntry<K, V> newEntry; if (nextInValueBucket == null && nextInKeyBucket == null) { /* * TODO(user): consider using a NonTerminalImmutableMapEntry when nextInKeyBucket is * nonnull but nextInValueBucket is null. This may save a few bytes on some platforms, but * 2-morphic call sites are often optimized much better than 3-morphic, so it'd require * benchmarking. */ boolean reusable = entry instanceof ImmutableMapEntry && ((ImmutableMapEntry<K, V>) entry).isReusable(); newEntry = reusable ? (ImmutableMapEntry<K, V>) entry : new ImmutableMapEntry<K, V>(key, value); } else { newEntry = new NonTerminalImmutableBiMapEntry<K, V>( key, value, nextInKeyBucket, nextInValueBucket); } keyTable[keyBucket] = newEntry; valueTable[valueBucket] = newEntry; entries[i] = newEntry; hashCode += keyHash ^ valueHash; } return new RegularImmutableBiMap<K, V>(keyTable, valueTable, entries, mask, hashCode); }
/** * Calculates the total ion current (=sum of all intensity values) * * @return a {@link java.lang.Float} object. * @param intensityValues an array of float. * @param size a {@link java.lang.Integer} object. */ public static @Nonnull Float getTIC(@Nonnull float intensityValues[], @Nonnull Integer size) { // Parameter check Preconditions.checkNotNull(intensityValues); Preconditions.checkNotNull(size); Preconditions.checkPositionIndex(size, intensityValues.length); float tic = 0f; for (int i = 0; i < size; i++) { tic += intensityValues[i]; } return tic; }
/** * Returns the m/z range of given data points. Can return null if the data point list is empty. * * @return a {@link com.google.common.collect.Range} object. * @param mzValues an array of double. * @param size a {@link java.lang.Integer} object. */ @Nullable public static Range<Double> getMzRange(@Nonnull double mzValues[], @Nonnull Integer size) { // Parameter check Preconditions.checkNotNull(mzValues); Preconditions.checkNotNull(size); Preconditions.checkPositionIndex(size, mzValues.length); if (size == 0) return null; double min = mzValues[0]; double max = mzValues[size - 1]; return Range.closed(min, max); }
/** * Returns the index of the highest intensity value. Returns null if the list has no data points. * * @return a {@link java.lang.Integer} object. * @param intensityValues an array of float. * @param size a {@link java.lang.Integer} object. */ public static @Nullable Integer getBasePeakIndex( @Nonnull float intensityValues[], @Nonnull Integer size) { // Parameter check Preconditions.checkNotNull(intensityValues); Preconditions.checkNotNull(size); Preconditions.checkPositionIndex(size, intensityValues.length); Integer topIndex = null; for (int i = 0; i < size; i++) { if ((topIndex == null) || ((intensityValues[i] > intensityValues[topIndex]))) topIndex = i; } return topIndex; }
public static void normalizeIntensity( @Nonnull float intensityValues[], @Nonnull Integer size, @Nonnull Float scale) { // Parameter check Preconditions.checkNotNull(intensityValues); Preconditions.checkNotNull(size); Preconditions.checkPositionIndex(size, intensityValues.length); Preconditions.checkNotNull(scale); final float max = getMaxIntensity(intensityValues, size); for (int i = 0; i < intensityValues.length; i++) { intensityValues[i] = intensityValues[i] / max * scale; } }
// Assumes input grouped on relevant pagesHashStrategy columns private static int findGroupEnd( PagesIndex pagesIndex, PagesHashStrategy pagesHashStrategy, int startPosition) { checkArgument(pagesIndex.getPositionCount() > 0, "Must have at least one position"); checkPositionIndex(startPosition, pagesIndex.getPositionCount(), "startPosition out of bounds"); // Short circuit if the whole page has the same value if (pagesIndex.positionEqualsPosition( pagesHashStrategy, startPosition, pagesIndex.getPositionCount() - 1)) { return pagesIndex.getPositionCount(); } // TODO: do position binary search int endPosition = startPosition + 1; while ((endPosition < pagesIndex.getPositionCount()) && pagesIndex.positionEqualsPosition(pagesHashStrategy, endPosition - 1, endPosition)) { endPosition++; } return endPosition; }
public void add(Slice key, Slice value) { Preconditions.checkNotNull(key, "key is null"); Preconditions.checkNotNull(value, "value is null"); Preconditions.checkState(!finished, "block is finished"); Preconditions.checkPositionIndex(restartBlockEntryCount, blockRestartInterval); Preconditions.checkArgument( lastKey == null || comparator.compare(key, lastKey) > 0, "key %s must be greater than last key %s", key, lastKey); int sharedKeyBytes = 0; if (restartBlockEntryCount < blockRestartInterval) { sharedKeyBytes = calculateSharedBytes(key, lastKey); } else { // restart prefix compression restartPositions.add(block.size()); restartBlockEntryCount = 0; } int nonSharedKeyBytes = key.length() - sharedKeyBytes; // write "<shared><non_shared><value_size>" VariableLengthQuantity.writeVariableLengthInt(sharedKeyBytes, block); VariableLengthQuantity.writeVariableLengthInt(nonSharedKeyBytes, block); VariableLengthQuantity.writeVariableLengthInt(value.length(), block); // write non-shared key bytes block.writeBytes(key, sharedKeyBytes, nonSharedKeyBytes); // write value bytes block.writeBytes(value, 0, value.length()); // update last key lastKey = key; // update state entryCount++; restartBlockEntryCount++; }
/** * 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(); } /* * We can't use call the two-arg constructor with arguments (offset, end) * because the returned Iterator is a ListIterator that may be moved back * past the beginning of the iteration. */ return new AbstractIndexedListIterator<T>(length, index) { @Override protected T get(int index) { return array[offset + index]; } }; }
public UnmodifiableListIterator<Object> listIterator(int paramInt) { Preconditions.checkPositionIndex(paramInt, 0); return Iterators.EMPTY_LIST_ITERATOR; }
@Override public void writeTo(OutputStream stream, long start, long end) throws IOException { Preconditions.checkPositionIndex(Ints.checkedCast(end), size); stream.write(bytes, Ints.checkedCast(start), Ints.checkedCast(end - start)); }
private int reversePosition(int index) { int size = size(); checkPositionIndex(index, size); return size - index; }
/** * 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 new ByteArrayDataInputStream(bytes, start); }
public void testCheckPositionIndex_ok() { assertEquals(0, Preconditions.checkPositionIndex(0, 0)); assertEquals(0, Preconditions.checkPositionIndex(0, 1)); assertEquals(1, Preconditions.checkPositionIndex(1, 1)); }
public UnmodifiableListIterator<E> a(int paramInt) { Preconditions.checkPositionIndex(paramInt, 1); return new SingletonImmutableList.1(this, paramInt); }
@Override public int indexIn(CharSequence sequence, int start) { int length = sequence.length(); Preconditions.checkPositionIndex(start, length); return -1; }