예제 #1
0
 private static Object doPrimWrapperArithmetic(
     final Number val1,
     final int operation,
     final Number val2,
     boolean iNumber,
     int returnTarget) {
   switch (operation) {
     case ADD:
       return toType(val1.doubleValue() + val2.doubleValue(), returnTarget);
     case DIV:
       return toType(val1.doubleValue() / val2.doubleValue(), returnTarget);
     case SUB:
       return toType(val1.doubleValue() - val2.doubleValue(), returnTarget);
     case MULT:
       return toType(val1.doubleValue() * val2.doubleValue(), returnTarget);
     case POWER:
       return toType(Math.pow(val1.doubleValue(), val2.doubleValue()), returnTarget);
     case MOD:
       return toType(val1.doubleValue() % val2.doubleValue(), returnTarget);
     case GTHAN:
       return val1.doubleValue() > val2.doubleValue() ? Boolean.TRUE : Boolean.FALSE;
     case GETHAN:
       return val1.doubleValue() >= val2.doubleValue() ? Boolean.TRUE : Boolean.FALSE;
     case LTHAN:
       return val1.doubleValue() < val2.doubleValue() ? Boolean.TRUE : Boolean.FALSE;
     case LETHAN:
       return val1.doubleValue() <= val2.doubleValue() ? Boolean.TRUE : Boolean.FALSE;
     case EQUAL:
       return val1.doubleValue() == val2.doubleValue() ? Boolean.TRUE : Boolean.FALSE;
     case NEQUAL:
       return val1.doubleValue() != val2.doubleValue() ? Boolean.TRUE : Boolean.FALSE;
   }
   return null;
 }
예제 #2
0
  private static Object doOperationsSameType(int type1, Object val1, int operation, Object val2) {
    switch (type1) {
      case DataTypes.COLLECTION:
        switch (operation) {
          case ADD:
            List list = new ArrayList((Collection) val1);
            list.addAll((Collection) val2);
            return list;

          case EQUAL:
            return val1.equals(val2);

          case NEQUAL:
            return !val1.equals(val2);

          default:
            throw new UnsupportedOperationException("illegal operation on Collection type");
        }

      case DataTypes.INTEGER:
      case DataTypes.W_INTEGER:
        switch (operation) {
          case ADD:
            return ((Integer) val1) + ((Integer) val2);
          case SUB:
            return ((Integer) val1) - ((Integer) val2);
          case DIV:
            return ((Integer) val1).doubleValue() / ((Integer) val2).doubleValue();
          case MULT:
            return ((Integer) val1) * ((Integer) val2);
          case POWER:
            double d = Math.pow((Integer) val1, (Integer) val2);
            if (d > Integer.MAX_VALUE) return d;
            else return (int) d;
          case MOD:
            return ((Integer) val1) % ((Integer) val2);
          case GTHAN:
            return ((Integer) val1) > ((Integer) val2) ? Boolean.TRUE : Boolean.FALSE;
          case GETHAN:
            return ((Integer) val1) >= ((Integer) val2) ? Boolean.TRUE : Boolean.FALSE;
          case LTHAN:
            return ((Integer) val1) < ((Integer) val2) ? Boolean.TRUE : Boolean.FALSE;
          case LETHAN:
            return ((Integer) val1) <= ((Integer) val2) ? Boolean.TRUE : Boolean.FALSE;
          case EQUAL:
            return ((Integer) val1).intValue() == ((Integer) val2).intValue()
                ? Boolean.TRUE
                : Boolean.FALSE;
          case NEQUAL:
            return ((Integer) val1).intValue() != ((Integer) val2).intValue()
                ? Boolean.TRUE
                : Boolean.FALSE;
          case BW_AND:
            if (val2 instanceof Long) return (Integer) val1 & (Long) val2;
            return (Integer) val1 & (Integer) val2;
          case BW_OR:
            if (val2 instanceof Long) return (Integer) val1 | (Long) val2;
            return (Integer) val1 | (Integer) val2;
          case BW_SHIFT_LEFT:
            if (val2 instanceof Long) return (Integer) val1 << (Long) val2;
            return (Integer) val1 << (Integer) val2;
          case BW_SHIFT_RIGHT:
            if (val2 instanceof Long) return (Integer) val1 >> (Long) val2;
            return (Integer) val1 >> (Integer) val2;
          case BW_USHIFT_RIGHT:
            if (val2 instanceof Long) return (Integer) val1 >>> (Long) val2;
            return (Integer) val1 >>> (Integer) val2;
          case BW_XOR:
            if (val2 instanceof Long) return (Integer) val1 ^ (Long) val2;
            return (Integer) val1 ^ (Integer) val2;
        }

      case DataTypes.SHORT:
      case DataTypes.W_SHORT:
        switch (operation) {
          case ADD:
            return ((Short) val1) + ((Short) val2);
          case SUB:
            return ((Short) val1) - ((Short) val2);
          case DIV:
            return ((Short) val1).doubleValue() / ((Short) val2).doubleValue();
          case MULT:
            return ((Short) val1) * ((Short) val2);
          case POWER:
            double d = Math.pow((Short) val1, (Short) val2);
            if (d > Short.MAX_VALUE) return d;
            else return (short) d;
          case MOD:
            return ((Short) val1) % ((Short) val2);
          case GTHAN:
            return ((Short) val1) > ((Short) val2) ? Boolean.TRUE : Boolean.FALSE;
          case GETHAN:
            return ((Short) val1) >= ((Short) val2) ? Boolean.TRUE : Boolean.FALSE;
          case LTHAN:
            return ((Short) val1) < ((Short) val2) ? Boolean.TRUE : Boolean.FALSE;
          case LETHAN:
            return ((Short) val1) <= ((Short) val2) ? Boolean.TRUE : Boolean.FALSE;
          case EQUAL:
            return ((Short) val1).shortValue() == ((Short) val2).shortValue()
                ? Boolean.TRUE
                : Boolean.FALSE;
          case NEQUAL:
            return ((Short) val1).shortValue() != ((Short) val2).shortValue()
                ? Boolean.TRUE
                : Boolean.FALSE;
          case BW_AND:
            return (Short) val1 & (Short) val2;
          case BW_OR:
            return (Short) val1 | (Short) val2;
          case BW_SHIFT_LEFT:
            return (Short) val1 << (Short) val2;
          case BW_SHIFT_RIGHT:
            return (Short) val1 >> (Short) val2;
          case BW_USHIFT_RIGHT:
            return (Short) val1 >>> (Short) val2;
          case BW_XOR:
            return (Short) val1 ^ (Short) val2;
        }

      case DataTypes.LONG:
      case DataTypes.W_LONG:
        switch (operation) {
          case ADD:
            return ((Long) val1) + ((Long) val2);
          case SUB:
            return ((Long) val1) - ((Long) val2);
          case DIV:
            return ((Long) val1).doubleValue() / ((Long) val2).doubleValue();
          case MULT:
            return ((Long) val1) * ((Long) val2);
          case POWER:
            double d = Math.pow((Long) val1, (Long) val2);
            if (d > Long.MAX_VALUE) return d;
            else return (long) d;
          case MOD:
            return ((Long) val1) % ((Long) val2);
          case GTHAN:
            return ((Long) val1) > ((Long) val2) ? Boolean.TRUE : Boolean.FALSE;
          case GETHAN:
            return ((Long) val1) >= ((Long) val2) ? Boolean.TRUE : Boolean.FALSE;
          case LTHAN:
            return ((Long) val1) < ((Long) val2) ? Boolean.TRUE : Boolean.FALSE;
          case LETHAN:
            return ((Long) val1) <= ((Long) val2) ? Boolean.TRUE : Boolean.FALSE;
          case EQUAL:
            return ((Long) val1).longValue() == ((Long) val2).longValue()
                ? Boolean.TRUE
                : Boolean.FALSE;
          case NEQUAL:
            return ((Long) val1).longValue() != ((Long) val2).longValue()
                ? Boolean.TRUE
                : Boolean.FALSE;
          case BW_AND:
            if (val2 instanceof Integer) return (Long) val1 & (Integer) val2;
            return (Long) val1 & (Long) val2;
          case BW_OR:
            if (val2 instanceof Integer) return (Long) val1 | (Integer) val2;
            return (Long) val1 | (Long) val2;
          case BW_SHIFT_LEFT:
            if (val2 instanceof Integer) return (Long) val1 << (Integer) val2;
            return (Long) val1 << (Long) val2;
          case BW_USHIFT_LEFT:
            throw new UnsupportedOperationException("unsigned left-shift not supported");
          case BW_SHIFT_RIGHT:
            if (val2 instanceof Integer) return (Long) val1 >> (Integer) val2;
            return (Long) val1 >> (Long) val2;
          case BW_USHIFT_RIGHT:
            if (val2 instanceof Integer) return (Long) val1 >>> (Integer) val2;
            return (Long) val1 >>> (Long) val2;
          case BW_XOR:
            if (val2 instanceof Integer) return (Long) val1 ^ (Integer) val2;
            return (Long) val1 ^ (Long) val2;
        }

      case DataTypes.UNIT:
        val2 = ((Unit) val1).convertFrom(val2);
        val1 = ((Unit) val1).getValue();

      case DataTypes.DOUBLE:
      case DataTypes.W_DOUBLE:
        switch (operation) {
          case ADD:
            return ((Double) val1) + ((Double) val2);
          case SUB:
            return ((Double) val1) - ((Double) val2);
          case DIV:
            return ((Double) val1) / ((Double) val2);
          case MULT:
            return ((Double) val1) * ((Double) val2);
          case POWER:
            return Math.pow((Double) val1, (Double) val2);
          case MOD:
            return ((Double) val1) % ((Double) val2);
          case GTHAN:
            return ((Double) val1) > ((Double) val2) ? Boolean.TRUE : Boolean.FALSE;
          case GETHAN:
            return ((Double) val1) >= ((Double) val2) ? Boolean.TRUE : Boolean.FALSE;
          case LTHAN:
            return ((Double) val1) < ((Double) val2) ? Boolean.TRUE : Boolean.FALSE;
          case LETHAN:
            return ((Double) val1) <= ((Double) val2) ? Boolean.TRUE : Boolean.FALSE;
          case EQUAL:
            return ((Double) val1).doubleValue() == ((Double) val2).doubleValue()
                ? Boolean.TRUE
                : Boolean.FALSE;
          case NEQUAL:
            return ((Double) val1).doubleValue() != ((Double) val2).doubleValue()
                ? Boolean.TRUE
                : Boolean.FALSE;
          case BW_AND:
          case BW_OR:
          case BW_SHIFT_LEFT:
          case BW_SHIFT_RIGHT:
          case BW_USHIFT_RIGHT:
          case BW_XOR:
            throw new RuntimeException("bitwise operation on a non-fixed-point number.");
        }

      case DataTypes.FLOAT:
      case DataTypes.W_FLOAT:
        switch (operation) {
          case ADD:
            return ((Float) val1) + ((Float) val2);
          case SUB:
            return ((Float) val1) - ((Float) val2);
          case DIV:
            return ((Float) val1).doubleValue() / ((Float) val2).doubleValue();
          case MULT:
            return ((Float) val1) * ((Float) val2);
          case POWER:
            return narrowType(
                new InternalNumber((Float) val1, MATH_CONTEXT)
                    .pow(new InternalNumber((Float) val2).intValue(), MATH_CONTEXT),
                -1);
          case MOD:
            return ((Float) val1) % ((Float) val2);
          case GTHAN:
            return ((Float) val1) > ((Float) val2) ? Boolean.TRUE : Boolean.FALSE;
          case GETHAN:
            return ((Float) val1) >= ((Float) val2) ? Boolean.TRUE : Boolean.FALSE;
          case LTHAN:
            return ((Float) val1) < ((Float) val2) ? Boolean.TRUE : Boolean.FALSE;
          case LETHAN:
            return ((Float) val1) <= ((Float) val2) ? Boolean.TRUE : Boolean.FALSE;
          case EQUAL:
            return ((Float) val1).floatValue() == ((Float) val2).floatValue()
                ? Boolean.TRUE
                : Boolean.FALSE;
          case NEQUAL:
            return ((Float) val1).floatValue() != ((Float) val2).floatValue()
                ? Boolean.TRUE
                : Boolean.FALSE;
          case BW_AND:
          case BW_OR:
          case BW_SHIFT_LEFT:
          case BW_SHIFT_RIGHT:
          case BW_USHIFT_RIGHT:
          case BW_XOR:
            throw new RuntimeException("bitwise operation on a non-fixed-point number.");
        }

      case DataTypes.BIG_INTEGER:
        switch (operation) {
          case ADD:
            return ((BigInteger) val1).add(((BigInteger) val2));
          case SUB:
            return ((BigInteger) val1).subtract(((BigInteger) val2));
          case DIV:
            return ((BigInteger) val1).divide(((BigInteger) val2));
          case MULT:
            return ((BigInteger) val1).multiply(((BigInteger) val2));
          case POWER:
            return ((BigInteger) val1).pow(((BigInteger) val2).intValue());
          case MOD:
            return ((BigInteger) val1).remainder(((BigInteger) val2));
          case GTHAN:
            return ((BigInteger) val1).compareTo(((BigInteger) val2)) == 1
                ? Boolean.TRUE
                : Boolean.FALSE;
          case GETHAN:
            return ((BigInteger) val1).compareTo(((BigInteger) val2)) >= 0
                ? Boolean.TRUE
                : Boolean.FALSE;
          case LTHAN:
            return ((BigInteger) val1).compareTo(((BigInteger) val2)) == -1
                ? Boolean.TRUE
                : Boolean.FALSE;
          case LETHAN:
            return ((BigInteger) val1).compareTo(((BigInteger) val2)) <= 0
                ? Boolean.TRUE
                : Boolean.FALSE;
          case EQUAL:
            return ((BigInteger) val1).compareTo(((BigInteger) val2)) == 0
                ? Boolean.TRUE
                : Boolean.FALSE;
          case NEQUAL:
            return ((BigInteger) val1).compareTo(((BigInteger) val2)) != 0
                ? Boolean.TRUE
                : Boolean.FALSE;
          case BW_AND:
          case BW_OR:
          case BW_SHIFT_LEFT:
          case BW_SHIFT_RIGHT:
          case BW_USHIFT_RIGHT:
          case BW_XOR:
            throw new RuntimeException(
                "bitwise operation on a number greater than 32-bits not possible");
        }

      default:
        switch (operation) {
          case EQUAL:
            return safeEquals(val2, val1);
          case NEQUAL:
            return safeNotEquals(val2, val1);
          case ADD:
            return valueOf(val1) + valueOf(val2);
        }
    }
    return null;
  }