Search code examples
javaarrayschar

Java Convert two char Operator String to working Operator?


Have char[] operator = // "+" or "<" or "<=" etc
  if(op.length == 1) {
    switch (op[0]) {
      case '+':
        if (a instanceof Double || b instanceof Double) {
          result = a.doubleValue() + b.doubleValue();
        } else if (a instanceof Long && b instanceof Long) {
          result = a.longValue() + b.longValue();
        }
        break;
      case '-':
        if (a instanceof Double || b instanceof Double) {
          result = a.doubleValue() - b.doubleValue();
        } else if (a instanceof Long && b instanceof Long) {
          result = a.longValue() - b.longValue();
        }
        break;
      case '*':
        if (a instanceof Double || b instanceof Double) {
          result = a.doubleValue() * b.doubleValue();
        } else if (a instanceof Long && b instanceof Long) {
          result = a.longValue() * b.longValue();
        }
        break;
      case '^':
        if (a instanceof Double || b instanceof Double) {
          result = Double.valueOf(Math.pow(a.doubleValue(), b.doubleValue()));
        } else if (a instanceof Long && b instanceof Long) {
          result = (long) Math.pow(a.longValue(), b.longValue());
        }
        break;
      case '/':
        if (b.doubleValue() == 0) {
          throw new UnsupportedOperationException("Cannot divide by zero");
        }
        if (a instanceof Double || b instanceof Double) {
          result = a.doubleValue() / b.doubleValue();
        } else if (a instanceof Long && b instanceof Long) {
          result = a.longValue() / b.longValue();
        }
        break;
      case '>':
        resultDecorator.setTypeOfexpresion(EvaluatorConstants.Boolean);
      if (a instanceof Double || b instanceof Double) {
        result = a.doubleValue() > b.doubleValue()?1:0;
      } else if (a instanceof Long && b instanceof Long) {
        result = a.longValue() > b.longValue()?1:0;
      }
      break;
    case '<':
      resultDecorator.setTypeOfexpresion(EvaluatorConstants.Boolean);
      if (a instanceof Double || b instanceof Double) {
        result = a.doubleValue() < b.doubleValue()?1:0;
      } else if (a instanceof Long && b instanceof Long) {
        result = a.longValue() < b.longValue()?1:0;
      }
      break;
    }
  } else if (op.length == 2) {
    resultDecorator.setTypeOfexpresion(EvaluatorConstants.Boolean);
    switch (op.toString()) {
      case ">=":
        if (a instanceof Double || b instanceof Double) {
          result = a.doubleValue() >= b.doubleValue()?1:0;
        } else if (a instanceof Long && b instanceof Long) {
          result = a.longValue() >= b.longValue()?1:0;
        }
        break;
      case "<=":
        if (a instanceof Double || b instanceof Double) {
          result = a.doubleValue() <= b.doubleValue()?1:0;
        } else if (a instanceof Long && b instanceof Long) {
          result = a.longValue() <= b.longValue()?1:0;
        }
        break;
          case "==":
            if (a instanceof Double || b instanceof Double) {
              result = a.doubleValue() == b.doubleValue()?1:0;
            } else if (a instanceof Long && b instanceof Long) {
              result = a.longValue() == b.longValue()?1:0;
            }
            break;
          }
        }

Wanted to avoid many comparison operations.

I don't see a solution to covert string to literal I mean convert to working operator(single char operator or two char operator).

Can it be achieved without using any lib?


Solution

  • Convert each one of these into BiFunctions.

        if (a instanceof Double || b instanceof Double) {
          result = a.doubleValue() + b.doubleValue();
        } else if (a instanceof Long && b instanceof Long) {
          result = a.longValue() + b.longValue();
        }
    

    And put them into a map using the operator as key. Your big if-else-switch statement becomes:

    result = operatorMap.get(op.toString()).apply(a, b);