GeodeConverter { public static Object convertToActualType(String stringValue, String type) throws IllegalArgumentException { if (stringValue != null && type == null) { return stringValue; } else { Object o = null; if (type.startsWith("java.lang.")) type = type.substring(10); if ("string".equalsIgnoreCase(type)) return stringValue; if ("char".equalsIgnoreCase(type) || "Character".equals(type)) return stringValue.charAt(0); try { if ("byte".equalsIgnoreCase(type)) { o = Byte.parseByte(stringValue); return o; } else if ("short".equalsIgnoreCase(type)) { o = Short.parseShort(stringValue); return o; } else if ("int".equalsIgnoreCase(type) || "Integer".equals(type)) { o = Integer.parseInt(stringValue); return o; } else if ("long".equalsIgnoreCase(type)) { o = Long.parseLong(stringValue); return o; } else if ("double".equalsIgnoreCase(type)) { o = Double.parseDouble(stringValue); return o; } else if ("boolean".equalsIgnoreCase(type)) { o = Boolean.parseBoolean(stringValue); return o; } else if ("float".equalsIgnoreCase(type)) { o = Float.parseFloat(stringValue); return o; } return o; } catch (NumberFormatException e) { throw new IllegalArgumentException( "Failed to convert input key to " + type + " Msg : " + e.getMessage()); } } }  static Object convertToActualType(String stringValue, String type);  }
@Test public void coercesInt() { assertThat(GeodeConverter.convertToActualType("8", "int")).isEqualTo(8); assertThat(GeodeConverter.convertToActualType("8", Integer.class.getName())).isEqualTo(8); assertThat(GeodeConverter.convertToActualType("-8", "int")).isEqualTo(-8); assertThat(GeodeConverter.convertToActualType("-8", Integer.class.getName())).isEqualTo(-8); }
@Test public void coercesBoolean() { assertThat(GeodeConverter.convertToActualType("true", "boolean")).isEqualTo(true); assertThat(GeodeConverter.convertToActualType("true", Boolean.class.getName())).isEqualTo(true); assertThat(GeodeConverter.convertToActualType("false", "boolean")).isEqualTo(false); assertThat(GeodeConverter.convertToActualType("false", Boolean.class.getName())) .isEqualTo(false); }
@Test public void coercesString() { assertThat(GeodeConverter.convertToActualType("foo", "string")).isEqualTo("foo"); assertThat(GeodeConverter.convertToActualType("foo", String.class.getName())).isEqualTo("foo"); }
@Test public void coercesChar() { assertThat(GeodeConverter.convertToActualType("C", "char")).isEqualTo('C'); assertThat(GeodeConverter.convertToActualType("C", Character.class.getName())).isEqualTo('C'); }
@Test public void coercesByte() { assertThat(GeodeConverter.convertToActualType("5", "byte")).isEqualTo((byte) 5); assertThat(GeodeConverter.convertToActualType("5", Byte.class.getName())).isEqualTo((byte) 5); }
@Test public void coercesShort() { assertThat(GeodeConverter.convertToActualType("5", "short")).isEqualTo((short) 5); assertThat(GeodeConverter.convertToActualType("5", Short.class.getName())).isEqualTo((short) 5); }
@Test public void coercesLong() { assertThat(GeodeConverter.convertToActualType("555555555555555555", "long")) .isEqualTo(555555555555555555L); assertThat(GeodeConverter.convertToActualType("5", Long.class.getName())).isEqualTo((long) 5); }
@Test public void coercesFloat() { assertThat(GeodeConverter.convertToActualType("5.0", "float")).isEqualTo((float) 5); assertThat(GeodeConverter.convertToActualType("5.0", Float.class.getName())) .isEqualTo((float) 5); }
@Test public void coercesDouble() { assertThat(GeodeConverter.convertToActualType("5.0", "double")).isEqualTo((double) 5); assertThat(GeodeConverter.convertToActualType("5.0", Double.class.getName())) .isEqualTo((double) 5); }