Пример #1
0
  @Override
  public boolean evaluate(InvocationContext context) {
    Object left = ((AbstractExpression) jjtGetChild(0)).value(context);
    Object right = ((AbstractExpression) jjtGetChild(1)).value(context);
    if (left instanceof Number && right instanceof Number) {
      return MathUtils.compare((Number) left, (Number) right) != 0;
    }

    // 都不为null,一个是另一个的子类,则使用equals操作
    if (left != null
        && right != null
        && (left.getClass().isAssignableFrom(right.getClass())
            || right.getClass().isAssignableFrom(left.getClass()))) {
      return !left.equals(right);
    }

    if (left == null && right == null) { // 都为null
      return false;
    } else if (left == null || right == null) { // 一个为null,一个不为null
      return true;
    } else { // 都不为null
      left = left.toString();
      right = right.toString();
      return !left.equals(right);
    }
  }
Пример #2
0
  /**
   * Calculates the value of the logical expression
   *
   * <p>arg1 == arg2
   *
   * <p>All class types are supported. Uses equals() to determine equivalence. This should work as
   * we represent with the types we already support, and anything else that implements equals() to
   * mean more than identical references.
   *
   * @param context internal context used to evaluate the LHS and RHS
   * @return true if equivalent, false if not equivalent, false if not compatible arguments, or
   *     false if either LHS or RHS is null
   * @throws MethodInvocationException
   */
  public boolean evaluate(InternalContextAdapter context) throws MethodInvocationException {
    Object left = jjtGetChild(0).value(context);
    Object right = jjtGetChild(1).value(context);

    /*
     *  convert to Number if applicable
     */
    if (left instanceof TemplateNumber) {
      left = ((TemplateNumber) left).getAsNumber();
    }
    if (right instanceof TemplateNumber) {
      right = ((TemplateNumber) right).getAsNumber();
    }

    /*
     * If comparing Numbers we do not care about the Class.
     */
    if (left instanceof Number && right instanceof Number) {
      return MathUtils.compare((Number) left, (Number) right) == 0;
    }

    /**
     * if both are not null, then assume that if one class is a subclass of the other that we should
     * use the equals operator
     */
    if (left != null
        && right != null
        && (left.getClass().isAssignableFrom(right.getClass())
            || right.getClass().isAssignableFrom(left.getClass()))) {
      return left.equals(right);
    }

    /*
     * Ok, time to compare string values
     */
    left = (left == null) ? null : left.toString();
    right = (right == null) ? null : right.toString();

    if (left == null && right == null) {
      if (log.isDebugEnabled()) {
        log.debug(
            "Both right ("
                + getLiteral(false)
                + " and left "
                + getLiteral(true)
                + " sides of '==' operation returned null."
                + "If references, they may not be in the context."
                + getLocation(context));
      }
      return true;
    } else if (left == null || right == null) {
      if (log.isDebugEnabled()) {
        log.debug(
            (left == null ? "Left" : "Right")
                + " side ("
                + getLiteral(left == null)
                + ") of '==' operation has null value. If it is a "
                + "reference, it may not be in the context or its "
                + "toString() returned null. "
                + getLocation(context));
      }
      return false;
    } else {
      return left.equals(right);
    }
  }