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