private static ExpressionWriter write()

in linq4j/src/main/java/org/apache/calcite/linq4j/tree/ConstantExpression.java [86:238]


  private static ExpressionWriter write(ExpressionWriter writer,
      final Object value, @Nullable Type type) {
    if (value == null) {
      return writer.append("null");
    }
    if (type == null) {
      type = value.getClass();
      type = Primitive.unbox(type);
    }
    if (value instanceof String) {
      escapeString(writer.getBuf(), (String) value);
      return writer;
    }
    final Primitive primitive = Primitive.of(type);
    final BigDecimal bigDecimal;
    if (primitive != null) {
      switch (primitive) {
      case BYTE:
        return writer.append("(byte)").append(((Byte) value).intValue());
      case CHAR:
        return writer.append("(char)").append((int) (Character) value);
      case SHORT:
        return writer.append("(short)").append(((Short) value).intValue());
      case LONG:
        return writer.append(value).append("L");
      case FLOAT:
        if (value instanceof BigDecimal) {
          bigDecimal = (BigDecimal) value;
        } else {
          float f = (Float) value;
          if (f == Float.POSITIVE_INFINITY) {
            return writer.append("Float.POSITIVE_INFINITY");
          } else if (f == Float.NEGATIVE_INFINITY) {
            return writer.append("Float.NEGATIVE_INFINITY");
          } else if (Float.isNaN(f)) {
            return writer.append("Float.NaN");
          }
          bigDecimal = BigDecimal.valueOf(f);
        }
        if (bigDecimal.precision() > 6) {
          return writer.append("Float.intBitsToFloat(")
              .append(Float.floatToIntBits(bigDecimal.floatValue()))
              .append(")");
        }
        return writer.append(value).append("F");
      case DOUBLE:
        if (value instanceof BigDecimal) {
          bigDecimal = (BigDecimal) value;
        } else {
          double d = (Double) value;
          if (d == Double.POSITIVE_INFINITY) {
            return writer.append("Double.POSITIVE_INFINITY");
          } else if (d == Double.NEGATIVE_INFINITY) {
            return writer.append("Double.NEGATIVE_INFINITY");
          } else if (Double.isNaN(d)) {
            return writer.append("Double.NaN");
          }
          bigDecimal = BigDecimal.valueOf(d);
        }
        if (bigDecimal.precision() > 10) {
          return writer.append("Double.longBitsToDouble(")
              .append(Double.doubleToLongBits(bigDecimal.doubleValue()))
              .append("L)");
        }
        return writer.append(value).append("D");
      default:
        return writer.append(value);
      }
    }
    final Primitive primitive2 = Primitive.ofBox(type);
    if (primitive2 != null) {
      writer.append(primitive2.boxName + ".valueOf(");
      write(writer, value, primitive2.primitiveClass);
      return writer.append(")");
    }
    Primitive primitive3 = Primitive.ofBox(value.getClass());
    if (Object.class.equals(type) && primitive3 != null) {
      return write(writer, value, primitive3.primitiveClass);
    }
    if (value instanceof Enum) {
      return writer.append(((Enum) value).getDeclaringClass())
          .append('.')
          .append(((Enum) value).name());
    }
    if (value instanceof BigDecimal) {
      bigDecimal = ((BigDecimal) value).stripTrailingZeros();
      try {
        final int scale = bigDecimal.scale();
        final long exact = bigDecimal.scaleByPowerOfTen(scale).longValueExact();
        writer.append("java.math.BigDecimal.valueOf(").append(exact).append("L");
        if (scale != 0) {
          writer.append(", ").append(scale);
        }
        return writer.append(")");
      } catch (ArithmeticException e) {
        return writer.append("new java.math.BigDecimal(\"")
            .append(bigDecimal.toString()).append("\")");
      }
    }
    if (value instanceof BigInteger) {
      BigInteger bigInteger = (BigInteger) value;
      return writer.append("new java.math.BigInteger(\"")
          .append(bigInteger.toString()).append("\")");
    }
    if (value instanceof Class) {
      Class clazz = (Class) value;
      return writer.append(clazz.getCanonicalName()).append(".class");
    }
    if (value instanceof Types.RecordType) {
      final Types.RecordType recordType = (Types.RecordType) value;
      return writer.append(recordType.getName()).append(".class");
    }
    if (value.getClass().isArray()) {
      writer.append("new ").append(requireNonNull(value.getClass().getComponentType()));
      list(writer, Primitive.asList(value), "[] {\n", ",\n", "}");
      return writer;
    }
    if (value instanceof List) {
      if (((List) value).isEmpty()) {
        writer.append("java.util.Collections.EMPTY_LIST");
        return writer;
      }
      list(writer, (List) value, "java.util.Arrays.asList(", ",\n", ")");
      return writer;
    }
    if (value instanceof Map) {
      return writeMap(writer, (Map) value);
    }
    if (value instanceof Set) {
      return writeSet(writer, (Set) value);
    }
    if (value instanceof Charset) {
      writer.append("java.nio.charset.Charset.forName(\"");
      writer.append(value);
      writer.append("\")");
      return writer;
    }

    final Field[] classFields = getClassFields(value.getClass());
    Constructor constructor = matchingConstructor(value, classFields);
    if (constructor != null) {
      writer.append("new ").append(value.getClass());
      list(writer,
          Arrays.stream(classFields)
              // <@Nullable Object> is needed for CheckerFramework
              .<@Nullable Object>map(field -> getFieldValue(value, field))
              .collect(Collectors.toList()),
          "(\n", ",\n", ")");
      return writer;
    }

    return writer.append(value);
  }