BigIntegerConverter implements PropertyConverter<BigInteger> { @Override public BigInteger convert(String value, ConversionContext ctx) { ctx.addSupportedFormats(getClass(), "[-]0X.. (hex)", "[-]0x... (hex)", "<bigint> -> new BigInteger(bigint)"); if(value==null){ return null; } String trimmed = value.trim(); if(trimmed.startsWith("0x") || trimmed.startsWith("0X")){ LOG.finest("Parsing Hex createValue to BigInteger: " + value); return new BigInteger(value.substring(2), 16); } else if(trimmed.startsWith("-0x") || trimmed.startsWith("-0X")){ LOG.finest("Parsing Hex createValue to BigInteger: " + value); return new BigInteger('-' + value.substring(3), 16); } try{ return new BigInteger(trimmed); } catch(Exception e){ LOG.log(Level.FINEST, "Failed to parse BigInteger from: " + value, e); return null; } }  @Override BigInteger convert(String value, ConversionContext ctx); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void converterHandlesNullValueCorrectly() throws Exception { ConversionContext context = mock(ConversionContext.class); BigIntegerConverter converter = new BigIntegerConverter(); BigInteger value = converter.convert("", context); assertThat(value).isNull(); }
@Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(BigInteger.class)).build(); BigIntegerConverter converter = new BigIntegerConverter(); BigInteger value = converter.convert("", context); assertThat(value).isNull(); assertThat(context.getSupportedFormats()).contains("<bigint> -> new BigInteger(bigint) (BigIntegerConverter)"); }