@Test public void coerceNumberObjectToDoublePrimitiveWhenGivenNonNumberObjectShouldReturnZero() { JSONObject jsonObject = new JSONObject(); double expectedDouble = new Double(0); assertEquals(expectedDouble, ObjectCoercer.coerceNumberObjectToDoublePrimitive(jsonObject), 0); }
public static double coerceNumberObjectToDoublePrimitive(Object numberObject) { if (numberObject == null) { return 0; } if (numberObject instanceof Integer) { Integer number = (Integer) numberObject; return number.doubleValue(); } else if (numberObject instanceof Float) { Float number = (Float) numberObject; return number.doubleValue(); } else if (numberObject instanceof Long) { Long number = (Long) numberObject; return number.doubleValue(); } else { double finalValue; try { finalValue = (double) numberObject; } catch (ClassCastException e) { finalValue = 0d; } return finalValue; } }
ObjectCoercer { public static double coerceNumberObjectToDoublePrimitive(Object numberObject) { if (numberObject == null) { return 0; } if (numberObject instanceof Integer) { Integer number = (Integer) numberObject; return number.doubleValue(); } else if (numberObject instanceof Float) { Float number = (Float) numberObject; return number.doubleValue(); } else if (numberObject instanceof Long) { Long number = (Long) numberObject; return number.doubleValue(); } else { double finalValue; try { finalValue = (double) numberObject; } catch (ClassCastException e) { finalValue = 0d; } return finalValue; } } }
ObjectCoercer { public static double coerceNumberObjectToDoublePrimitive(Object numberObject) { if (numberObject == null) { return 0; } if (numberObject instanceof Integer) { Integer number = (Integer) numberObject; return number.doubleValue(); } else if (numberObject instanceof Float) { Float number = (Float) numberObject; return number.doubleValue(); } else if (numberObject instanceof Long) { Long number = (Long) numberObject; return number.doubleValue(); } else { double finalValue; try { finalValue = (double) numberObject; } catch (ClassCastException e) { finalValue = 0d; } return finalValue; } }  }
ObjectCoercer { public static double coerceNumberObjectToDoublePrimitive(Object numberObject) { if (numberObject == null) { return 0; } if (numberObject instanceof Integer) { Integer number = (Integer) numberObject; return number.doubleValue(); } else if (numberObject instanceof Float) { Float number = (Float) numberObject; return number.doubleValue(); } else if (numberObject instanceof Long) { Long number = (Long) numberObject; return number.doubleValue(); } else { double finalValue; try { finalValue = (double) numberObject; } catch (ClassCastException e) { finalValue = 0d; } return finalValue; } }  static double coerceNumberObjectToDoublePrimitive(Object numberObject); static float coerceNumberObjectToFloatPrimitive(Object numberObject); }
ObjectCoercer { public static double coerceNumberObjectToDoublePrimitive(Object numberObject) { if (numberObject == null) { return 0; } if (numberObject instanceof Integer) { Integer number = (Integer) numberObject; return number.doubleValue(); } else if (numberObject instanceof Float) { Float number = (Float) numberObject; return number.doubleValue(); } else if (numberObject instanceof Long) { Long number = (Long) numberObject; return number.doubleValue(); } else { double finalValue; try { finalValue = (double) numberObject; } catch (ClassCastException e) { finalValue = 0d; } return finalValue; } }  static double coerceNumberObjectToDoublePrimitive(Object numberObject); static float coerceNumberObjectToFloatPrimitive(Object numberObject);  }