Example usage for com.google.common.base Preconditions checkPositionIndexes

List of usage examples for com.google.common.base Preconditions checkPositionIndexes

Introduction

In this page you can find the example usage for com.google.common.base Preconditions checkPositionIndexes.

Prototype

public static void checkPositionIndexes(int start, int end, int size) 

Source Link

Document

Ensures that start and end specify a valid positions in an array, list or string of size size , and are in order.

Usage

From source file:com.facebook.buck.util.collect.ArrayIterable.java

public static <T> Iterable<T> of(final T[] array, final int startIndex, int endIndex) {
    Preconditions.checkNotNull(array);// w ww.  j av  a 2s.co  m
    Preconditions.checkPositionIndexes(startIndex, endIndex, array.length);

    // Special-case the empty Iterator with a singleton value.
    if (startIndex >= endIndex) {
        // Note that Collections.emptyIterator().remove() throws an IllegalStateException. We prefer
        // that remove() throws an UnsupportedOperationException for an empty Iterator, so we use
        // ImmutableList instead.
        return ImmutableList.<T>of();
    } else if (endIndex - startIndex == 1) {
        return new Iterable<T>() {
            @Override
            public Iterator<T> iterator() {
                // This always looks up the element in the array in case the user modifies the contents of
                // the array outside of this method. That would probably be a bad thing for the user to
                // do, but this ensures that the behavior is consistent with ArrayIterable.
                return Iterators.singletonIterator(array[startIndex]);
            }
        };
    } else {
        return new ArrayIterable<T>(array, startIndex, endIndex);
    }
}

From source file:com.andrewkroh.cisco.common.HexUtility.java

/**
 * Converts the specified range to elements from the given array to their
 * hex representation.//w  w  w . j  a  va  2s.c o  m
 *
 * @param bytes
 *            byte array containing elements to convert to a string
 * @param from
 *            index of the first element to convert to hex
 * @param to
 *            index of the last element to convert
 * @return a string containing the species range of bytes as hex
 *
 * @throws NullPointerException
 *             if {@code bytes} is null
 * @throws IndexOutOfBoundsException
 *             if either {@code from} index or {@code to} index is negative
 *             or is greater than size, or if {@code to} index is less than
 *             {@code from}
 */
public static String bytesToHex(byte[] bytes, int from, int to) {
    Preconditions.checkNotNull(bytes);
    Preconditions.checkPositionIndexes(from, to, bytes.length);

    int numElements = to - from + 1;
    char[] hexChars = new char[numElements * 2];

    for (int j = 0; j < numElements; j++) {
        int value = bytes[from + j] & 0xFF;
        hexChars[j * 2] = HEX_CHARACTERS[value >>> 4];
        hexChars[j * 2 + 1] = HEX_CHARACTERS[value & 0x0F];
    }

    return new String(hexChars);
}

From source file:org.b1.pack.standard.writer.ChunkedOutputStream.java

@Override
public void write(byte[] b, int off, int len) throws IOException {
    Preconditions.checkPositionIndexes(off, off + len, b.length);
    while (len > 0) {
        int size = Math.min(len, buffer.length - count);
        System.arraycopy(b, off, buffer, count, size);
        off += size;//from   w w w.  ja v a  2  s  .  c o  m
        len -= size;
        count += size;
        ensureFreeSpace();
    }
}

From source file:org.apache.drill.exec.store.ResourceInputStream.java

public int read(long position, byte b[], int off, int len) {
    Preconditions.checkNotNull(b);/*ww  w .ja  va  2  s .c om*/
    Preconditions.checkPositionIndexes(off, off + len, b.length);

    int start = (int) position;

    if (start >= count) {
        return -1;
    }

    int avail = count - start;
    if (len > avail) {
        len = avail;
    }
    if (len <= 0) {
        return 0;
    }
    System.arraycopy(buf, start, b, off, len);
    return len;
}

From source file:com.facebook.presto.block.rle.RunLengthEncodedBlock.java

@Override
public Block getRegion(int positionOffset, int length) {
    Preconditions.checkPositionIndexes(positionOffset, positionOffset + length, positionCount);
    return new RunLengthEncodedBlock(value, length);
}

From source file:hihex.cs.FlushableGzipOutputStream.java

@Override
public void write(final byte[] buffer, final int offset, final int count) throws IOException {
    Preconditions.checkPositionIndexes(offset, offset + count, buffer.length);
    nativeWrite(mNative, buffer, offset, count);
}

From source file:com.facebook.presto.block.uncompressed.UncompressedLongBlock.java

@Override
public Block getRegion(int positionOffset, int length) {
    Preconditions.checkPositionIndexes(positionOffset, positionOffset + length, getPositionCount());
    return cursor().getRegionAndAdvance(length);
}

From source file:com.facebook.presto.block.uncompressed.UncompressedSliceBlock.java

@Override
public Block getRegion(int positionOffset, int length) {
    Preconditions.checkPositionIndexes(positionOffset, positionOffset + length, offsets.length);
    return cursor().getRegionAndAdvance(length);
}

From source file:org.iq80.leveldb.util.Slice.java

/**
 * Gets a byte at the specified absolute {@code index} in this buffer.
 *
 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
 * {@code index + 1} is greater than {@code this.capacity}
 *///from   w  ww. ja v a2 s  . c o m
public byte getByte(int index) {
    Preconditions.checkPositionIndexes(index, index + SIZE_OF_BYTE, this.length);
    index += offset;
    return data[index];
}

From source file:de.cosmocode.collections.CyclingList.java

@Override
public List<E> subList(int fromIndex, int toIndex) {
    final int realFromIndex = subListIndex(fromIndex);
    final int realToIndex = subListIndex(toIndex);
    Preconditions.checkPositionIndexes(realFromIndex, realToIndex, size());
    return super.subList(realFromIndex, realToIndex);
}