Example usage for java.lang.reflect Array getDouble

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

Introduction

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

Prototype

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

Source Link

Document

Returns the value of the indexed component in the specified array object, as a double .

Usage

From source file:Main.java

public static void main(String args[]) {
    double[] array = new double[] { 1.1, 2.2, 3.3 };

    double value = Array.getDouble(array, 1);

    System.out.println(value);//from w  ww  . j a va2s.c o  m

}

From source file:gda.analysis.numerical.straightline.StraightLineFit.java

public static Results fitInt(List<Object> data, long[] dims, double[] x) {

    Object object = data.get(0);/*w  w w  .  j  ava  2 s  .com*/
    if (!object.getClass().isArray()) {
        throw new IllegalArgumentException("fitInt can only accept arrays");
    }
    int numLines = ArrayUtils.getLength(object);
    int pointsPerLine = x.length;
    if (data.size() != pointsPerLine)
        throw new IllegalArgumentException("data.size() != pointsPerLine");

    for (int i = 0; i < pointsPerLine; i++) {
        if (ArrayUtils.getLength(data.get(i)) != numLines)
            throw new IllegalArgumentException("data.get(i).length != numLines");

    }

    double[] slopes = new double[numLines];
    double[] offsets = new double[numLines];
    short[] fitoks = new short[numLines];
    Arrays.fill(fitoks, (short) 0);
    if (pointsPerLine > 2) {
        double xAverage = getXAverage(x);
        double x1 = getX(x, xAverage);
        double[] y = new double[pointsPerLine];

        Arrays.fill(fitoks, (short) 1);
        for (int line = 0; line < numLines; line++) {
            for (int point = 0; point < pointsPerLine; point++) {
                y[point] = Array.getDouble(data.get(point), line);
            }
            Result fit2 = fit2(y, x, xAverage, x1);
            slopes[line] = fit2.getSlope();
            offsets[line] = fit2.getOffset();
        }
    } else if (pointsPerLine == 2) {

        double[] y = new double[pointsPerLine];
        Arrays.fill(fitoks, (short) 1);
        for (int line = 0; line < numLines; line++) {
            for (int point = 0; point < pointsPerLine; point++) {
                y[point] = Array.getDouble(data.get(point), line);
            }

            slopes[line] = (y[1] - y[0]) / (x[1] - x[0]);
            offsets[line] = y[1] - slopes[line] * x[1];
        }
    }
    return new Results(offsets, slopes, dims, fitoks);
}

From source file:gda.device.scannable.ScannableUtils.java

/**
 * Returns an array which is the current position, but with the 'extra' fields removed
 * //w w  w .j  a  va  2  s .  c o m
 * @param scannable
 * @return the position as an array of doubles
 * @throws Exception
 */
public static double[] getCurrentPositionArray_InputsOnly(Scannable scannable) throws Exception {

    // get object returned by getPosition
    Object currentPositionObj = scannable.getPosition();

    // if its null or were expecting it to be null from the arrays, return
    // null
    if (currentPositionObj == null || scannable.getInputNames().length == 0) {
        return null;
    }

    // else create an array of the expected size and fill it
    double[] currentPosition = new double[scannable.getInputNames().length];
    for (int i = 0; i < currentPosition.length; i++) {
        if (currentPositionObj.getClass().isArray()) {
            currentPosition[i] = Array.getDouble(currentPositionObj, i);
        } else if (currentPositionObj instanceof PySequence) {
            currentPosition[i] = Double
                    .parseDouble(((PySequence) currentPositionObj).__finditem__(i).toString());
        } else {
            currentPosition[i] = Double.parseDouble(currentPositionObj.toString());
        }
    }
    return currentPosition;

}

From source file:gda.device.scannable.ScannableUtils.java

@SuppressWarnings("rawtypes")
private static Double getDouble(Object val, int index) {
    if (val instanceof Number[]) {
        return ((Number[]) val)[index].doubleValue();
    }//from   w  w w .ja v  a 2s  . co m
    if (val.getClass().isArray()) {
        return Array.getDouble(val, index);
    }
    if (val instanceof PySequence) {
        if (((PySequence) val).__finditem__(index) instanceof PyNone) {
            return null;
        }
        return Double.parseDouble(((PySequence) val).__finditem__(index).toString());
    }
    if (val instanceof List) {
        return Double.parseDouble(((List) val).get(index).toString());
    }
    throw new IllegalArgumentException("getDouble. Object cannot be converted to Double");
}

From source file:gda.device.scannable.ScannableUtils.java

/**
 * Converts a position object to an array of doubles. It position is an Angle quantity it is converted to degrees,
 * if a linear quantity it is converted to mm, else it is just treated as a number.
 * //from   w w  w .  j av a2 s. c o m
 * @param position
 * @return array of doubles
 */
public static Double[] objectToArray(Object position) {

    Double[] posArray = new Double[0];
    if (position instanceof Double[]) {
        return (Double[]) position;

    }
    if (position instanceof Number[]) {
        int length = ArrayUtils.getLength(position);
        posArray = new Double[length];
        for (int i = 0; i < length; i++) {
            posArray[i] = ((Number[]) position)[i].doubleValue();
        }
        return posArray;
    } else if (position instanceof Object[]) {
        int length = ((Object[]) position).length;
        posArray = new Double[length];
        for (int i = 0; i < length; i++) {
            Object obj = ((Object[]) position)[i];
            Double val = null;
            if (obj instanceof String) {
                val = Double.parseDouble((String) obj);
            } else if (obj instanceof Number) {
                val = ((Number) obj).doubleValue();
            } else if (obj instanceof PyObject) {
                val = (Double) ((PyObject) obj).__tojava__(Double.class);
            }
            posArray[i] = val;
        }
        return posArray;
    }
    // if its a Java array
    else if (position.getClass().isArray()) {
        int length = ArrayUtils.getLength(position);
        for (int i = 0; i < length; i++) {
            posArray = (Double[]) ArrayUtils.add(posArray, Array.getDouble(position, i));
        }
        return posArray;
    }
    // if its a Jython array
    else if (position instanceof PySequence) {
        int length = ((PySequence) position).__len__();
        for (int i = 0; i < length; i++) {
            posArray = (Double[]) ArrayUtils.add(posArray, getDouble(position, i));
        }
    }
    // if its a Jython array
    else if (position instanceof List) {
        @SuppressWarnings("rawtypes")
        int length = ((List) position).size();
        for (int i = 0; i < length; i++) {
            posArray = (Double[]) ArrayUtils.add(posArray, getDouble(position, i));
        }
    }
    // if its a Quantity
    else if (position instanceof Quantity) {
        posArray = (Double[]) ArrayUtils.add(posArray, ((Quantity) position).getAmount());
    }
    // if its a String, then try to convert to a double
    else if (position instanceof String) {
        Quantity posAsQ = QuantityFactory.createFromString((String) position);
        Double posAsDouble = posAsQ != null ? posAsQ.getAmount() : Double.parseDouble((String) position);
        posArray = (Double[]) ArrayUtils.add(posArray, posAsDouble);
    }
    // else assume its some object whose toString() method returns a String which can be converted
    else {
        posArray = (Double[]) ArrayUtils.add(posArray, Double.parseDouble(position.toString()));
    }
    return posArray;
}

From source file:alice.tuprolog.lib.OOLibrary.java

/**
 * Sets the value of the field 'i' with 'what'
 * @param obj_id//from ww  w  .j  a  v a 2 s.c o m
 * @param i
 * @param what
 * @return
 * @throws JavaException
 */
public boolean java_array_get_primitive_3(PTerm obj_id, PTerm i, PTerm what) throws JavaException {
    Struct objId = (Struct) obj_id.getTerm();
    Number index = (Number) i.getTerm();
    what = what.getTerm();
    Object obj = null;
    if (!index.isInteger()) {
        throw new JavaException(new IllegalArgumentException(index.toString()));
    }
    try {
        Class<?> cl = null;
        String objName = alice.util.Tools.removeApices(objId.toString());
        obj = currentObjects.get(objName);
        if (obj != null) {
            cl = obj.getClass();
        } else {
            throw new JavaException(new IllegalArgumentException(objId.toString()));
        }

        if (!cl.isArray()) {
            throw new JavaException(new IllegalArgumentException(objId.toString()));
        }
        String name = cl.toString();
        switch (name) {
        case "class [I": {
            PTerm value = new Int(Array.getInt(obj, index.intValue()));
            if (unify(what, value))
                return true;
            else
                throw new JavaException(new IllegalArgumentException(what.toString()));
        }
        case "class [D": {
            PTerm value = new alice.tuprolog.Double(Array.getDouble(obj, index.intValue()));
            if (unify(what, value))
                return true;
            else
                throw new JavaException(new IllegalArgumentException(what.toString()));
        }
        case "class [F": {
            PTerm value = new alice.tuprolog.Float(Array.getFloat(obj, index.intValue()));
            if (unify(what, value))
                return true;
            else
                throw new JavaException(new IllegalArgumentException(what.toString()));
        }
        case "class [L": {
            PTerm value = new alice.tuprolog.Long(Array.getLong(obj, index.intValue()));
            if (unify(what, value))
                return true;
            else
                throw new JavaException(new IllegalArgumentException(what.toString()));
        }
        case "class [C": {
            PTerm value = new Struct("" + Array.getChar(obj, index.intValue()));
            if (unify(what, value))
                return true;
            else
                throw new JavaException(new IllegalArgumentException(what.toString()));
        }
        case "class [Z":
            boolean b = Array.getBoolean(obj, index.intValue());
            if (b) {
                if (unify(what, PTerm.TRUE))
                    return true;
                else
                    throw new JavaException(new IllegalArgumentException(what.toString()));
            } else {
                if (unify(what, PTerm.FALSE))
                    return true;
                else
                    throw new JavaException(new IllegalArgumentException(what.toString()));
            }
        case "class [B": {
            PTerm value = new Int(Array.getByte(obj, index.intValue()));
            if (unify(what, value))
                return true;
            else
                throw new JavaException(new IllegalArgumentException(what.toString()));
        }
        case "class [S": {
            PTerm value = new Int(Array.getInt(obj, index.intValue()));
            if (unify(what, value))
                return true;
            else
                throw new JavaException(new IllegalArgumentException(what.toString()));
        }
        default:
            throw new JavaException(new Exception());
        }
    } catch (Exception ex) {
        // ex.printStackTrace();
        throw new JavaException(ex);
    }

}

From source file:org.jgentleframework.core.interceptor.AnnotationAroundAdviceMethodInterceptor.java

/**
 * Hash code./*ww  w .j  a v a 2 s  . com*/
 * 
 * @param proxy
 *            the proxy
 * @return the int
 * @throws IllegalArgumentException
 *             the illegal argument exception
 * @throws IllegalAccessException
 *             the illegal access exception
 * @throws InvocationTargetException
 *             the invocation target exception
 */
protected int hashCode(Object proxy)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

    Method[] methods = targetClass.getDeclaredMethods();
    int hashCode = 0;
    for (Method method : methods) {
        Class<?> type = method.getReturnType();
        Object value = (Object) this.attributesMapping.get(method);
        if (value == null)
            value = method.getDefaultValue();
        // if type is primitive type
        if (type.isPrimitive()) {
            // //////////////////////////////////
            // //////////////////////////////////
            hashCode += 127 * method.getName().hashCode() ^ value.hashCode();
        } else if (type.isArray()) {
            Object array = method.invoke(proxy);
            Class<?> comtype = type.getComponentType();
            if (comtype == byte.class || comtype == Byte.class) {
                byte[] valueArr = new byte[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getByte(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == char.class || comtype == Character.class) {
                char[] valueArr = new char[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getChar(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == double.class || comtype == Double.class) {
                double[] valueArr = new double[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getDouble(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == float.class || comtype == Float.class) {
                float[] valueArr = new float[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getFloat(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == int.class || comtype == Integer.class) {
                int[] valueArr = new int[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getInt(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == long.class || comtype == Long.class) {
                long[] valueArr = new long[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getLong(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == short.class || comtype == Short.class) {
                short[] valueArr = new short[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getShort(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else if (comtype == boolean.class || comtype == Boolean.class) {
                boolean[] valueArr = new boolean[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.getBoolean(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            } else {
                Object[] valueArr = new Object[Array.getLength(array)];
                for (int i = 0; i < valueArr.length; i++) {
                    valueArr[i] = Array.get(array, i);
                }
                hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr);
            }
        } else {
            // Object value = method.invoke(proxy);
            hashCode += 127 * method.getName().hashCode() ^ value.hashCode();
        }
    }
    return hashCode;
}