Example usage for java.lang.reflect Array get

List of usage examples for java.lang.reflect Array get

Introduction

In this page you can find the example usage for java.lang.reflect Array get.

Prototype

public static native Object get(Object array, int index)
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

Source Link

Document

Returns the value of the indexed component in the specified array object.

Usage

From source file:edu.umn.cs.spatialHadoop.nasa.StockQuadTree.java

/**
 * Constructs an aggregate quad tree out of a two-dimensional array of values.
 * /*from   w w  w  .  ja  v  a2 s . com*/
 * @param values
 * @param out
 *          - the output stream to write the constructed quad tree to
 * @throws IOException
 */
public static void build(NASADataset metadata, short[] values, short fillValue, DataOutputStream out)
        throws IOException {
    int length = Array.getLength(values);
    int resolution = (int) Math.round(Math.sqrt(length));

    // Write tree header
    out.writeInt(resolution); // resolution
    out.writeShort(fillValue);
    out.writeInt(1); // cardinality
    out.writeLong(metadata.time); // Timestamp

    // Fetch the stock quad tree of the associated resolution
    StockQuadTree stockQuadTree = getOrCreateStockQuadTree(resolution);
    // Sort values by their respective Z-Order values in linear time
    short[] sortedValues = new short[length];
    for (int i = 0; i < length; i++)
        sortedValues[i] = values[stockQuadTree.r[i]];

    // Write all sorted values
    for (short v : sortedValues)
        out.writeShort(v);

    // Compute aggregate values for all nodes in the tree
    // Go in reverse ID order to ensure children are computed before parents
    Node[] nodes = new Node[stockQuadTree.nodesID.length];
    for (int iNode = stockQuadTree.nodesID.length - 1; iNode >= 0; iNode--) {
        // Initialize all aggregate values
        nodes[iNode] = new Node();

        int firstChildId = stockQuadTree.nodesID[iNode] * 4;
        int firstChildPos = Arrays.binarySearch(stockQuadTree.nodesID, firstChildId);
        boolean isLeaf = firstChildPos < 0;

        if (isLeaf) {
            for (int iVal = stockQuadTree.nodesStartPosition[iNode]; iVal < stockQuadTree.nodesEndPosition[iNode]; iVal++) {
                short value;
                Object val = Array.get(sortedValues, iVal);
                if (val instanceof Short) {
                    value = (Short) val;
                } else {
                    throw new RuntimeException("Cannot handle values of type " + val.getClass());
                }
                if (value != fillValue)
                    nodes[iNode].accumulate(value);
            }
        } else {
            // Compute from the four children
            for (int iChild = 0; iChild < 4; iChild++) {
                int childPos = firstChildPos + iChild;
                nodes[iNode].accumulate(nodes[childPos]);
            }
        }
    }

    // Write nodes to file in sorted order
    for (int iNode = 0; iNode < nodes.length; iNode++)
        nodes[iNode].write(out);
}

From source file:com.frostwire.android.LollipopFileSystem.java

private static String getVolumeId(Context context, final String volumePath) {
    try {//from ww  w. j a v a  2s  . com
        StorageManager mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);

        Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");

        Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
        Method getUuid = storageVolumeClazz.getMethod("getUuid");
        Method getPath = storageVolumeClazz.getMethod("getPath");
        Object result = getVolumeList.invoke(mStorageManager);

        final int length = Array.getLength(result);
        for (int i = 0; i < length; i++) {
            Object storageVolumeElement = Array.get(result, i);
            String path = (String) getPath.invoke(storageVolumeElement);

            if (path != null) {
                if (path.equals(volumePath)) {
                    return (String) getUuid.invoke(storageVolumeElement);
                }
            }
        }

        // not found.
        return null;
    } catch (Exception ex) {
        return null;
    }
}

From source file:org.mypsycho.beans.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./*from w  w w  . j av  a2  s  .c  o  m*/
 *
 * @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
 * @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() + "'");
    }

    // Retrieve the property descriptor for the specified property
    PropertyDescriptor descriptor = getRequiredDescriptor(bean, name);
    return invoker.get(bean, descriptor, index);
}

From source file:com.flexive.faces.FxJsfComponentUtils.java

/**
 * Check if an item is selected//from ww  w .jav  a  2  s .c o  m
 *
 * @param context    faces context
 * @param component  our component
 * @param itemValue  the value to check for selection
 * @param valueArray all values to compare against
 * @param converter  the converter
 * @return selected
 */
public static boolean isSelectItemSelected(FacesContext context, UIComponent component, Object itemValue,
        Object valueArray, Converter converter) {
    if (itemValue == null && valueArray == null)
        return true;
    if (null != valueArray) {
        if (!valueArray.getClass().isArray()) {
            LOG.warn("valueArray is not an array, the actual type is " + valueArray.getClass());
            return valueArray.equals(itemValue);
        }
        int len = Array.getLength(valueArray);
        for (int i = 0; i < len; i++) {
            Object value = Array.get(valueArray, i);
            if (value == null && itemValue == null) {
                return true;
            } else {
                if ((value == null) ^ (itemValue == null))
                    continue;

                Object compareValue;
                if (converter == null) {
                    compareValue = coerceToModelType(context, itemValue, value.getClass());
                } else {
                    compareValue = itemValue;
                    if (compareValue instanceof String && !(value instanceof String)) {
                        // type mismatch between the time and the value we're
                        // comparing.  Invoke the Converter.
                        compareValue = converter.getAsObject(context, component, (String) compareValue);
                    }
                }

                if (value.equals(compareValue))
                    return true; //selected
            }
        }
    }
    return false;
}

From source file:org.eclipse.january.dataset.DTypeUtils.java

public static double toReal(final Object b) {
    if (b instanceof Number) {
        return ((Number) b).doubleValue();
    } else if (b instanceof Boolean) {
        return ((Boolean) b).booleanValue() ? 1 : 0;
    } else if (b instanceof Complex) {
        return ((Complex) b).getReal();
    } else if (b.getClass().isArray()) {
        if (Array.getLength(b) == 0)
            return 0;
        return toReal(Array.get(b, 0));
    } else if (b instanceof Dataset) {
        Dataset db = (Dataset) b;/* w  w  w  . jav a  2s.c om*/
        if (db.getSize() != 1) {
            logger.error("Given dataset must have only one item");
            throw new IllegalArgumentException("Given dataset must have only one item");
        }
        return toReal(db.getObjectAbs(0));
    } else if (b instanceof IDataset) {
        IDataset db = (Dataset) b;
        if (db.getSize() != 1) {
            logger.error("Given dataset must have only one item");
            throw new IllegalArgumentException("Given dataset must have only one item");
        }
        return toReal(db.getObject(new int[db.getRank()]));
    } else {
        logger.error("Argument is of unsupported class");
        throw new IllegalArgumentException("Argument is of unsupported class");
    }
}

From source file:org.enerj.apache.commons.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
 *
 * @exception ArrayIndexOutOfBoundsException if the specified index
 *  is outside the valid range for the underlying array
 * @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//from w  w w.  j  a v  a 2s.c  om
 * @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) {
        throw new IllegalArgumentException("No name specified");
    }

    // Handle DynaBean instances specially
    if (bean instanceof DynaBean) {
        DynaProperty descriptor = ((DynaBean) bean).getDynaClass().getDynaProperty(name);
        if (descriptor == null) {
            throw new NoSuchMethodException("Unknown property '" + name + "'");
        }
        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 + "'");
    }

    // Call the indexed getter method if there is one
    if (descriptor instanceof IndexedPropertyDescriptor) {
        Method readMethod = ((IndexedPropertyDescriptor) descriptor).getIndexedReadMethod();
        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 ArrayIndexOutOfBoundsException) {
                    throw (ArrayIndexOutOfBoundsException) e.getTargetException();
                } else {
                    throw e;
                }
            }
        }
    }

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

    // Call the property getter and return the value
    Object value = invokeMethod(readMethod, bean, new Object[0]);
    if (!value.getClass().isArray()) {
        if (!(value instanceof java.util.List)) {
            throw new IllegalArgumentException("Property '" + name + "' is not indexed");
        } else {
            //get the List's value
            return ((java.util.List) value).get(index);
        }
    } else {
        //get the array's value
        return (Array.get(value, index));
    }

}

From source file:kx.c.java

public static Object[] autoboxArray(Object data) {
    if (data.getClass().isArray()) {
        Object[] dstArray = new Object[Array.getLength(data)];
        for (int row = 0; row < Array.getLength(data); row++) {
            Object o = Array.get(data, row);
            if (o == null) {
                o = "::";
            }//from ww w.  ja va  2s  .c om
            if (o instanceof char[]) {
                // char arrays are strings...
                o = new String((char[]) o);
            } else if (o.getClass().isArray()) {
                o = autoboxArray(o);
            }
            Array.set(dstArray, row, o);

        }
        return dstArray;
    }
    throw new IncompleteArgumentException("data was not an array");
}

From source file:de.undercouch.gradle.tasks.download.DownloadAction.java

@Override
public void src(Object src) throws MalformedURLException {
    if (src instanceof Closure) {
        //lazily evaluate closure
        Closure<?> closure = (Closure<?>) src;
        src = closure.call();/* ww  w. j  a v  a 2s.  com*/
    }

    if (src instanceof CharSequence) {
        sources.add(new URL(src.toString()));
    } else if (src instanceof URL) {
        sources.add((URL) src);
    } else if (src instanceof Collection) {
        Collection<?> sc = (Collection<?>) src;
        for (Object sco : sc) {
            src(sco);
        }
    } else if (src != null && src.getClass().isArray()) {
        int len = Array.getLength(src);
        for (int i = 0; i < len; ++i) {
            Object sco = Array.get(src, i);
            src(sco);
        }
    } else {
        throw new IllegalArgumentException(
                "Download source must " + "either be a URL, a CharSequence, a Collection or an array.");
    }
}

From source file:com.taobao.adfs.util.Utilities.java

public static boolean deepEquals(Object object0, Object object1) {
    if (object0 == null && object1 == null)
        return true;
    if (object0 == null && object1 != null)
        return false;
    if (object0 != null && object1 == null)
        return false;
    if (!object0.getClass().equals(object1.getClass()))
        return false;
    if (object0.equals(object1))
        return true;
    if (object0.getClass().isArray() && Array.getLength(object0) == Array.getLength(object1)) {
        int length = Array.getLength(object0);
        for (int i = 0; i < length; ++i) {
            if (!deepEquals(Array.get(object0, i), Array.get(object1, i)))
                return false;
        }//w w  w .  j  a va  2  s  . co  m
        return true;
    }
    return false;
}

From source file:com.frostwire.android.LollipopFileSystem.java

private static String getVolumePath(StorageManager mStorageManager, final String volumeId) {
    try {// w w  w  .j  a va 2s  . c  o m
        Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");

        Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
        Method getUuid = storageVolumeClazz.getMethod("getUuid");
        Method getPath = storageVolumeClazz.getMethod("getPath");
        Method isPrimary = storageVolumeClazz.getMethod("isPrimary");
        Object result = getVolumeList.invoke(mStorageManager);

        final int length = Array.getLength(result);
        for (int i = 0; i < length; i++) {
            Object storageVolumeElement = Array.get(result, i);
            String uuid = (String) getUuid.invoke(storageVolumeElement);
            Boolean primary = (Boolean) isPrimary.invoke(storageVolumeElement);

            // primary volume?
            if (primary && "primary".equals(volumeId)) {
                return (String) getPath.invoke(storageVolumeElement);
            }

            // other volumes?
            if (uuid != null) {
                if (uuid.equals(volumeId)) {
                    return (String) getPath.invoke(storageVolumeElement);
                }
            }
        }

        // not found.
        return null;
    } catch (Exception ex) {
        return null;
    }
}