Example usage for java.lang ArrayStoreException ArrayStoreException

List of usage examples for java.lang ArrayStoreException ArrayStoreException

Introduction

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

Prototype

public ArrayStoreException(String s) 

Source Link

Document

Constructs an ArrayStoreException with the specified detail message.

Usage

From source file:Main.java

public static Collection<Node> createNodeCollection(final NodeList nodeList) {
    // http://www.java2s.com/Code/Java/XML/WrapNodeListtoCollection.htm
    // Written by Tomer Gabel under the Apache License Version 2.0
    return new Collection<Node>() {
        @Override/* w ww  .ja va2  s  .c  o  m*/
        public int size() {
            return nodeList.getLength();
        }

        @Override
        public boolean isEmpty() {
            return nodeList.getLength() > 0;
        }

        @Override
        public boolean contains(final Object o) {
            if (o == null || !(o instanceof Node))
                return false;
            for (int i = 0; i < nodeList.getLength(); ++i)
                if (o == nodeList.item(i))
                    return true;
            return false;
        }

        @Override
        public Iterator<Node> iterator() {
            return new Iterator<Node>() {
                private int index = 0;

                @Override
                public boolean hasNext() {
                    return nodeList.getLength() > this.index;
                }

                @Override
                public Node next() {
                    if (this.index >= nodeList.getLength())
                        throw new NoSuchElementException();
                    return nodeList.item(this.index++);
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }

        @Override
        public Object[] toArray() {
            final Node[] array = new Node[nodeList.getLength()];
            for (int i = 0; i < array.length; ++i)
                array[i] = nodeList.item(i);
            return array;
        }

        @Override
        @SuppressWarnings({ "unchecked" })
        public <T> T[] toArray(final T[] a) throws ArrayStoreException {
            if (!a.getClass().getComponentType().isAssignableFrom(Node.class))
                throw new ArrayStoreException(
                        a.getClass().getComponentType().getName() + " is not the same or a supertype of Node");

            if (a.length >= nodeList.getLength()) {
                for (int i = 0; i < nodeList.getLength(); ++i)
                    a[i] = (T) nodeList.item(i);
                if (a.length > nodeList.getLength())
                    a[nodeList.getLength()] = null;
                return a;
            }

            return (T[]) toArray();
        }

        @Override
        public boolean add(final Node node) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean remove(final Object o) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean containsAll(final Collection<?> c) {
            for (final Object o : c)
                if (!this.contains(o))
                    return false;
            return true;
        }

        @Override
        public boolean addAll(final Collection<? extends Node> c) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean removeAll(final Collection<?> c) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean retainAll(final Collection<?> c) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void clear() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:XmlUtils.java

public static Collection<Node> wrapNodeList(final NodeList nodeList) throws IllegalArgumentException {
    if (nodeList == null)
        throw new IllegalArgumentException("Cannot wrap null NodeList");

    return new Collection<Node>() {
        @Override/* ww  w.  j  a v  a  2 s  .  c o  m*/
        public int size() {
            return nodeList.getLength();
        }

        @Override
        public boolean isEmpty() {
            return nodeList.getLength() > 0;
        }

        @Override
        public boolean contains(final Object o) {
            if (o == null || !(o instanceof Node))
                return false;
            for (int i = 0; i < nodeList.getLength(); ++i)
                if (o == nodeList.item(i))
                    return true;
            return false;
        }

        @Override
        public Iterator<Node> iterator() {
            return new Iterator<Node>() {
                private int index = 0;

                @Override
                public boolean hasNext() {
                    return nodeList.getLength() > this.index;
                }

                @Override
                public Node next() {
                    if (this.index >= nodeList.getLength())
                        throw new NoSuchElementException();
                    return nodeList.item(this.index++);
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }

        @Override
        public Object[] toArray() {
            final Node[] array = new Node[nodeList.getLength()];
            for (int i = 0; i < array.length; ++i)
                array[i] = nodeList.item(i);
            return array;
        }

        @Override
        @SuppressWarnings({ "unchecked" })
        public <T> T[] toArray(final T[] a) throws ArrayStoreException {
            if (!a.getClass().getComponentType().isAssignableFrom(Node.class))
                throw new ArrayStoreException(
                        a.getClass().getComponentType().getName() + " is not the same or a supertype of Node");

            if (a.length >= nodeList.getLength()) {
                for (int i = 0; i < nodeList.getLength(); ++i)
                    a[i] = (T) nodeList.item(i);
                if (a.length > nodeList.getLength())
                    a[nodeList.getLength()] = null;
                return a;
            }

            return (T[]) toArray();
        }

        @Override
        public boolean add(final Node node) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean remove(final Object o) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean containsAll(final Collection<?> c) {
            for (final Object o : c)
                if (!this.contains(o))
                    return false;
            return true;
        }

        @Override
        public boolean addAll(final Collection<? extends Node> c) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean removeAll(final Collection<?> c) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean retainAll(final Collection<?> c) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void clear() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:com.yarg.animatronics.datamodel.PwmBoard.java

/**
 * Add a motor to the board. If there are no more available channels to connect motors to an ArrayStoreException
 * will be thrown. If there is another motor already assigned to the same channel an ArrayStoreException will be
 * thrown. If the channels specified does not fit in the range available on the board then an ArrayStoreException
 * will be thrown./*from w w w .  j ava  2s . c o  m*/
 * @param motor Motor to add to board.
 */
public void addMotor(PwmMotor motor) {

    if (motor.getPwmChannel() > getNumberOfChannels()) {
        throw new ArrayStoreException(
                "Unable to add motor with PWM channel index greater than the number of available channels on the board.");
    } else if (motor.getPwmChannel() < 1) {
        throw new ArrayStoreException("Unable to add motor with PWM channel index less than 1.");
    }

    for (PwmMotor existingMotor : motors) {
        if (existingMotor.getPwmChannel() == motor.getPwmChannel()) {
            throw new ArrayStoreException(
                    "PWM channel is already in use by another motor on this board. Please choose a different channel.");
        }
    }

    motors.add(motor);
}

From source file:org.mili.core.collection.MapUtil.java

/**
 * Transforms a list with a transformation function to a map.
 *
 * @param l list with objects.//from   ww w.j a v  a  2s  .  co  m
 * @param f transformation function to get the key from an object.
 * @return transformed map based upon the list.
 * @throws ArrayStoreException is overwrite is false and duplicate keys occurs.
 * @throws IllegalStateException if error while using the transformation function occurs.
 */
public static <T, I> Map<I, T> listAsMap(boolean overwrite, List<T> l, UnaryFunction<T, I> f) {
    Validate.notNull(l, "list cannot be null!");
    Validate.notNull(f, "function cannot be null!");
    Map<I, T> m = new Hashtable<I, T>();
    try {
        for (int i = 0, n = l.size(); i < n; i++) {
            T o = l.get(i);
            I k = f.evaluate(o);
            if (m.containsKey(k) && !overwrite) {
                throw new ArrayStoreException("key " + k + " already exists in map!");
            }
            m.put(k, o);
        }
    } catch (ArrayStoreException e) {
        throw e;
    } catch (Exception e) {
        throw new IllegalStateException("exception while execute function occured!", e);
    }
    return m;
}

From source file:com.aerhard.oxygen.plugin.dbtagger.util.JsonUtil.java

/**
 * Converts a JSON array to from the JSON server response.
 * /*from   www . j a v  a2  s .  co  m*/
 * @param columns
 *            The number of columns.
 * @param rows
 *            The number of rows.
 * @param dataArray
 *            the input data.
 * 
 * @return the body content
 */
private String[][] convertArray(int rows, int columns, JSONArray dataArray) {
    String[][] resultTable = new String[rows][];
    for (int i = 0; i < rows; i++) {
        JSONArray arr = dataArray.getJSONArray(i);
        List<String> list = new ArrayList<String>();
        if (arr.length() < columns) {
            throw new ArrayStoreException(i18n.getString("jsonUtil.dataColumnError"));
        }
        for (int j = 0; j < columns; j++) {
            list.add(arr.isNull(j) ? "" : arr.getString(j));
        }
        resultTable[i] = list.toArray(new String[columns]);
    }
    return resultTable;
}

From source file:jef.tools.ArrayUtils.java

/**
 * addArray apacheadd??......//ww w .j  a  v  a  2  s .c om
 */
@SuppressWarnings("unchecked")
public static <T> T[] addElement(T[] array, T data, Class<T> componentType) {
    if (data == null)
        return array;
    T[] newArray;
    if (array == null) {
        Assert.notNull(componentType, "The componentType shoule be assigned when the array is null.");
        newArray = (T[]) Array.newInstance(componentType, 1);
        newArray[0] = data;
    } else {
        Class<?> containerType = array.getClass().getComponentType();
        if (!containerType.isAssignableFrom(data.getClass())) {// prompt the
            // type
            // error.
            throw new ArrayStoreException("The new element which typed " + data.getClass().getName()
                    + " can not be put into a array whoes type is " + containerType.getName());
        }
        newArray = (T[]) Array.newInstance(containerType, array.length + 1);
        System.arraycopy(array, 0, newArray, 0, array.length);
        newArray[array.length] = data;
    }
    return newArray;
}