NumericFormatter implements IValueFormatter { @Override public String toString(Object obj, Map<String, Object> options) { Value value = (Value) obj; char signature0 = value.type().signature().charAt(0); if (signature0 == LONG || signature0 == INT || signature0 == SHORT || signature0 == BYTE) { return formatNumber(Long.parseLong(value.toString()), options); } else if (hasFraction(signature0)) { return formatFloatDouble(Double.parseDouble(value.toString()), options); } throw new UnsupportedOperationException(String.format("%s is not a numeric type.", value.type().name())); }  @Override String toString(Object obj, Map<String, Object> options); @Override Value valueOf(String value, Type type, Map<String, Object> options); @Override boolean acceptType(Type type, Map<String, Object> options); @Override Map<String, Object> getDefaultOptions(); static final String NUMERIC_FORMAT_OPTION; static final String NUMERIC_PRECISION_OPTION; }
@Test public void testToString() throws Exception { Value i = this.getLocalValue("i"); assertEquals("NumericFormatter should be able to format int correctly.", "111", formatter.toString(i, new HashMap<>())); VirtualMachine vm = getVM(); assertEquals("NumericFormatter should be able to format double correctly.", "111.000000", formatter.toString(vm.mirrorOf(111.0), new HashMap<>())); assertEquals("NumericFormatter should be able to format float correctly.", "111.000000", formatter.toString(vm.mirrorOf(111.0f), new HashMap<>())); Map<String, Object> options = formatter.getDefaultOptions(); options.put(NUMERIC_PRECISION_OPTION, 1); assertEquals("NumericFormatter should be able to format double correctly.", "111.0", formatter.toString(vm.mirrorOf(111.0), options)); assertEquals("NumericFormatter should be able to format float correctly.", "111.0", formatter.toString(vm.mirrorOf(111.0f), options)); options.put(NUMERIC_PRECISION_OPTION, -1); assertEquals("NumericFormatter should be able to format double correctly.", "111.000000", formatter.toString(vm.mirrorOf(111.0), options)); }
@Test(expected = UnsupportedOperationException.class) public void testToStringNotSupported() { ObjectReference or = this.getObjectReference("Foo"); formatter.toString(or, new HashMap<>()); fail("format object should not be supported by numeric formatter."); }