MapValueGetter { public static <T> T getAsType(Map<String, Object> map, String key, Class<T> objectType) { com.workday.autoparse.json.utils.Preconditions.checkNotNull(map, "map"); com.workday.autoparse.json.utils.Preconditions.checkNotNull(key, "key"); com.workday.autoparse.json.utils.Preconditions.checkNotNull(objectType, "objectType"); Object value = map.get(key); if (value == null) { return null; } if (objectType.isInstance(value)) { @SuppressWarnings("unchecked") T castedValue = (T) value; return castedValue; } throw new WrongTypeException(key, objectType, value); } private  MapValueGetter(); static T getAsType(Map<String, Object> map, String key, Class<T> objectType); static BigDecimal getAsBigDecimal(Map<String, Object> map, String key); static BigInteger getAsBigInteger(Map<String, Object> map, String key); static boolean getAsBoolean(Map<String, Object> map, String key); static byte getAsByte(Map<String, Object> map, String key); static char getAsChar(Map<String, Object> map, String key); static double getAsDouble(Map<String, Object> map, String key); static float getAsFloat(Map<String, Object> map, String key); static int getAsInt(Map<String, Object> map, String key); static long getAsLong(Map<String, Object> map, String key); static short getAsShort(Map<String, Object> map, String key); static String getAsString(Map<String, Object> map, String key);  }
@Test public void testTypeAsType() { AtomicBoolean expected = new AtomicBoolean(true); map.put("value", expected); AtomicBoolean actual = MapValueGetter.getAsType(map, "value", AtomicBoolean.class); assertEquals(expected, actual); }
@Test public void testTypeFromNull() { AtomicBoolean actual = MapValueGetter.getAsType(map, "value", AtomicBoolean.class); AtomicBoolean expected = null; assertEquals(expected, actual); }
@Test(expected = WrongTypeException.class) public void testTypeWrongType() { map.put("value", true); MapValueGetter.getAsType(map, "value", AtomicBoolean.class); }