commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Eric Pugh" <ep...@upstate.com>
Subject RE: [configuration] Numbers conversions
Date Sun, 13 Jun 2004 13:43:26 GMT
[configuration] Numbers conversionsI like the upcasting stuff!  However
shouldn't these unit tests throw the ConversionRuntimeException:

// downcasting
+        config.setProperty("number", new Long(Long.MAX_VALUE));
+        assertEquals("Long to Integer", -1, config.getInt("number"));
+        assertEquals("Long to Short", -1, config.getShort("number"));
+        assertEquals("Long to Byte", -1, config.getByte("number"));

If I put in a really big number and then try to retrieve it as an Integer,
getting back -1 would be a rather shock!  I think I would rather hit an
exception instead, thereby clueing me in that I couldn't downcast...

Eric
  -----Original Message-----
  From: Emmanuel Bourg [mailto:smanux@lfjr.net]
  Sent: Sunday, June 13, 2004 2:58 PM
  To: Jakarta Commons Developers List
  Subject: [configuration] Numbers conversions


  Hello, I'm considering a change to improve the flexibility of
  [configuration] with regard to the numbers conversions. Currently any
  Number put in a configuration can't be retrieved as another Number type,
  for example:

  config.setProperty("number", new Byte(123));
  config.getInteger("number");

  throws a ClassCastException, but:

  config.setProperty("number", "123");
  config.getInteger("number");

  works fine.

  The change consists in testing in the getXXX methods if the object
  retrieved is a Number and call the Number.xxxValue() method to convert
  it to the appropriate type. I'm attaching a patch and a test case to
  illustrate this.

  What do you think ?

  Emmanuel Bourg



Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message