Java Array Last Index Of lastIndexOf(byte value, byte[] array)

Here you can find the source of lastIndexOf(byte value, byte[] array)

Description

last Index Of

License

Apache License

Declaration

public static int lastIndexOf(byte value, byte[] array) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/**   /*ww w .jav a  2s. c o m*/
 * Copyright 2011 The Buzz Media, LLC
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    public static final int INVALID_INDEX = -1;

    public static int lastIndexOf(byte value, byte[] array) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");

        return lastIndexOf(value, array, 0, array.length);
    }

    public static int lastIndexOf(byte value, byte[] array, int index) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");

        return indexOf(value, array, index, array.length - index);
    }

    public static int lastIndexOf(byte value, byte[] array, int index, int length) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");
        if (index < 0 || length < 0 || (index + length) > array.length)
            throw new IllegalArgumentException(
                    "index [" + index + "] must be >= 0, length [" + length + "] must be >= 0 and (index+length) ["
                            + (index + length) + "] must be < array.length [" + array.length + "]");

        return lastIndexOfNoCheck(value, array, index, length);
    }

    public static int lastIndexOf(byte[] values, byte[] array) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");

        return lastIndexOf(values, array, 0, array.length);
    }

    public static int lastIndexOf(byte[] values, byte[] array, int index) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");

        return lastIndexOf(values, array, index, array.length - index);
    }

    public static int lastIndexOf(byte[] values, byte[] array, int index, int length)
            throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");
        if (values == null)
            throw new IllegalArgumentException("values cannot be null");
        if (length < values.length)
            throw new IllegalArgumentException(
                    "length [" + length + "] must be >= values.length [" + values.length + "]");
        if (index < 0 || length < 0 || (index + length) > array.length)
            throw new IllegalArgumentException(
                    "index [" + index + "] must be >= 0, length [" + length + "] must be >= 0 and (index+length) ["
                            + (index + length) + "] must be < array.length [" + array.length + "]");

        return lastIndexOfNoCheck(values, array, index, length);
    }

    public static int lastIndexOf(char value, char[] array) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");

        return lastIndexOf(value, array, 0, array.length);
    }

    public static int lastIndexOf(char value, char[] array, int index) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");

        return indexOf(value, array, index, array.length - index);
    }

    public static int lastIndexOf(char value, char[] array, int index, int length) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");
        if (index < 0 || length < 0 || (index + length) > array.length)
            throw new IllegalArgumentException(
                    "index [" + index + "] must be >= 0, length [" + length + "] must be >= 0 and (index+length) ["
                            + (index + length) + "] must be < array.length [" + array.length + "]");

        return lastIndexOfNoCheck(value, array, index, length);
    }

    public static int lastIndexOf(char[] values, char[] array) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");

        return lastIndexOf(values, array, 0, array.length);
    }

    public static int lastIndexOf(char[] values, char[] array, int index) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");

        return lastIndexOf(values, array, index, array.length - index);
    }

    public static int lastIndexOf(char[] values, char[] array, int index, int length)
            throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");
        if (values == null)
            throw new IllegalArgumentException("values cannot be null");
        if (length < values.length)
            throw new IllegalArgumentException(
                    "length [" + length + "] must be >= values.length [" + values.length + "]");
        if (index < 0 || length < 0 || (index + length) > array.length)
            throw new IllegalArgumentException(
                    "index [" + index + "] must be >= 0, length [" + length + "] must be >= 0 and (index+length) ["
                            + (index + length) + "] must be < array.length [" + array.length + "]");

        return lastIndexOfNoCheck(values, array, index, length);
    }

    public static int indexOf(byte value, byte[] array) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");

        return indexOf(value, array, 0, array.length);
    }

    public static int indexOf(byte value, byte[] array, int index) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");

        return indexOf(value, array, index, array.length - index);
    }

    public static int indexOf(byte value, byte[] array, int index, int length) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");
        if (index < 0 || length < 0 || (index + length) > array.length)
            throw new IllegalArgumentException(
                    "index [" + index + "] must be >= 0, length [" + length + "] must be >= 0 and (index+length) ["
                            + (index + length) + "] must be < array.length [" + array.length + "]");

        return indexOfNoCheck(value, array, index, length);
    }

    public static int indexOf(byte[] values, byte[] array) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");

        return indexOf(values, array, 0, array.length);
    }

    public static int indexOf(byte[] values, byte[] array, int index) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");

        return indexOf(values, array, index, array.length - index);
    }

    public static int indexOf(byte[] values, byte[] array, int index, int length) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");
        if (values == null)
            throw new IllegalArgumentException("values cannot be null");
        if (length < values.length)
            throw new IllegalArgumentException(
                    "length [" + length + "] must be >= values.length [" + values.length + "]");
        if (index < 0 || length < 0 || (index + length) > array.length)
            throw new IllegalArgumentException(
                    "index [" + index + "] must be >= 0, length [" + length + "] must be >= 0 and (index+length) ["
                            + (index + length) + "] must be < array.length [" + array.length + "]");

        return indexOfNoCheck(values, array, index, length);
    }

    public static int indexOf(char value, char[] array) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");

        return indexOf(value, array, 0, array.length);
    }

    public static int indexOf(char value, char[] array, int index) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");

        return indexOf(value, array, index, array.length - index);
    }

    public static int indexOf(char value, char[] array, int index, int length) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");
        if (index < 0 || length < 0 || (index + length) > array.length)
            throw new IllegalArgumentException(
                    "index [" + index + "] must be >= 0, length [" + length + "] must be >= 0 and (index+length) ["
                            + (index + length) + "] must be < array.length [" + array.length + "]");

        return indexOfNoCheck(value, array, index, length);
    }

    public static int indexOf(char[] values, char[] array) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");

        return indexOf(values, array, 0, array.length);
    }

    public static int indexOf(char[] values, char[] array, int index) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");

        return indexOf(values, array, index, array.length - index);
    }

    public static int indexOf(char[] values, char[] array, int index, int length) throws IllegalArgumentException {
        if (array == null)
            throw new IllegalArgumentException("array cannot be null");
        if (values == null)
            throw new IllegalArgumentException("values cannot be null");
        if (length < values.length)
            throw new IllegalArgumentException(
                    "length [" + length + "] must be >= values.length [" + values.length + "]");
        if (index < 0 || length < 0 || (index + length) > array.length)
            throw new IllegalArgumentException(
                    "index [" + index + "] must be >= 0, length [" + length + "] must be >= 0 and (index+length) ["
                            + (index + length) + "] must be < array.length [" + array.length + "]");

        return indexOfNoCheck(values, array, index, length);
    }

    public static int lastIndexOfNoCheck(byte value, byte[] array, int index, int length) {
        for (int i = (index + length - 1); i >= index; i--) {
            if (array[i] == value)
                return i;
        }

        return INVALID_INDEX;
    }

    public static int lastIndexOfNoCheck(byte[] values, byte[] array, int index, int length) {
        // pre-define once
        int j;

        // Loop until it isn't possible for us to match values
        for (int i = (index + length - 1), endIndex = (index + values.length - 1); i >= endIndex; i--) {
            // Increment j for as many matches as we find
            for (j = 0; j < values.length && array[i - j] == values[values.length - 1 - j]; j++)
                ;

            // If we incremented j all the way, we matched all of values
            if (j == values.length)
                return i - j + 1;
            else
                i -= j;
        }

        return INVALID_INDEX;
    }

    public static int lastIndexOfNoCheck(char value, char[] array, int index, int length) {
        for (int i = (index + length - 1); i >= index; i--) {
            if (array[i] == value)
                return i;
        }

        return INVALID_INDEX;
    }

    public static int lastIndexOfNoCheck(char[] values, char[] array, int index, int length) {
        // pre-define once
        int j;

        // Loop until it isn't possible for us to match values
        for (int i = (index + length - 1), endIndex = (index + values.length - 1); i >= endIndex; i--) {
            // Increment j for as many matches as we find
            for (j = 0; j < values.length && array[i - j] == values[values.length - 1 - j]; j++)
                ;

            // If we incremented j all the way, we matched all of values
            if (j == values.length)
                return i - j + 1;
            else
                i -= j;
        }

        return INVALID_INDEX;
    }

    public static int indexOfNoCheck(byte value, byte[] array, int index, int length) {
        for (int end = (index + length); index < end; index++) {
            if (array[index] == value)
                return index;
        }

        return INVALID_INDEX;
    }

    public static int indexOfNoCheck(byte[] values, byte[] array, int index, int length) {
        // pre-define once
        int j;

        // Loop until we cannot match values anymore
        for (int end = (index + length - values.length); index <= end; index++) {
            // Increment j as long as we can match values in-order
            for (j = 0; j < values.length && values[j] == array[index + j]; j++)
                ;

            // If j is values.length, we matched all.
            if (j == values.length)
                return index;
            else
                index += j;
        }

        return INVALID_INDEX;
    }

    public static int indexOfNoCheck(char value, char[] array, int index, int length) {
        for (int end = (index + length); index < end; index++) {
            if (array[index] == value)
                return index;
        }

        return INVALID_INDEX;
    }

    public static int indexOfNoCheck(char[] values, char[] array, int index, int length) {
        // pre-define once
        int j;

        // Loop until we cannot match values anymore
        for (int end = (index + length - values.length); index <= end; index++) {
            // Increment j as long as we can match values in-order
            for (j = 0; j < values.length && values[j] == array[index + j]; j++)
                ;

            // If j is values.length, we matched all.
            if (j == values.length)
                return index;
            else
                index += j;
        }

        return INVALID_INDEX;
    }
}

Related

  1. arrayLastIndexOf(final T[] array, final T value)
  2. arrayLastIndexOf(Object[] array, Object objectToFind)
  3. lastIndexOf(boolean[] array, boolean valueToFind)
  4. lastIndexOf(byte[] array, byte valueToFind)
  5. lastIndexOf(byte[] ary, byte value)
  6. lastIndexOf(byte[] bytes, int pos, int len, byte b)
  7. lastIndexOf(byte[] data, int offset, int length, byte val)