NumericFormatter implements IValueFormatter { @Override public Value valueOf(String value, Type type, Map<String, Object> options) { VirtualMachine vm = type.virtualMachine(); char signature0 = type.signature().charAt(0); if (signature0 == LONG || signature0 == INT || signature0 == SHORT || signature0 == BYTE) { long number = parseNumber(value); if (signature0 == LONG) { return vm.mirrorOf(number); } else if (signature0 == INT) { return vm.mirrorOf((int) number); } else if (signature0 == SHORT) { return vm.mirrorOf((short) number); } else if (signature0 == BYTE) { return vm.mirrorOf((byte) number); } } else if (hasFraction(signature0)) { double doubleNumber = parseFloatDouble(value); if (signature0 == DOUBLE) { return vm.mirrorOf(doubleNumber); } else { return vm.mirrorOf((float) doubleNumber); } } throw new UnsupportedOperationException(String.format("%s is not a numeric type.", 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 testValueOf() throws Exception { Value i = this.getLocalValue("i"); Map<String, Object> options = formatter.getDefaultOptions(); Value newValue = formatter.valueOf(formatter.toString(i, options), i.type(), options); assertNotNull("NumericFormatter should be able to create integer by string.", newValue); assertTrue("Should create an integer value.", newValue instanceof IntegerValue); assertEquals("Should create an integer with right value.", "111", newValue.toString()); options.put(NUMERIC_FORMAT_OPTION, NumericFormatEnum.HEX); newValue = formatter.valueOf(formatter.toString(i, options), i.type(), options); assertNotNull("NumericFormatter should be able to create integer by string.", newValue); assertTrue("Should create an integer value.", newValue instanceof IntegerValue); assertEquals("Should create an integer with right value.", "111", newValue.toString()); options.put(NUMERIC_FORMAT_OPTION, NumericFormatEnum.OCT); newValue = formatter.valueOf(formatter.toString(i, options), i.type(), options); assertNotNull("NumericFormatter should be able to create integer by string.", newValue); assertTrue("Should create an integer value.", newValue instanceof IntegerValue); assertEquals("Should create an integer with right value.", "111", newValue.toString()); newValue = formatter.valueOf("-12121212", i.type(), options); assertNotNull("NumericFormatter should be able to create integer by string.", newValue); assertTrue("Should create an integer value.", newValue instanceof IntegerValue); assertEquals("Should create an integer with right value.", "-12121212", newValue.toString()); newValue = formatter.valueOf("0", i.type(), options); assertNotNull("NumericFormatter should be able to create integer by string.", newValue); assertTrue("Should create an integer value.", newValue instanceof IntegerValue); assertEquals("Should create an integer with right value.", "0", newValue.toString()); VirtualMachine vm = getVM(); newValue = formatter.valueOf("0", vm.mirrorOf(10.0f).type(), options); assertNotNull("NumericFormatter should be able to create float by string.", newValue); assertTrue("Should create an float value.", newValue instanceof FloatValue); assertEquals("Should create an float with right value.", "0.0", newValue.toString()); newValue = formatter.valueOf("10.0", vm.mirrorOf(10.0).type(), options); assertNotNull("NumericFormatter should be able to create double by string.", newValue); assertTrue("Should create an double value.", newValue instanceof DoubleValue); assertEquals("Should create an double with right value.", "10.0", newValue.toString()); newValue = formatter.valueOf("10", vm.mirrorOf((short)10).type(), options); assertNotNull("NumericFormatter should be able to create short by string.", newValue); assertTrue("Should create an short value.", newValue instanceof ShortValue); assertEquals("Should create an short with right value.", "10", newValue.toString()); newValue = formatter.valueOf("10", vm.mirrorOf(10L).type(), options); assertNotNull("NumericFormatter should be able to create long by string.", newValue); assertTrue("Should create an long value.", newValue instanceof LongValue); assertEquals("Should create an long with right value.", "10", newValue.toString()); newValue = formatter.valueOf("10", vm.mirrorOf((byte) 10).type(), options); assertNotNull("NumericFormatter should be able to create byte by string.", newValue); assertTrue("Should create an byte value.", newValue instanceof ByteValue); assertEquals("Should create an byte with right value.", "10", newValue.toString()); }
@Test(expected = UnsupportedOperationException.class) public void testValueOfNotSupported() { ObjectReference or = this.getObjectReference("Foo"); formatter.valueOf("new Foo()", or.referenceType(), new HashMap<>()); fail("Set value for object is not supported."); }
@Test(expected = NumberFormatException.class) public void testValueOfNotIllegalInput() { formatter.valueOf("new Foo()", getVM().mirrorOf(10.0).type(), new HashMap<>()); fail("Set value for object is not supported."); }