Exemplo n.º 1
0
  /* (non-Javadoc)
   * @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
   */
  public Object evaluate(
      InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
    String method = expr.getOperation();

    if (invokedValue == null) {
      return null;
    }
    if (!(invokedValue instanceof List)) {
      throw new NucleusException(
          LOCALISER.msg("021011", method, invokedValue.getClass().getName()));
    }

    Object param = expr.getArguments().get(0);
    Object paramValue = null;
    if (param instanceof Literal) {
      paramValue = ((Literal) param).getLiteral();
    } else if (param instanceof PrimaryExpression) {
      PrimaryExpression primExpr = (PrimaryExpression) param;
      paramValue = eval.getValueForPrimaryExpression(primExpr);
    } else if (param instanceof ParameterExpression) {
      ParameterExpression paramExpr = (ParameterExpression) param;
      paramValue = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
    } else if (param instanceof VariableExpression) {
      VariableExpression varExpr = (VariableExpression) param;
      try {
        paramValue = eval.getValueForVariableExpression(varExpr);
      } catch (VariableNotSetException vnse) {
        // Throw an exception with the possible values of elements
        throw new VariableNotSetException(varExpr, ((List) invokedValue).toArray());
      }
    } else {
      throw new NucleusException(
          "Dont currently support use of get(" + param.getClass().getName() + ")");
    }

    int paramInt;
    if (paramValue instanceof Number) {
      paramInt = ((Number) paramValue).intValue();
    } else {
      throw new NucleusException("List.get() should take in an integer but is " + paramValue);
    }
    return ((List) invokedValue).get(paramInt);
  }
Exemplo n.º 2
0
  /* (non-Javadoc)
   * @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
   */
  public Object evaluate(
      InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
    String method = expr.getOperation();
    Object param1 = expr.getArguments().get(0);
    int param1Value = -1;
    if (param1 instanceof PrimaryExpression) {
      PrimaryExpression primExpr = (PrimaryExpression) param1;
      Object val = eval.getValueForPrimaryExpression(primExpr);
      if (val instanceof Number) {
        param1Value = ((Number) val).intValue();
      } else {
        throw new NucleusException(
            method
                + "(num1, num2) where num1 is instanceof "
                + param1.getClass().getName()
                + " but should be integer");
      }
    } else if (param1 instanceof ParameterExpression) {
      ParameterExpression paramExpr = (ParameterExpression) param1;
      Object val = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
      if (val instanceof Number) {
        param1Value = ((Number) val).intValue();
      } else {
        throw new NucleusException(
            method
                + "(num1, num2) where num1 is instanceof "
                + param1.getClass().getName()
                + " but should be integer");
      }
    } else if (param1 instanceof Literal) {
      Object val = ((Literal) param1).getLiteral();
      if (val instanceof Number) {
        param1Value = ((Number) val).intValue();
      } else {
        throw new NucleusException(
            method
                + "(num1, num2) where num1 is instanceof "
                + param1.getClass().getName()
                + " but should be integer");
      }
    } else {
      throw new NucleusException(
          method
              + "(num1, num2) where num1 is instanceof "
              + param1.getClass().getName()
              + " not supported");
    }

    Object param2 = expr.getArguments().get(1);
    int param2Value = -1;
    if (param2 instanceof PrimaryExpression) {
      PrimaryExpression primExpr = (PrimaryExpression) param2;
      Object val = eval.getValueForPrimaryExpression(primExpr);
      if (val instanceof Number) {
        param2Value = ((Number) val).intValue();
      } else {
        throw new NucleusException(
            method
                + "(num1, num2) where num2 is instanceof "
                + param2.getClass().getName()
                + " but should be integer");
      }
    } else if (param2 instanceof ParameterExpression) {
      ParameterExpression paramExpr = (ParameterExpression) param2;
      Object val = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
      if (val instanceof Number) {
        param2Value = ((Number) val).intValue();
      } else {
        throw new NucleusException(
            method
                + "(num1, num2) where num1 is instanceof "
                + param2.getClass().getName()
                + " but should be integer");
      }
    } else if (param2 instanceof Literal) {
      Object val = ((Literal) param2).getLiteral();
      if (val instanceof Number) {
        param2Value = ((Number) val).intValue();
      } else {
        throw new NucleusException(
            method
                + "(num1, num2) where num2 is instanceof "
                + param2.getClass().getName()
                + " but should be integer");
      }
    } else {
      throw new NucleusException(
          method
              + "(num1, num2) where num2 is instanceof "
              + param2.getClass().getName()
              + " not supported");
    }

    return Integer.valueOf(param1Value % param2Value);
  }