protected String internalValueToString()

in odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/edm/EdmDecimal.java [152:193]


  protected <T> String internalValueToString(final T value, final EdmLiteralKind literalKind, final EdmFacets facets)
      throws EdmSimpleTypeException {
    String result;
    if (value instanceof Long || value instanceof Integer || value instanceof Short || value instanceof Byte
        || value instanceof BigInteger) {
      result = value.toString();
      final int digits = result.startsWith("-") ? result.length() - 1 : result.length();
      if (facets != null && facets.getPrecision() != null && facets.getPrecision() < digits) {
        throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_FACETS_NOT_MATCHED.addContent(value, facets));
      }

    } else if (value instanceof Double || value instanceof Float || value instanceof BigDecimal) {
      BigDecimal bigDecimalValue;
      try {
        if (value instanceof Double) {
          bigDecimalValue = BigDecimal.valueOf((Double) value);
        } else if (value instanceof Float) {
          bigDecimalValue = BigDecimal.valueOf((Float) value);
        } else {
          bigDecimalValue = (BigDecimal) value;
        }
      } catch (final NumberFormatException e) {
        throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_ILLEGAL_CONTENT.addContent(value), e);
      }

      final int digits = bigDecimalValue.scale() >= 0 ?
          Math.max(bigDecimalValue.precision(), bigDecimalValue.scale()) :
          bigDecimalValue.precision() - bigDecimalValue.scale();
      if (facets == null
          || (facets.getPrecision() == null || facets.getPrecision() >= digits)
          && (facets.getScale() == null || facets.getScale() >= bigDecimalValue.scale())) {
        result = bigDecimalValue.toPlainString();
      } else {
        throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_FACETS_NOT_MATCHED.addContent(value, facets));
      }

    } else {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(value.getClass()));
    }

    return result;
  }