From 3d17fad6d6332b8473cbeb9c516a84e808f43ca4 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Sat, 24 Oct 2009 00:00:47 +0000 Subject: [PATCH] Patch from Michael Rudolf in LANG-522 applied with additional String constructor tests added git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@829283 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/lang/mutable/MutableByte.java | 13 +++++++++++++ .../apache/commons/lang/mutable/MutableDouble.java | 13 +++++++++++++ .../apache/commons/lang/mutable/MutableFloat.java | 13 +++++++++++++ .../org/apache/commons/lang/mutable/MutableInt.java | 13 +++++++++++++ .../apache/commons/lang/mutable/MutableLong.java | 13 +++++++++++++ .../apache/commons/lang/mutable/MutableShort.java | 13 +++++++++++++ .../commons/lang/mutable/MutableByteTest.java | 5 ++++- .../commons/lang/mutable/MutableDoubleTest.java | 5 ++++- .../commons/lang/mutable/MutableFloatTest.java | 5 ++++- .../apache/commons/lang/mutable/MutableIntTest.java | 5 ++++- .../commons/lang/mutable/MutableLongTest.java | 5 ++++- .../commons/lang/mutable/MutableShortTest.java | 5 ++++- 12 files changed, 102 insertions(+), 6 deletions(-) diff --git a/src/java/org/apache/commons/lang/mutable/MutableByte.java b/src/java/org/apache/commons/lang/mutable/MutableByte.java index 72ead1d8d..a4fa91a1c 100644 --- a/src/java/org/apache/commons/lang/mutable/MutableByte.java +++ b/src/java/org/apache/commons/lang/mutable/MutableByte.java @@ -66,6 +66,19 @@ public class MutableByte extends Number implements Comparable, Muta this.value = value.byteValue(); } + /** + * Constructs a new MutableByte parsing the given string. + * + * @param value + * the string to parse. + * @throws NumberFormatException + * if the string cannot be parsed into a byte + */ + public MutableByte(String value) throws NumberFormatException { + super(); + this.value = Byte.parseByte(value); + } + //----------------------------------------------------------------------- /** * Gets the value as a Byte instance. diff --git a/src/java/org/apache/commons/lang/mutable/MutableDouble.java b/src/java/org/apache/commons/lang/mutable/MutableDouble.java index cabd5610a..c6b9d373e 100644 --- a/src/java/org/apache/commons/lang/mutable/MutableDouble.java +++ b/src/java/org/apache/commons/lang/mutable/MutableDouble.java @@ -66,6 +66,19 @@ public class MutableDouble extends Number implements Comparable, this.value = value.doubleValue(); } + /** + * Constructs a new MutableDouble parsing the given string. + * + * @param value + * the string to parse. + * @throws NumberFormatException + * if the string cannot be parsed into a double + */ + public MutableDouble(String value) throws NumberFormatException { + super(); + this.value = Double.parseDouble(value); + } + //----------------------------------------------------------------------- /** * Gets the value as a Double instance. diff --git a/src/java/org/apache/commons/lang/mutable/MutableFloat.java b/src/java/org/apache/commons/lang/mutable/MutableFloat.java index 04e03fbe0..c528301ea 100644 --- a/src/java/org/apache/commons/lang/mutable/MutableFloat.java +++ b/src/java/org/apache/commons/lang/mutable/MutableFloat.java @@ -66,6 +66,19 @@ public class MutableFloat extends Number implements Comparable, Mu this.value = value.floatValue(); } + /** + * Constructs a new MutableFloat parsing the given string. + * + * @param value + * the string to parse. + * @throws NumberFormatException + * if the string cannot be parsed into a float + */ + public MutableFloat(String value) throws NumberFormatException { + super(); + this.value = Float.parseFloat(value); + } + //----------------------------------------------------------------------- /** * Gets the value as a Float instance. diff --git a/src/java/org/apache/commons/lang/mutable/MutableInt.java b/src/java/org/apache/commons/lang/mutable/MutableInt.java index 668b717e5..0599dc2ea 100644 --- a/src/java/org/apache/commons/lang/mutable/MutableInt.java +++ b/src/java/org/apache/commons/lang/mutable/MutableInt.java @@ -66,6 +66,19 @@ public class MutableInt extends Number implements Comparable, Mutabl this.value = value.intValue(); } + /** + * Constructs a new MutableInt parsing the given string. + * + * @param value + * the string to parse. + * @throws NumberFormatException + * if the string cannot be parsed into an int + */ + public MutableInt(String value) throws NumberFormatException { + super(); + this.value = Integer.parseInt(value); + } + //----------------------------------------------------------------------- /** * Gets the value as a Integer instance. diff --git a/src/java/org/apache/commons/lang/mutable/MutableLong.java b/src/java/org/apache/commons/lang/mutable/MutableLong.java index f2282fab2..f89f1e14b 100644 --- a/src/java/org/apache/commons/lang/mutable/MutableLong.java +++ b/src/java/org/apache/commons/lang/mutable/MutableLong.java @@ -66,6 +66,19 @@ public class MutableLong extends Number implements Comparable, Muta this.value = value.longValue(); } + /** + * Constructs a new MutableLong parsing the given string. + * + * @param value + * the string to parse. + * @throws NumberFormatException + * if the string cannot be parsed into a long + */ + public MutableLong(String value) throws NumberFormatException { + super(); + this.value = Long.parseLong(value); + } + //----------------------------------------------------------------------- /** * Gets the value as a Long instance. diff --git a/src/java/org/apache/commons/lang/mutable/MutableShort.java b/src/java/org/apache/commons/lang/mutable/MutableShort.java index 35d222fbe..b653a3ed6 100644 --- a/src/java/org/apache/commons/lang/mutable/MutableShort.java +++ b/src/java/org/apache/commons/lang/mutable/MutableShort.java @@ -66,6 +66,19 @@ public class MutableShort extends Number implements Comparable, Mu this.value = value.shortValue(); } + /** + * Constructs a new MutableShort parsing the given string. + * + * @param value + * the string to parse. + * @throws NumberFormatException + * if the string cannot be parsed into a short + */ + public MutableShort(String value) throws NumberFormatException { + super(); + this.value = Short.parseShort(value); + } + //----------------------------------------------------------------------- /** * Gets the value as a Short instance. diff --git a/src/test/org/apache/commons/lang/mutable/MutableByteTest.java b/src/test/org/apache/commons/lang/mutable/MutableByteTest.java index 5e67070c6..907c98d75 100644 --- a/src/test/org/apache/commons/lang/mutable/MutableByteTest.java +++ b/src/test/org/apache/commons/lang/mutable/MutableByteTest.java @@ -49,8 +49,11 @@ public class MutableByteTest extends TestCase { assertEquals((byte) 2, new MutableByte(Byte.valueOf((byte) 2)).byteValue()); assertEquals((byte) 3, new MutableByte(new MutableByte((byte) 3)).byteValue()); + + assertEquals((byte) 2, new MutableByte("2").byteValue()); + try { - new MutableByte(null); + new MutableByte((Number)null); fail(); } catch (NullPointerException ex) {} } diff --git a/src/test/org/apache/commons/lang/mutable/MutableDoubleTest.java b/src/test/org/apache/commons/lang/mutable/MutableDoubleTest.java index 3f0392302..eef280f33 100644 --- a/src/test/org/apache/commons/lang/mutable/MutableDoubleTest.java +++ b/src/test/org/apache/commons/lang/mutable/MutableDoubleTest.java @@ -49,8 +49,11 @@ public class MutableDoubleTest extends TestCase { assertEquals(2d, new MutableDouble(new Double(2d)).doubleValue(), 0.0001d); assertEquals(3d, new MutableDouble(new MutableDouble(3d)).doubleValue(), 0.0001d); + + assertEquals(2d, new MutableDouble("2.0").doubleValue(), 0.0001d); + try { - new MutableDouble(null); + new MutableDouble((Number)null); fail(); } catch (NullPointerException ex) {} } diff --git a/src/test/org/apache/commons/lang/mutable/MutableFloatTest.java b/src/test/org/apache/commons/lang/mutable/MutableFloatTest.java index a0e7052f1..53d1835a5 100644 --- a/src/test/org/apache/commons/lang/mutable/MutableFloatTest.java +++ b/src/test/org/apache/commons/lang/mutable/MutableFloatTest.java @@ -49,8 +49,11 @@ public class MutableFloatTest extends TestCase { assertEquals(2f, new MutableFloat(new Float(2f)).floatValue(), 0.0001f); assertEquals(3f, new MutableFloat(new MutableFloat(3f)).floatValue(), 0.0001f); + + assertEquals(2f, new MutableDouble("2.0").floatValue(), 0.0001f); + try { - new MutableFloat(null); + new MutableFloat((Number)null); fail(); } catch (NullPointerException ex) {} } diff --git a/src/test/org/apache/commons/lang/mutable/MutableIntTest.java b/src/test/org/apache/commons/lang/mutable/MutableIntTest.java index 10dcb0446..9a6a957f9 100644 --- a/src/test/org/apache/commons/lang/mutable/MutableIntTest.java +++ b/src/test/org/apache/commons/lang/mutable/MutableIntTest.java @@ -49,8 +49,11 @@ public class MutableIntTest extends TestCase { assertEquals(2, new MutableInt(new Integer(2)).intValue()); assertEquals(3, new MutableInt(new MutableLong(3)).intValue()); + + assertEquals(2, new MutableInt("2").intValue()); + try { - new MutableInt(null); + new MutableInt((Number)null); fail(); } catch (NullPointerException ex) {} } diff --git a/src/test/org/apache/commons/lang/mutable/MutableLongTest.java b/src/test/org/apache/commons/lang/mutable/MutableLongTest.java index f23413db1..1de71af5d 100644 --- a/src/test/org/apache/commons/lang/mutable/MutableLongTest.java +++ b/src/test/org/apache/commons/lang/mutable/MutableLongTest.java @@ -49,8 +49,11 @@ public class MutableLongTest extends TestCase { assertEquals(2, new MutableLong(new Long(2)).longValue()); assertEquals(3, new MutableLong(new MutableLong(3)).longValue()); + + assertEquals(2, new MutableLong("2").longValue()); + try { - new MutableLong(null); + new MutableLong((Number)null); fail(); } catch (NullPointerException ex) {} } diff --git a/src/test/org/apache/commons/lang/mutable/MutableShortTest.java b/src/test/org/apache/commons/lang/mutable/MutableShortTest.java index 497d4acc1..b18eef0c3 100644 --- a/src/test/org/apache/commons/lang/mutable/MutableShortTest.java +++ b/src/test/org/apache/commons/lang/mutable/MutableShortTest.java @@ -49,8 +49,11 @@ public class MutableShortTest extends TestCase { assertEquals((short) 2, new MutableShort(new Short((short) 2)).shortValue()); assertEquals((short) 3, new MutableShort(new MutableShort((short) 3)).shortValue()); + + assertEquals((short) 2, new MutableShort("2").shortValue()); + try { - new MutableShort(null); + new MutableShort((Number)null); fail(); } catch (NullPointerException ex) {} }