Esempio n. 1
0
  public static JsonValue eval(JsonValue value1, JsonValue value2, int op) {
    // changes here should be reflected in getSchema() below
    if (value1 == null || value2 == null) {
      return null;
    }
    switch (value1.getType()) {
      case LONG:
        switch (value2.getType()) {
          case LONG:
            return longEval((JsonNumber) value1, (JsonNumber) value2, op);
          case DOUBLE:
            return doubleEval((JsonNumber) value1, (JsonNumber) value2, op);
          case DECFLOAT:
            return decimalEval((JsonNumber) value1, (JsonNumber) value2, op);
        }
        break;
      case DOUBLE:
        switch (value2.getType()) {
          case LONG:
            return doubleEval((JsonNumber) value1, (JsonNumber) value2, op);
          case DOUBLE:
            return doubleEval((JsonNumber) value1, (JsonNumber) value2, op);
          case DECFLOAT:
            return decimalEval((JsonNumber) value1, (JsonNumber) value2, op);
        }
        break;
      case DECFLOAT:
        switch (value2.getType()) {
          case LONG:
            return decimalEval((JsonNumber) value1, (JsonNumber) value2, op);
          case DOUBLE:
            return decimalEval((JsonNumber) value1, (JsonNumber) value2, op);
          case DECFLOAT:
            return decimalEval((JsonNumber) value1, (JsonNumber) value2, op);
        }
        break;
      case STRING:
        if (value2.getType() == JsonType.STRING && op == PLUS) {
          JsonString text1 = (JsonString) value1;
          JsonString text2 = (JsonString) value2;
          MutableJsonString result = new MutableJsonString();
          result.ensureCapacity(text1.bytesLength() + text2.bytesLength());
          byte[] buf = result.get();
          text1.writeBytes(buf, 0, text1.bytesLength());
          text2.writeBytes(buf, text1.bytesLength(), text2.bytesLength());
          result.set(buf, text1.bytesLength() + text2.bytesLength());
          return result;
        }
        break;
    }

    // if we reach this point, we cannot apply the op
    throw new RuntimeException(
        "Operation "
            + OP_STR[op]
            + " not defined for input types "
            + value1.getType()
            + " and "
            + value2.getType());
  }
Esempio n. 2
0
 @Override
 public void run() {
   try {
     OutputStream os = proc.getOutputStream();
     // PrintStream out = new PrintStream(new BufferedOutputStream(os));
     OutputStream out = new BufferedOutputStream(os);
     for (JsonValue sv : iter) {
       // TODO:  force jstrings here? add i/o layer here? add serialize function that has i/o
       // layer?
       JsonString s = (JsonString) sv;
       if (s != null) {
         // out.println(s.toString()); // TODO: PrintStream swallows IOExceptions...
         out.write(s.getInternalBytes(), s.bytesOffset(), s.bytesLength());
         out.write('\n');
       }
     }
     out.close();
   } catch (Throwable e) {
     if (error == null) {
       error = e;
     }
     proc.destroy();
   }
 }