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);  }
@Test public void coerceNumberObjectToDoublePrimitiveWhenGivenDoubleObjectShouldReturnDoublePrimitive() { double expectedDouble = 8.394; Double doubleObject = new Double(expectedDouble); assertEquals(expectedDouble, ObjectCoercer.coerceNumberObjectToDoublePrimitive(doubleObject), 0); }
@Test public void coerceNumberObjectToDoublePrimitiveWhenGivenFloatObjectShouldReturnDoublePrimitive() { double expectedDouble = 8.394F; Float floatObject = new Float(expectedDouble); assertEquals(expectedDouble, ObjectCoercer.coerceNumberObjectToDoublePrimitive(floatObject), 0); }
@Test public void coerceNumberObjectToDoublePrimitiveWhenGivenIntegerObjectShouldReturnDoublePrimitive() { double expectedDouble = 8; Integer integerObject = new Integer(8); assertEquals(expectedDouble, ObjectCoercer.coerceNumberObjectToDoublePrimitive(integerObject), 0); }
@Test public void coerceNumberObjectToDoublePrimitiveWhenGivenLongObjectShouldReturnDoublePrimitive() { double expectedDouble = 9882398; Long longObject = new Long(9882398); assertEquals(expectedDouble, ObjectCoercer.coerceNumberObjectToDoublePrimitive(longObject), 0); }
@Test public void coerceNumberObjectToDoublePrimitiveWhenGivenDoublePrimitiveShouldReturnDoublePrimitive() { double expectedDouble = 8923.23099; assertEquals(expectedDouble, ObjectCoercer.coerceNumberObjectToDoublePrimitive(expectedDouble), 0); }
@Test public void coerceNumberObjectToDoublePrimitiveWhenGivenIntegerPrimitiveShouldReturnDoublePrimitive() { double expectedDouble = 898232; int intPrimtive = 898232; assertEquals(expectedDouble, ObjectCoercer.coerceNumberObjectToDoublePrimitive(intPrimtive), 0); }
@Test public void coerceNumberObjectToDoublePrimitiveWhenGivenFloatPrimitiveShouldReturnDoublePrimitive() { double expectedDouble = 898.234f; float floatPrimitive = 898.234f; assertEquals(expectedDouble, ObjectCoercer.coerceNumberObjectToDoublePrimitive(floatPrimitive), 0); }
@Test public void coerceNumberObjectToDoublePrimitiveWhenGivenLongPrimitiveShouldReturnDoublePrimitive() { double expectedDouble = 898090L; long longPrimitive = 898090L; assertEquals(expectedDouble, ObjectCoercer.coerceNumberObjectToDoublePrimitive(longPrimitive), 0); }
@Test public void coerceNumberObjectToFloatPrimitiveWhenGivenFloatPrimitiveShouldReturnDoublePrimitive() { float expectedFloat = 898.234f; assertEquals(expectedFloat, ObjectCoercer.coerceNumberObjectToDoublePrimitive(expectedFloat), 0); }
@Test public void coerceNumberObjectToDoublePrimitiveWhenGivenNonNumberObjectShouldReturnZero() { JSONObject jsonObject = new JSONObject(); double expectedDouble = new Double(0); assertEquals(expectedDouble, ObjectCoercer.coerceNumberObjectToDoublePrimitive(jsonObject), 0); }