} else {
// Now only "normal" numbers remain.
final Matcher matcher = PATTERN.matcher(value);
if (!matcher.matches()
|| (literalKind == EdmLiteralKind.URI) == (matcher.group(1) == null)) {
throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
}
if (literalKind == EdmLiteralKind.URI) {
valueString = value.substring(0, value.length() - 1);
}
// The number format is checked above, so we don't have to catch NumberFormatException.
result = Float.valueOf(valueString);
// "Real" infinite values have been treated already above, so we can throw an exception
// if the conversion to a float results in an infinite value.
if (result.isInfinite()) {
throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
}
}
if (returnType.isAssignableFrom(Float.class)) {
return returnType.cast(result);
} else if (returnType.isAssignableFrom(Double.class)) {
if (result.isInfinite() || result.isNaN()) {
return returnType.cast(result.doubleValue());
} else {
return returnType.cast(Double.valueOf(valueString));
}
} else if (result.isInfinite() || result.isNaN()) {
throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_UNCONVERTIBLE_TO_VALUE_TYPE.addContent(value,
returnType));
} else {
try {
final BigDecimal valueBigDecimal = new BigDecimal(valueString);
if (returnType.isAssignableFrom(BigDecimal.class)) {
return returnType.cast(valueBigDecimal);
} else if (returnType.isAssignableFrom(Long.class)) {
return returnType.cast(valueBigDecimal.longValueExact());
} else if (returnType.isAssignableFrom(Integer.class)) {
return returnType.cast(valueBigDecimal.intValueExact());
} else if (returnType.isAssignableFrom(Short.class)) {
return returnType.cast(valueBigDecimal.shortValueExact());
} else if (returnType.isAssignableFrom(Byte.class)) {
return returnType.cast(valueBigDecimal.byteValueExact());
} else {
throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(returnType));
}
} catch (final ArithmeticException e) {
throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_UNCONVERTIBLE_TO_VALUE_TYPE.addContent(value,
returnType), e);
}
}
}