Search in sources :

Example 1 with JavaClassExpr

use of org.matheclipse.core.expression.data.JavaClassExpr in project symja_android_library by axkr.

the class JavaFunctions method determineParameters.

public static Object[] determineParameters(final IAST ast, Parameter[] parameters, int offset) {
    try {
        Object[] params = new Object[parameters.length];
        for (int j = 0; j < parameters.length; j++) {
            Parameter p = parameters[j];
            IExpr arg = ast.get(j + offset);
            Class<?> clazz = p.getType();
            if (arg instanceof DataExpr<?>) {
                Object obj = ((DataExpr) arg).toData();
                if (clazz.isInstance(obj)) {
                    params[j] = obj;
                    continue;
                }
            }
            if (clazz.isInstance(arg)) {
                params[j] = arg;
            } else if (clazz.equals(boolean.class)) {
                if (arg.isTrue()) {
                    params[j] = Boolean.TRUE;
                } else if (arg.isFalse()) {
                    params[j] = Boolean.FALSE;
                } else {
                    return null;
                }
            } else if (clazz.equals(double.class)) {
                params[j] = Double.valueOf(arg.evalDouble());
            } else if (clazz.equals(float.class)) {
                params[j] = Float.valueOf((float) arg.evalDouble());
            } else if (clazz.equals(int.class)) {
                int n = arg.toIntDefault();
                if (n == Integer.MIN_VALUE) {
                    return null;
                }
                params[j] = Integer.valueOf(n);
            } else if (clazz.equals(long.class)) {
                long l = arg.toLongDefault();
                if (l == Long.MIN_VALUE) {
                    return null;
                }
                params[j] = Long.valueOf(l);
            } else if (clazz.equals(short.class)) {
                int s = arg.toIntDefault();
                if (s < Short.MIN_VALUE || s > Short.MAX_VALUE) {
                    return null;
                }
                params[j] = Short.valueOf((short) s);
            } else if (clazz.equals(byte.class)) {
                int b = arg.toIntDefault();
                if (b < Byte.MIN_VALUE || b > Byte.MAX_VALUE) {
                    return null;
                }
                params[j] = Byte.valueOf((byte) b);
            } else if (clazz.equals(char.class)) {
                if (!arg.isString()) {
                    return null;
                }
                String str = arg.toString();
                if (str.length() != 1) {
                    return null;
                }
                params[j] = Character.valueOf(str.charAt(0));
            } else if (clazz.equals(String.class)) {
                if (!arg.isString()) {
                    return null;
                }
                params[j] = arg.toString();
            } else if (clazz.equals(org.hipparchus.complex.Complex.class)) {
                org.hipparchus.complex.Complex complex = arg.evalComplex();
                if (complex == null) {
                    return null;
                }
                params[j] = complex;
            } else if (clazz.equals(Class.class) && arg instanceof JavaClassExpr) {
                params[j] = ((JavaClassExpr) arg).toData();
            } else {
                params[j] = arg;
            }
        }
        return params;
    } catch (ArgumentTypeException atex) {
    }
    return null;
}
Also used : DataExpr(org.matheclipse.core.expression.DataExpr) JavaClassExpr(org.matheclipse.core.expression.data.JavaClassExpr) Parameter(java.lang.reflect.Parameter) IExpr(org.matheclipse.core.interfaces.IExpr) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException)

Aggregations

Parameter (java.lang.reflect.Parameter)1 ArgumentTypeException (org.matheclipse.core.eval.exception.ArgumentTypeException)1 DataExpr (org.matheclipse.core.expression.DataExpr)1 JavaClassExpr (org.matheclipse.core.expression.data.JavaClassExpr)1 IExpr (org.matheclipse.core.interfaces.IExpr)1