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);  }
@Test public void coerceNumberObjectToFloatPrimitiveWhenGivenDoubleObjectShouldReturnDoublePrimitive() { float expectedFloat = 8.394F; Double doubleObject = new Double(expectedFloat); assertEquals(expectedFloat, ObjectCoercer.coerceNumberObjectToFloatPrimitive(doubleObject), 0); }
@Test public void coerceNumberObjectToFloatPrimitiveWhenGivenFloatObjectShouldReturnDoublePrimitive() { float expectedFloat = 8.394F; Float floatObject = new Float(expectedFloat); assertEquals(expectedFloat, ObjectCoercer.coerceNumberObjectToFloatPrimitive(floatObject), 0); }
@Test public void coerceNumberObjectToFloatPrimitiveWhenGivenIntegerObjectShouldReturnDoublePrimitive() { float expectedFloat = 8; Integer integerObject = new Integer(8); assertEquals(expectedFloat, ObjectCoercer.coerceNumberObjectToFloatPrimitive(integerObject), 0); }
@Test public void coerceNumberObjectToFloatPrimitiveWhenGivenLongObjectShouldReturnDoublePrimitive() { float expectedFloat = 9882398; Long longObject = new Long(9882398); assertEquals(expectedFloat, ObjectCoercer.coerceNumberObjectToFloatPrimitive(longObject), 0); }
@Test public void coerceNumberObjectToFloatPrimitiveWhenGivenDoublePrimitiveShouldReturnDoublePrimitive() { float expectedFloat = 8923.23099F; double doublePrimitive = new Double(8923.23099F); assertEquals(expectedFloat, ObjectCoercer.coerceNumberObjectToFloatPrimitive(expectedFloat), 0); }
@Test public void coerceNumberObjectToFloatPrimitiveWhenGivenIntegerPrimitiveShouldReturnDoublePrimitive() { float expectedFloat = 898232; int intPrimtive = 898232; assertEquals(expectedFloat, ObjectCoercer.coerceNumberObjectToFloatPrimitive(intPrimtive), 0); }
@Test public void coerceNumberObjectToFloatPrimitiveWhenGivenLongPrimitiveShouldReturnFloatPrimitive() { float expectedFloat = 898090L; long longPrimitive = 898090L; assertEquals(expectedFloat, ObjectCoercer.coerceNumberObjectToFloatPrimitive(longPrimitive), 0); }
@Test public void coerceNumberObjectToFloatPrimitiveWhenGivenNonNumberObjectShouldReturnZero() { JSONObject jsonObject = new JSONObject(); float expectedFloat = new Float(0); assertEquals(expectedFloat, ObjectCoercer.coerceNumberObjectToFloatPrimitive(jsonObject), 0); }