@Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(Number.class)).build(); NumberConverter converter = new NumberConverter(); converter.convert("", context); assertThat(context.getSupportedFormats()).contains("<double>, <long> (NumberConverter)", "POSITIVE_INFINITY (NumberConverter)", "NEGATIVE_INFINITY (NumberConverter)", "NAN (NumberConverter)"); }
@Override public Number convert(String value, ConversionContext ctx) { ctx.addSupportedFormats(getClass(), "<double>, <long>", "0x (hex)", "0X... (hex)", "POSITIVE_INFINITY", "NEGATIVE_INFINITY", "NAN"); if(value==null){ return null; } String trimmed = value.trim(); switch(trimmed.toUpperCase(Locale.ENGLISH)) { case "POSITIVE_INFINITY": return Double.POSITIVE_INFINITY; case "NEGATIVE_INFINITY": return Double.NEGATIVE_INFINITY; case "NAN": return Double.NaN; default: Long lVal = longConverter.convert(trimmed, ctx); if (lVal != null) { return lVal; } try { return new BigDecimal(trimmed); } catch(Exception e) { LOGGER.finest("Unparseable Number: " + trimmed); return null; } } }
NumberConverter implements PropertyConverter<Number> { @Override public Number convert(String value, ConversionContext ctx) { ctx.addSupportedFormats(getClass(), "<double>, <long>", "0x (hex)", "0X... (hex)", "POSITIVE_INFINITY", "NEGATIVE_INFINITY", "NAN"); if(value==null){ return null; } String trimmed = value.trim(); switch(trimmed.toUpperCase(Locale.ENGLISH)) { case "POSITIVE_INFINITY": return Double.POSITIVE_INFINITY; case "NEGATIVE_INFINITY": return Double.NEGATIVE_INFINITY; case "NAN": return Double.NaN; default: Long lVal = longConverter.convert(trimmed, ctx); if (lVal != null) { return lVal; } try { return new BigDecimal(trimmed); } catch(Exception e) { LOGGER.finest("Unparseable Number: " + trimmed); return null; } } } }
NumberConverter implements PropertyConverter<Number> { @Override public Number convert(String value, ConversionContext ctx) { ctx.addSupportedFormats(getClass(), "<double>, <long>", "0x (hex)", "0X... (hex)", "POSITIVE_INFINITY", "NEGATIVE_INFINITY", "NAN"); if(value==null){ return null; } String trimmed = value.trim(); switch(trimmed.toUpperCase(Locale.ENGLISH)) { case "POSITIVE_INFINITY": return Double.POSITIVE_INFINITY; case "NEGATIVE_INFINITY": return Double.NEGATIVE_INFINITY; case "NAN": return Double.NaN; default: Long lVal = longConverter.convert(trimmed, ctx); if (lVal != null) { return lVal; } try { return new BigDecimal(trimmed); } catch(Exception e) { LOGGER.finest("Unparseable Number: " + trimmed); return null; } } }  }
NumberConverter implements PropertyConverter<Number> { @Override public Number convert(String value, ConversionContext ctx) { ctx.addSupportedFormats(getClass(), "<double>, <long>", "0x (hex)", "0X... (hex)", "POSITIVE_INFINITY", "NEGATIVE_INFINITY", "NAN"); if(value==null){ return null; } String trimmed = value.trim(); switch(trimmed.toUpperCase(Locale.ENGLISH)) { case "POSITIVE_INFINITY": return Double.POSITIVE_INFINITY; case "NEGATIVE_INFINITY": return Double.NEGATIVE_INFINITY; case "NAN": return Double.NaN; default: Long lVal = longConverter.convert(trimmed, ctx); if (lVal != null) { return lVal; } try { return new BigDecimal(trimmed); } catch(Exception e) { LOGGER.finest("Unparseable Number: " + trimmed); return null; } } }  @Override Number convert(String value, ConversionContext ctx); @Override boolean equals(Object o); @Override int hashCode(); }
NumberConverter implements PropertyConverter<Number> { @Override public Number convert(String value, ConversionContext ctx) { ctx.addSupportedFormats(getClass(), "<double>, <long>", "0x (hex)", "0X... (hex)", "POSITIVE_INFINITY", "NEGATIVE_INFINITY", "NAN"); if(value==null){ return null; } String trimmed = value.trim(); switch(trimmed.toUpperCase(Locale.ENGLISH)) { case "POSITIVE_INFINITY": return Double.POSITIVE_INFINITY; case "NEGATIVE_INFINITY": return Double.NEGATIVE_INFINITY; case "NAN": return Double.NaN; default: Long lVal = longConverter.convert(trimmed, ctx); if (lVal != null) { return lVal; } try { return new BigDecimal(trimmed); } catch(Exception e) { LOGGER.finest("Unparseable Number: " + trimmed); return null; } } }  @Override Number convert(String value, ConversionContext ctx); @Override boolean equals(Object o); @Override int hashCode();  }