Example usage for java.lang ArrayIndexOutOfBoundsException ArrayIndexOutOfBoundsException

List of usage examples for java.lang ArrayIndexOutOfBoundsException ArrayIndexOutOfBoundsException

Introduction

In this page you can find the example usage for java.lang ArrayIndexOutOfBoundsException ArrayIndexOutOfBoundsException.

Prototype

public ArrayIndexOutOfBoundsException(int index) 

Source Link

Document

Constructs a new ArrayIndexOutOfBoundsException class with an argument indicating the illegal index.

Usage

From source file:Time.java

/**
 * Find the n'th xxxxday of s specified month (for instance find 1st sunday of
 * May 2006; findNthOfMonth (1, Calendar.SUNDAY, Calendar.MAY, 2006); Return
 * null if the specified day doesn't exists.
 * //from  w ww  .j  a  v  a2s  . c  om
 * @param n
 *          Nth day to look for.
 * @param dayOfWeek
 *          Day to look for (Calendar.XXXDAY).
 * @param month
 *          Month to check (Calendar.XXX).
 * @param year
 *          Year to check.
 * @return Required Day (or null if non-existent)
 * @throws ArrayIndexOutOfBoundsException
 *           if dyaOfWeek parameter doesn't represent a valid day.
 */
public static Day getNthOfMonth(int n, int dayOfWeek, int month, int year)
        throws ArrayIndexOutOfBoundsException {
    // Validate the dayOfWeek argument
    if (dayOfWeek < 0 || dayOfWeek > 6)
        throw new ArrayIndexOutOfBoundsException(dayOfWeek);

    Day first = new Day(year, month, 1);

    int offset = dayOfWeek - first.getDayOfWeek();
    if (offset < 0)
        offset = 7 + offset;

    int dayNo = (n - 1) * 7 + offset + 1;

    return dayNo > first.getDaysInMonth() ? null : new Day(year, month, dayNo);
}

From source file:com.idylwood.utils.MathUtils.java

/**
 * Numerically precise dot product. Keeps a running error along with the
 * accumulator. Equivalent to MathUtils.sum(MathUtils.multiply(x,y))
 * but much faster and with O(1) memory overhead.
 * O(n) with O(1) space./*from w  ww.  j  a va2 s  .co  m*/
 * Even faster than the naive implementation ;).
 * @param x
 * @param y
 * @param startOne
 * @param startTwo
 * @param len
 * @return
 */
public static final double linearCombination(final double[] x, final double[] y, final int startOne,
        final int startTwo, final int len) {
    //if (true) return MathUtils.sum(MathUtils.multiply(x,y));
    if (startOne + len > x.length || startTwo + len > y.length)
        throw new ArrayIndexOutOfBoundsException("Vector indices don't make sense!");
    final int unroll = 3; // don't blindly change this without changing the loop!
    // unroll was tuned to my machine. the optimal value is
    // probably architecture specific and probably depends on
    // the number of CPU registers
    final int modLen = len - len % unroll;
    double sum = 0;
    double err = 0;
    int i = 0;
    for (; i < modLen; i += unroll) {
        // this line depends on unroll variable.
        final int xPtr = i + startOne;
        final int yPtr = i + startTwo;
        final double prod = x[xPtr] * y[yPtr] + x[xPtr + 1] * y[yPtr + 1] + x[xPtr + 2] * y[yPtr + 2];
        final double hi = sum + prod;
        err += (hi - sum) - prod;
        sum = hi;
    }
    for (; i < len; i++) {
        final int xPtr = i + startOne;
        final int yPtr = i + startTwo;
        final double prod = x[xPtr] * y[yPtr];
        final double hi = sum + prod;
        err += (hi - sum) - prod;
        sum = hi;
    }
    return sum - err;
}

From source file:com.cburch.logisim.circuit.CircuitWires.java

private void computeBundleMap(BundleMap ret) {
    // create bundles corresponding to wires and tunnels
    connectWires(ret);/*from ww  w.  ja  v  a 2s .  c  om*/
    connectTunnels(ret);
    connectPullResistors(ret);

    // merge any WireBundle objects united by previous steps
    for (Iterator<WireBundle> it = ret.getBundles().iterator(); it.hasNext();) {
        WireBundle b = it.next();
        WireBundle bpar = b.find();
        if (bpar != b) { // b isn't group's representative
            for (Location pt : b.points) {
                ret.setBundleAt(pt, bpar);
                bpar.points.add(pt);
            }
            bpar.addPullValue(b.getPullValue());
            it.remove();
        }
    }

    // make a WireBundle object for each end of a splitter
    for (Splitter spl : splitters) {
        List<EndData> ends = new ArrayList<EndData>(spl.getEnds());
        for (EndData end : ends) {
            Location p = end.getLocation();
            WireBundle pb = ret.createBundleAt(p);
            pb.setWidth(end.getWidth(), p);
        }
    }

    // set the width for each bundle whose size is known
    // based on components
    for (Location p : ret.getBundlePoints()) {
        WireBundle pb = ret.getBundleAt(p);
        BitWidth width = points.getWidth(p);
        if (width != BitWidth.UNKNOWN) {
            pb.setWidth(width, p);
        }
    }

    // determine the bundles at the end of each splitter
    for (Splitter spl : splitters) {
        List<EndData> ends = new ArrayList<EndData>(spl.getEnds());
        int index = -1;
        for (EndData end : ends) {
            index++;
            Location p = end.getLocation();
            WireBundle pb = ret.getBundleAt(p);
            if (pb != null) {
                pb.setWidth(end.getWidth(), p);
                spl.wire_data.end_bundle[index] = pb;
            }
        }
    }

    // unite threads going through splitters
    for (Splitter spl : splitters) {
        synchronized (spl) {
            SplitterAttributes spl_attrs = (SplitterAttributes) spl.getAttributeSet();
            byte[] bit_end = spl_attrs.bit_end;
            SplitterData spl_data = spl.wire_data;
            WireBundle from_bundle = spl_data.end_bundle[0];
            if (from_bundle == null || !from_bundle.isValid())
                continue;

            for (int i = 0; i < bit_end.length; i++) {
                int j = bit_end[i];
                if (j > 0) {
                    int thr = spl.bit_thread[i];
                    WireBundle to_bundle = spl_data.end_bundle[j];
                    WireThread[] to_threads = to_bundle.threads;
                    if (to_threads != null && to_bundle.isValid()) {
                        WireThread[] from_threads = from_bundle.threads;
                        if (i >= from_threads.length) {
                            throw new ArrayIndexOutOfBoundsException(
                                    "from " + i + " of " + from_threads.length);
                        }
                        if (thr >= to_threads.length) {
                            throw new ArrayIndexOutOfBoundsException("to " + thr + " of " + to_threads.length);
                        }
                        from_threads[i].unite(to_threads[thr]);
                    }
                }
            }
        }
    }

    // merge any threads united by previous step
    for (WireBundle b : ret.getBundles()) {
        if (b.isValid() && b.threads != null) {
            for (int i = 0; i < b.threads.length; i++) {
                WireThread thr = b.threads[i].find();
                b.threads[i] = thr;
                thr.getBundles().add(new ThreadBundle(i, b));
            }
        }
    }

    // All threads are sewn together! Compute the exception set before leaving
    Collection<WidthIncompatibilityData> exceptions = points.getWidthIncompatibilityData();
    if (exceptions != null && exceptions.size() > 0) {
        for (WidthIncompatibilityData wid : exceptions) {
            ret.addWidthIncompatibilityData(wid);
        }
    }
    for (WireBundle b : ret.getBundles()) {
        WidthIncompatibilityData e = b.getWidthIncompatibilityData();
        if (e != null)
            ret.addWidthIncompatibilityData(e);
    }
}

From source file:org.diorite.command.Arguments.java

private void check(final int index) throws ArrayIndexOutOfBoundsException {
    if (this.args.length <= index) {
        throw new ArrayIndexOutOfBoundsException(
                "Out of range, length: " + Arguments.this.args.length + ", index: " + index);
    }//w  w w.  j  a  va 2 s . c  om
}

From source file:ResizableDoubleArray.java

/**
 * Sets the element at the specified index. If the specified index is greater
 * than <code>getNumElements() - 1</code>, the <code>numElements</code>
 * property is increased to <code>index +1</code> and additional storage is
 * allocated (if necessary) for the new element and all (uninitialized)
 * elements between the new element and the previous end of the array).
 * //from w w w. ja v  a  2 s.  c o  m
 * @param index
 *          index to store a value in
 * @param value
 *          value to store at the specified index
 * @throws ArrayIndexOutOfBoundsException
 *           if <code>index</code> is less than zero.
 */
public synchronized void setElement(int index, double value) {
    if (index < 0) {
        String msg = "Cannot set an element at a negative index";
        throw new ArrayIndexOutOfBoundsException(msg);
    }
    if (index + 1 > numElements) {
        numElements = index + 1;
    }
    if ((startIndex + index) >= internalArray.length) {
        expandTo(startIndex + (index + 1));
    }
    internalArray[startIndex + index] = value;
}

From source file:javadz.beanutils.PropertyUtilsBean.java

/**
 * Return the value of the specified indexed property of the specified
 * bean, with no type conversions.  In addition to supporting the JavaBeans
 * specification, this method has been extended to support
 * <code>List</code> objects as well.
 *
 * @param bean Bean whose property is to be extracted
 * @param name Simple property name of the property value to be extracted
 * @param index Index of the property value to be extracted
 * @return the indexed property value/*from  www  . ja  v  a2s. c om*/
 *
 * @exception IndexOutOfBoundsException if the specified index
 *  is outside the valid range for the underlying property
 * @exception IllegalAccessException if the caller does not have
 *  access to the property accessor method
 * @exception IllegalArgumentException if <code>bean</code> or
 *  <code>name</code> is null
 * @exception InvocationTargetException if the property accessor method
 *  throws an exception
 * @exception NoSuchMethodException if an accessor method for this
 *  propety cannot be found
 */
public Object getIndexedProperty(Object bean, String name, int index)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    if (bean == null) {
        throw new IllegalArgumentException("No bean specified");
    }
    if (name == null || name.length() == 0) {
        if (bean.getClass().isArray()) {
            return Array.get(bean, index);
        } else if (bean instanceof List) {
            return ((List) bean).get(index);
        }
    }
    if (name == null) {
        throw new IllegalArgumentException("No name specified for bean class '" + bean.getClass() + "'");
    }

    // Handle DynaBean instances specially
    if (bean instanceof DynaBean) {
        DynaProperty descriptor = ((DynaBean) bean).getDynaClass().getDynaProperty(name);
        if (descriptor == null) {
            throw new NoSuchMethodException(
                    "Unknown property '" + name + "' on bean class '" + bean.getClass() + "'");
        }
        return (((DynaBean) bean).get(name, index));
    }

    // Retrieve the property descriptor for the specified property
    PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
    if (descriptor == null) {
        throw new NoSuchMethodException(
                "Unknown property '" + name + "' on bean class '" + bean.getClass() + "'");
    }

    // Call the indexed getter method if there is one
    if (descriptor instanceof IndexedPropertyDescriptor) {
        Method readMethod = ((IndexedPropertyDescriptor) descriptor).getIndexedReadMethod();
        readMethod = MethodUtils.getAccessibleMethod(bean.getClass(), readMethod);
        if (readMethod != null) {
            Object[] subscript = new Object[1];
            subscript[0] = new Integer(index);
            try {
                return (invokeMethod(readMethod, bean, subscript));
            } catch (InvocationTargetException e) {
                if (e.getTargetException() instanceof IndexOutOfBoundsException) {
                    throw (IndexOutOfBoundsException) e.getTargetException();
                } else {
                    throw e;
                }
            }
        }
    }

    // Otherwise, the underlying property must be an array
    Method readMethod = getReadMethod(bean.getClass(), descriptor);
    if (readMethod == null) {
        throw new NoSuchMethodException(
                "Property '" + name + "' has no " + "getter method on bean class '" + bean.getClass() + "'");
    }

    // Call the property getter and return the value
    Object value = invokeMethod(readMethod, bean, EMPTY_OBJECT_ARRAY);
    if (!value.getClass().isArray()) {
        if (!(value instanceof java.util.List)) {
            throw new IllegalArgumentException(
                    "Property '" + name + "' is not indexed on bean class '" + bean.getClass() + "'");
        } else {
            //get the List's value
            return ((java.util.List) value).get(index);
        }
    } else {
        //get the array's value
        try {
            return (Array.get(value, index));
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new ArrayIndexOutOfBoundsException(
                    "Index: " + index + ", Size: " + Array.getLength(value) + " for property '" + name + "'");
        }
    }

}

From source file:com.idylwood.utils.MathUtils.java

public static final double linearCombination(final double[] x, final double[] y) {
    if (x.length != y.length)
        throw new ArrayIndexOutOfBoundsException("Unequal length vectors!");
    return linearCombination(x, y, 0, 0, x.length);
}

From source file:eu.qualityontime.commons.QPropertyUtilsBean.java

public Object _getIndexedProperty(final Object bean, final String name, final int index) throws Exception {

    if (bean == null) {
        throw new IllegalArgumentException("No bean specified");
    }/*  w  w  w .j  a  v a 2s . c o m*/
    if (name == null || name.length() == 0) {
        if (bean.getClass().isArray()) {
            return Array.get(bean, index);
        } else if (bean instanceof List) {
            return ((List<?>) bean).get(index);
        }
    }
    if (name == null) {
        throw new IllegalArgumentException("No name specified for bean class '" + bean.getClass() + "'");
    }

    if (name.startsWith("@")) {
        Object f = FieldUtils.readField(bean, trimAnnotations(name));
        if (null == f) {
            if (name.endsWith("?"))
                return null;
            else
                throw new NestedNullException();
        }
        if (f.getClass().isArray())
            return Array.get(f, index);
        else if (f instanceof List)
            return ((List<?>) f).get(index);
    }
    // Retrieve the property descriptor for the specified property
    final PropertyDescriptor descriptor = getPropertyDescriptor(bean, trimAnnotations(name));
    if (descriptor == null) {
        throw new NoSuchMethodException(
                "Unknown property '" + name + "' on bean class '" + bean.getClass() + "'");
    }

    // Call the indexed getter method if there is one
    if (descriptor instanceof IndexedPropertyDescriptor) {
        Method readMethod = ((IndexedPropertyDescriptor) descriptor).getIndexedReadMethod();
        readMethod = MethodUtils.getAccessibleMethod(bean.getClass(), readMethod);
        if (readMethod != null) {
            final Object[] subscript = new Object[1];
            subscript[0] = new Integer(index);
            try {
                return invokeMethod(readMethod, bean, subscript);
            } catch (final InvocationTargetException e) {
                if (e.getTargetException() instanceof IndexOutOfBoundsException) {
                    throw (IndexOutOfBoundsException) e.getTargetException();
                } else {
                    throw e;
                }
            }
        }
    }

    // Otherwise, the underlying property must be an array
    final Method readMethod = getReadMethod(bean.getClass(), descriptor);
    if (readMethod == null) {
        throw new NoSuchMethodException(
                "Property '" + name + "' has no " + "getter method on bean class '" + bean.getClass() + "'");
    }

    // Call the property getter and return the value
    final Object value = invokeMethod(readMethod, bean, EMPTY_OBJECT_ARRAY);
    if (null == value && name.endsWith("?"))
        return null;
    if (!value.getClass().isArray()) {
        if (!(value instanceof java.util.List)) {
            throw new IllegalArgumentException(
                    "Property '" + name + "' is not indexed on bean class '" + bean.getClass() + "'");
        } else {
            // get the List's value
            return ((java.util.List<?>) value).get(index);
        }
    } else {
        // get the array's value
        try {
            return Array.get(value, index);
        } catch (final ArrayIndexOutOfBoundsException e) {
            throw new ArrayIndexOutOfBoundsException(
                    "Index: " + index + ", Size: " + Array.getLength(value) + " for property '" + name + "'");
        }
    }

}

From source file:com.alvermont.terraj.fracplanet.geom.VertexBufferArray.java

/**
 * Retrieve a vertex from the buffer//w  w  w.  j av  a2  s.c  o m
 *
 * @param index The index of the element that is to be retrieved
 * @return The corresponding vertex data for this index
 */
public Vertex get(int index) {
    if (index >= size()) {
        throw new ArrayIndexOutOfBoundsException(
                "Request for invalid vertex index: " + index + " max is: " + size());
    }

    return new BufferedVertex(index);
}

From source file:com.luseen.spacenavigation.SpaceNavigationView.java

/**
 * Change current selected item to given index
 *
 * @param indexToChange given index/*  w  w  w .  j  a v  a 2s .  c om*/
 */
public void changeCurrentItem(int indexToChange) {
    if (indexToChange < 0 || indexToChange > spaceItems.size())
        throw new ArrayIndexOutOfBoundsException(
                "Please be more careful, we do't have such item : " + indexToChange);
    else
        updateSpaceItems(indexToChange);
}