public boolean isBigDecimal(final String path) { return isBigDecimal(getNode(path)); }
public boolean isBigDecimal() { return isBigDecimal(asJsonNode()); }
private BigDecimal getBigDecimal(final String path, final String format, final JsonNode node) { if (representsNull(node)) { return null; } final String requiredType = "a bigdecimal"; if(!isBigDecimal(node)) { throw new IllegalArgumentException(formatExMsg(path, "is not " + requiredType)); } checkValue(path, node, requiredType); final BigDecimal bigDecimal = getBigDecimal(path, node); if(format != null) { final Matcher matcher = FORMAT_BIG_DECIMAL.matcher(format); if(matcher.matches()) { final int precision = Integer.parseInt(matcher.group(1)); final int scale = Integer.parseInt(matcher.group(2)); final BigDecimal maxAllowed = BigDecimal.TEN.pow(precision-scale); if(bigDecimal.compareTo(maxAllowed) > 0) { throw new IllegalArgumentException(String.format("Value '%s' larger than that allowed by format '%s'", bigDecimal, format)); } return bigDecimal.setScale(scale, BigDecimal.ROUND_HALF_EVEN); } } return bigDecimal; }
@Override public ObjectAdapter asAdapter(JsonRepresentation repr, String format) { if (repr.isString()) { return adapterFor(new BigDecimal(repr.asString())); } if (repr.isBigDecimal()) { return adapterFor(repr.asBigDecimal(format)); } if (repr.isBigInteger()) { return adapterFor(new BigDecimal(repr.asBigInteger())); } if (repr.isDouble()) { return adapterFor(BigDecimal.valueOf(repr.asDouble())); } if (repr.isLong()) { return adapterFor(BigDecimal.valueOf(repr.asLong())); } if (repr.isInt()) { return adapterFor(BigDecimal.valueOf(repr.asInt())); } return null; } @Override
@Override public ObjectAdapter asAdapter(JsonRepresentation repr, String format) { if (repr.isString()) { return adapterFor(new BigDecimal(repr.asString())); } if (repr.isBigDecimal()) { return adapterFor(repr.asBigDecimal(format)); } if (repr.isBigInteger()) { return adapterFor(new BigDecimal(repr.asBigInteger())); } if (repr.isDouble()) { return adapterFor(BigDecimal.valueOf(repr.asDouble())); } if (repr.isLong()) { return adapterFor(BigDecimal.valueOf(repr.asLong())); } if (repr.isInt()) { return adapterFor(BigDecimal.valueOf(repr.asInt())); } return null; } @Override
@Override public ObjectAdapter asAdapter(JsonRepresentation repr, String format) { if (repr.isDouble()) { return adapterFor(repr.asDouble()); } if (repr.isLong()) { return adapterFor((double)repr.asLong()); } if (repr.isInt()) { return adapterFor((double)repr.asInt()); } if (repr.isBigInteger()) { return adapterFor(repr.asBigInteger().doubleValue()); } if (repr.isBigDecimal()) { return adapterFor(repr.asBigDecimal().doubleValue()); } if (repr.isNumber()) { return adapterFor(repr.asNumber().doubleValue()); } return null; } @Override
@Override public ObjectAdapter asAdapter(JsonRepresentation repr, String format) { if (repr.isDouble()) { return adapterFor(repr.asDouble()); } if (repr.isLong()) { return adapterFor((double)repr.asLong()); } if (repr.isInt()) { return adapterFor((double)repr.asInt()); } if (repr.isBigInteger()) { return adapterFor(repr.asBigInteger().doubleValue()); } if (repr.isBigDecimal()) { return adapterFor(repr.asBigDecimal().doubleValue()); } if (repr.isNumber()) { return adapterFor(repr.asNumber().doubleValue()); } return null; } @Override
@Test public void whenBigDecimal() throws Exception { allowingObjectSpecToReturnSpecIdFor(BigDecimal.class); allowingObjectAdapterToReturn(new BigDecimal("12345678901234567890.1234")); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, "big-decimal(27,4)", false); assertThat(representation.isString("value"), is(true)); assertThat(representation.isBigDecimal("value"), is(true)); assertThat(representation.getBigDecimal("value"), is(new BigDecimal("12345678901234567890.1234"))); assertThat(representation.getString("format"), is("big-decimal(27,4)")); assertThat(representation.getString("extensions.x-isis-format"), is("javamathbigdecimal")); }
@Test public void whenBigDecimal() throws Exception { allowingObjectSpecToReturnSpecIdFor(BigDecimal.class); allowingObjectAdapterToReturn(new BigDecimal("12345678901234567890.1234")); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, "big-decimal(27,4)"); assertThat(representation.isString("value"), is(true)); assertThat(representation.isBigDecimal("value"), is(true)); assertThat(representation.getBigDecimal("value"), is(new BigDecimal("12345678901234567890.1234"))); assertThat(representation.getString("format"), is("big-decimal(27,4)")); assertThat(representation.getString("extensions.x-isis-format"), is("javamathbigdecimal")); }
if (representation.isBigDecimal()) { final String argStr = "" + representation.asBigDecimal(); return encodableFacet.fromEncodedString(argStr);