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:org.apache.kudu.util.Slice.java

/**
 * Transfers the content of the specified source channel to this buffer
 * starting at the specified absolute {@code index}.
 *
 * @param length the maximum number of bytes to transfer
 * @return the actual number of bytes read in from the specified channel.
 *         {@code -1} if the specified channel is closed.
 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
 * if {@code index + length} is greater than {@code this.capacity}
 * @throws java.io.IOException if the specified channel threw an exception during I/O
 *//*w ww  . j av a 2 s  .  c o  m*/
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    Preconditions.checkPositionIndexes(index, index + length, this.length);
    index += offset;
    ByteBuffer buf = ByteBuffer.wrap(data, index, length);
    int readBytes = 0;

    do {
        int localReadBytes;
        try {
            localReadBytes = in.read(buf);
        } catch (ClosedChannelException e) {
            localReadBytes = -1;
        }
        if (localReadBytes < 0) {
            if (readBytes == 0) {
                return -1;
            } else {
                break;
            }
        } else if (localReadBytes == 0) {
            break;
        }
        readBytes += localReadBytes;
    } while (readBytes < length);

    return readBytes;
}

From source file:net.automatalib.commons.util.array.RichArray.java

public void parallelSetAll(int startInclusive, int endExclusive, IntFunction<? extends T> generator) {
    Preconditions.checkPositionIndexes(startInclusive, endExclusive, length);
    Objects.requireNonNull(generator);
    IntStream.range(start + startInclusive, start + endExclusive).parallel()
            .forEach(i -> contents[i] = generator.apply(i - start));
}

From source file:org.apache.kudu.util.Slice.java

public int setBytes(int index, FileChannel in, int position, int length) throws IOException {
    Preconditions.checkPositionIndexes(index, index + length, this.length);
    index += offset;/*from   w w w .jav  a 2  s. co m*/
    ByteBuffer buf = ByteBuffer.wrap(data, index, length);
    int readBytes = 0;

    do {
        int localReadBytes;
        try {
            localReadBytes = in.read(buf, position + readBytes);
        } catch (ClosedChannelException e) {
            localReadBytes = -1;
        }
        if (localReadBytes < 0) {
            if (readBytes == 0) {
                return -1;
            } else {
                break;
            }
        } else if (localReadBytes == 0) {
            break;
        }
        readBytes += localReadBytes;
    } while (readBytes < length);

    return readBytes;
}

From source file:net.automatalib.commons.util.array.RichArray.java

public void parallelSetAll(int startInclusive, int endExclusive, T value) {
    Preconditions.checkPositionIndexes(startInclusive, endExclusive, length);
    IntStream.range(start + startInclusive, start + endExclusive).parallel().forEach(i -> contents[i] = value);
}

From source file:net.automatalib.commons.util.array.RichArray.java

public void parallelSetAll(int startInclusive, int endExclusive, Supplier<? extends T> supplier) {
    Preconditions.checkPositionIndexes(startInclusive, endExclusive, length);
    Objects.requireNonNull(supplier);
    IntStream.range(start + startInclusive, start + endExclusive).parallel()
            .forEach(i -> contents[i] = supplier.get());
}

From source file:org.apache.kudu.util.Slice.java

/**
 * Returns a copy of this buffer's sub-region.  Modifying the content of
 * the returned buffer or this buffer does not affect each other at all.
 *//*  ww w  .j  av  a 2s  . c o m*/
public Slice copySlice(int index, int length) {
    Preconditions.checkPositionIndexes(index, index + length, this.length);

    index += offset;
    byte[] copiedArray = new byte[length];
    System.arraycopy(data, index, copiedArray, 0, length);
    return new Slice(copiedArray);
}

From source file:org.apache.kudu.util.Slice.java

public byte[] copyBytes(int index, int length) {
    Preconditions.checkPositionIndexes(index, index + length, this.length);
    index += offset;// w w w  .j  av  a2s  . com
    if (index == 0) {
        return Arrays.copyOf(data, length);
    } else {
        byte[] value = new byte[length];
        System.arraycopy(data, index, value, 0, length);
        return value;
    }
}

From source file:org.apache.kudu.util.Slice.java

/**
 * Returns a slice of this buffer's sub-region. Modifying the content of
 * the returned buffer or this buffer affects each other's content while
 * they maintain separate indexes and marks.
 *//*from  ww  w  .  j a  va 2  s  .c  o m*/
public Slice slice(int index, int length) {
    if (index == 0 && length == this.length) {
        return this;
    }

    Preconditions.checkPositionIndexes(index, index + length, this.length);
    if (index >= 0 && length == 0) {
        return Slices.EMPTY_SLICE;
    }
    return new Slice(data, offset + index, length);
}

From source file:org.apache.kudu.util.Slice.java

/**
 * Converts this buffer's sub-region into a NIO buffer.  The returned
 * buffer shares the content with this buffer.
 *//*from w  w w.ja  va  2 s. com*/
public ByteBuffer toByteBuffer(int index, int length) {
    Preconditions.checkPositionIndexes(index, index + length, this.length);
    index += offset;
    return ByteBuffer.wrap(data, index, length).order(LITTLE_ENDIAN);
}

From source file:net.automatalib.commons.util.array.RichArray.java

public void transform(int startInclusive, int endExclusive, Function<? super T, ? extends T> transformer) {
    Preconditions.checkPositionIndexes(startInclusive, endExclusive, length);
    Objects.requireNonNull(transformer);

    int end = start + endExclusive;
    for (int i = start + startInclusive; i < end; i++) {
        contents[i] = transformer.apply(contents[i]);
    }// ww w .  j  a  va2 s.c o  m
}