RequestParameter extends Parameter<String> { public Double toDouble() { String value = toString(); try { return Double.parseDouble(value); } catch (NumberFormatException e) { throw new InvalidRequestParameterException(getName(), value); } } RequestParameter(String name, String value); boolean isBlank(); @Override boolean isEmpty(); @Override RequestParameter validateWith(ValidationStrategy<String> validationStrategy); @Override String toString(); String toString(String defaultValue); Double toDouble(); Double toDouble(double defaultValue); Long toLong(); Long toLong(long defaultValue); Integer toInteger(); Integer toInteger(int defaultValue); boolean toBoolean(); boolean toBoolean(boolean defaultValue); E toEnum(Class<E> type); E toEnum(Class<E> type, E defaultValue);  }
@Test public void testToDoubleWithValidValues() throws Exception { assertEquals(Double.valueOf(0.1), new RequestParameter(null, "0.1").toDouble()); assertEquals(Double.valueOf(-0.1), new RequestParameter(null, "-0.1").toDouble()); }
@Test public void testToDouble() throws Exception { assertEquals(Double.valueOf(0.1), new RequestParameter(null, "0.1").toDouble()); assertEquals(Double.valueOf(-0.1), new RequestParameter(null, "-0.1").toDouble()); assertEquals(Double.valueOf(0.1), new RequestParameter(null, "foo").toDouble(0.1)); assertEquals(Double.valueOf(0.1), new RequestParameter(null, "").toDouble(0.1)); assertEquals(Double.valueOf(0.1), new RequestParameter(null, null).toDouble(0.1)); try { new RequestParameter("foo", "").toDouble(); } catch (MissingRequestParameterException e) { assertEquals("foo", e.getParameterName()); } try { new RequestParameter("foo", null).toDouble(); } catch (MissingRequestParameterException e) { assertEquals("foo", e.getParameterName()); } try { new RequestParameter("foo", "0.1").validateWith(ALWAYS_INVALID_VALIDATION).toDouble(); } catch (InvalidRequestParameterException e) { assertEquals("foo", e.getParameterName()); assertEquals("0.1", e.getParameterValue()); } }