From 954280893c34f560b35c3eb1c106852110d8ca27 Mon Sep 17 00:00:00 2001 From: Duncan Jones Date: Sat, 1 Feb 2014 07:53:54 +0000 Subject: [PATCH] Reverts changes introduced in r1563259. Discussion to be had over correct place for this functionality (likely org.apache.commons.lang3.Conversion). git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1563378 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 3 - src/changes/changes.xml | 1 - .../commons/lang3/math/NumberUtils.java | 236 ------------------ .../commons/lang3/math/NumberUtilsTest.java | 105 +------- 4 files changed, 10 insertions(+), 335 deletions(-) diff --git a/pom.xml b/pom.xml index 677d7fc99..027e8e82a 100644 --- a/pom.xml +++ b/pom.xml @@ -364,9 +364,6 @@ Travis Reeder - - Vincent Ricard - Antony Riley diff --git a/src/changes/changes.xml b/src/changes/changes.xml index eba6f7900..3942550a5 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -23,7 +23,6 @@ Add SerializationUtils.roundtrip(T extends Serializable) to serialize then deserialize - Please add number to byte[] methods org.apache.commons.lang3.reflect.FieldUtils.removeFinalModifier(Field) does not clean up after itself FastDateParser javadoc incorrectly states that SimpleDateFormat is used internally There should be a DifferenceBuilder with a ReflectionDifferenceBuilder implementation diff --git a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java index 1b34d529b..70b8d6466 100644 --- a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java +++ b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java @@ -21,7 +21,6 @@ import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.Validate; /** *

Provides extra functionality for Java Number classes.

@@ -1434,239 +1433,4 @@ public class NumberUtils { return !allowSigns && foundDigit; } - // NOTE: toPrimitive(byte[]) method contents are taken from: - // http://svn.apache.org/repos/asf/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/DataInputStream.java - // toByteArray(primitive) method contents are taken from: - // http://svn.apache.org/repos/asf/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/DataOutputStream.java - - - /** - *

- * Converts a byte array to a {@code short}. - *

- *

- * The first two bytes of the - * array are converted from two's complement representation to a {@code short}. - * Any additional bytes are ignored. - *

- *

- * If the array is {@code null}, {@code 0} is returned. - *

- * - * @param array the array to convert - * @return the converted value - * @throws IllegalArgumentException if the array has fewer than two elements - */ - public static short toShort(byte[] array) { - // Match toShort(String) - if (array == null) { - return 0; - } - - Validate.isTrue(array.length >= 2, "Byte array length too short: %d", array.length); - - return (short) (((array[0] & 0xff) << 8) | (array[1] & 0xff)); - } - - /** - *

- * Converts a byte array to an {@code int}. - *

- *

- * The first four bytes of the - * array are converted from two's complement representation to an {@code int}. - * Any additional bytes are ignored. - *

- *

- * If the array is {@code null}, {@code 0} is returned. - *

- * - * @param array the array to convert - * @return the converted value - * @throws IllegalArgumentException if the array has fewer than four elements - */ - public static int toInt(byte[] array) { - // Match toInt(String) - if (array == null) { - return 0; - } - - Validate.isTrue(array.length >= 4, "Byte array length too short: %d", array.length); - - return ((array[0] & 0xff) << 24) | ((array[1] & 0xff) << 16) | - ((array[2] & 0xff) << 8) | (array[3] & 0xff); - } - - - - /** - *

- * Converts a byte array to a {@code long}. - *

- *

- * The first eight bytes of the - * array are converted from two's complement representation to a {@code long}. - * Any additional bytes are ignored. - *

- *

- * If the array is {@code null}, {@code 0} is returned. - *

- * - * @param array the array to convert - * @return the converted value - * @throws IllegalArgumentException if the array has fewer than eight elements - */ - public static long toLong(byte[] array) { - // Match toLong(String) - if (array == null) { - return 0l; - } - - Validate.isTrue(array.length >= 8, "Byte array length too short: %d", array.length); - - int i1 = ((array[0] & 0xff) << 24) | ((array[1] & 0xff) << 16) | - ((array[2] & 0xff) << 8) | (array[3] & 0xff); - int i2 = ((array[4] & 0xff) << 24) | ((array[5] & 0xff) << 16) | - ((array[6] & 0xff) << 8) | (array[7] & 0xff); - - return ((i1 & 0xffffffffL) << 32) | (i2 & 0xffffffffL); - } - - - - /** - *

- * Converts a byte array to a {@code float}. - *

- *

- * The first four bytes of the array are converted from IEEE 754 floating-point - * "single format" bit layout to a {@code float}. - *

- *

- * If the array is {@code null}, {@code 0} is returned. - *

- * - * @param array the array to convert - * @return the converted value - * @throws IllegalArgumentException if the array has fewer than four elements - */ - public static float toFloat(byte[] array) { - // Match toFloat(String) - if (array == null) { - return 0.0f; - } - - Validate.isTrue(array.length >= 4, "Byte array length too short: %d", array.length); - - return Float.intBitsToFloat(toInt(array)); - } - - /** - *

- * Converts a byte array to a {@code double}. - *

- *

- * The first eight bytes of the array are converted from IEEE 754 floating-point - * "double format" bit layout to a {@code double}. - *

- *

- * If the array is {@code null}, {@code 0} is returned. - *

- * - * @param array the array to convert - * @return the converted value - * @throws IllegalArgumentException if the array has fewer than eight elements - */ - public static double toDouble(byte[] array) { - // Match toDouble(String) - if (array == null) { - return 0.0; - } - - Validate.isTrue(array.length >= 8, "Byte array length too short: %d", array.length); - - return Double.longBitsToDouble(toLong(array)); - } - - - /** - *

- * Converts a {@code short} to a two-byte array in two's complement format. - *

- * - * @param value the value to convert - * @return the byte array - */ - public static byte[] toByteArray(short value) { - byte[] buffer = new byte[2]; - buffer[0] = (byte) (value >> 8); - buffer[1] = (byte) value; - return buffer; - } - - /** - *

- * Converts an {@code int} to a four-byte array in two's complement format. - *

- * - * @param value the value to convert - * @return the byte array - */ - public static byte[] toByteArray(int value) { - byte[] buffer = new byte[4]; - buffer[0] = (byte) (value >> 24); - buffer[1] = (byte) (value >> 16); - buffer[2] = (byte) (value >> 8); - buffer[3] = (byte) value; - return buffer; - } - - - - /** - *

- * Converts a {@code float} to a four-byte array in IEEE 754 floating-point - * "single format" bit layout. - *

- * - * @param value the value to convert - * @return the byte array - */ - public static byte[] toByteArray(float value) { - return toByteArray(Float.floatToIntBits(value)); - } - - /** - *

- * Converts a {@code long} to an eight-byte array in two's complement format. - *

- * - * @param value the value to convert - * @return the byte array - */ - public static byte[] toByteArray(long value) { - byte[] buffer = new byte[8]; - buffer[0] = (byte) (value >> 56); - buffer[1] = (byte) (value >> 48); - buffer[2] = (byte) (value >> 40); - buffer[3] = (byte) (value >> 32); - buffer[4] = (byte) (value >> 24); - buffer[5] = (byte) (value >> 16); - buffer[6] = (byte) (value >> 8); - buffer[7] = (byte) value; - return buffer; - } - - /** - *

- * Converts a {@code double} to an eight-byte array in IEEE 754 floating-point - * "double format" bit layout. - *

- * - * @param value the value to convert - * @return the byte array - */ - public static byte[] toByteArray(double val) { - return toByteArray(Double.doubleToLongBits(val)); - } } diff --git a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java index 21d0ef21a..3404a9faa 100644 --- a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java @@ -16,7 +16,11 @@ */ package org.apache.commons.lang3.math; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; @@ -53,7 +57,7 @@ public class NumberUtilsTest { assertTrue("toInt(String) 1 failed", NumberUtils.toInt("12345") == 12345); assertTrue("toInt(String) 2 failed", NumberUtils.toInt("abc") == 0); assertTrue("toInt(empty) failed", NumberUtils.toInt("") == 0); - assertTrue("toInt(null) failed", NumberUtils.toInt((String) null) == 0); + assertTrue("toInt(null) failed", NumberUtils.toInt(null) == 0); } /** @@ -77,7 +81,7 @@ public class NumberUtilsTest { assertTrue("toLong(Long.MAX_VALUE) failed", NumberUtils.toLong(Long.MAX_VALUE+"") == Long.MAX_VALUE); assertTrue("toLong(Long.MIN_VALUE) failed", NumberUtils.toLong(Long.MIN_VALUE+"") == Long.MIN_VALUE); assertTrue("toLong(empty) failed", NumberUtils.toLong("") == 0l); - assertTrue("toLong(null) failed", NumberUtils.toLong((String) null) == 0l); + assertTrue("toLong(null) failed", NumberUtils.toLong(null) == 0l); } /** @@ -100,7 +104,7 @@ public class NumberUtilsTest { assertTrue("toFloat(Float.MAX_VALUE) failed", NumberUtils.toFloat(Float.MAX_VALUE+"") == Float.MAX_VALUE); assertTrue("toFloat(Float.MIN_VALUE) failed", NumberUtils.toFloat(Float.MIN_VALUE+"") == Float.MIN_VALUE); assertTrue("toFloat(empty) failed", NumberUtils.toFloat("") == 0.0f); - assertTrue("toFloat(null) failed", NumberUtils.toFloat((String) null) == 0.0f); + assertTrue("toFloat(null) failed", NumberUtils.toFloat(null) == 0.0f); } /** @@ -136,7 +140,7 @@ public class NumberUtilsTest { assertTrue("toDouble(Double.MAX_VALUE) failed", NumberUtils.toDouble(Double.MAX_VALUE+"") == Double.MAX_VALUE); assertTrue("toDouble(Double.MIN_VALUE) failed", NumberUtils.toDouble(Double.MIN_VALUE+"") == Double.MIN_VALUE); assertTrue("toDouble(empty) failed", NumberUtils.toDouble("") == 0.0d); - assertTrue("toDouble(null) failed", NumberUtils.toDouble((String) null) == 0.0d); + assertTrue("toDouble(null) failed", NumberUtils.toDouble(null) == 0.0d); } /** @@ -176,7 +180,7 @@ public class NumberUtilsTest { assertTrue("toShort(String) 1 failed", NumberUtils.toShort("12345") == 12345); assertTrue("toShort(String) 2 failed", NumberUtils.toShort("abc") == 0); assertTrue("toShort(empty) failed", NumberUtils.toShort("") == 0); - assertTrue("toShort(null) failed", NumberUtils.toShort((String) null) == 0); + assertTrue("toShort(null) failed", NumberUtils.toShort(null) == 0); } /** @@ -1395,94 +1399,5 @@ public class NumberUtilsTest { final float[] bF = new float[] { Float.NaN, 1.2f, Float.NaN, 3.7f, 27.0f, 42.0f, Float.NaN }; assertTrue(Float.isNaN(NumberUtils.max(bF))); } - - @Test - public void testByteArrayConversionArgChecking() throws Exception { - try { - NumberUtils.toShort(new byte[1]); - fail(); - } catch (IllegalArgumentException e) {} - - try { - NumberUtils.toInt(new byte[3]); - fail(); - } catch (IllegalArgumentException e) {} - - try { - NumberUtils.toLong(new byte[7]); - fail(); - } catch (IllegalArgumentException e) {} - - try { - NumberUtils.toFloat(new byte[3]); - fail(); - } catch (IllegalArgumentException e) {} - - try { - NumberUtils.toDouble(new byte[7]); - fail(); - } catch (IllegalArgumentException e) {} - } - - @Test - public void testShortByteArrayConversion() { - // tests symmetry - assertEquals((short) 1, NumberUtils.toShort(NumberUtils.toByteArray((short) 1))); - - assertEquals(0, NumberUtils.toShort((byte[]) null)); - assertArrayEquals(new byte[] {(byte) 0x7F, (byte) 0xFF}, NumberUtils.toByteArray(Short.MAX_VALUE)); - assertArrayEquals(new byte[2], NumberUtils.toByteArray((short) 0)); - assertArrayEquals(new byte[] {(byte) 0xFF, (byte) 0xFF}, NumberUtils.toByteArray((short) -1)); - assertArrayEquals(new byte[] {(byte) 0x80, (byte) 0x00}, NumberUtils.toByteArray(Short.MIN_VALUE)); - } - - - @Test - public void testIntByteArrayConversion() { - // tests symmetry - assertEquals(1, NumberUtils.toInt(NumberUtils.toByteArray(1))); - - assertEquals(0, NumberUtils.toInt((byte[]) null)); - assertArrayEquals(new byte[] {(byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}, - NumberUtils.toByteArray(Integer.MAX_VALUE)); - assertArrayEquals(new byte[4], - NumberUtils.toByteArray(0)); - assertArrayEquals(new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}, - NumberUtils.toByteArray(-1)); - assertArrayEquals(new byte[] {(byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00}, - NumberUtils.toByteArray(Integer.MIN_VALUE)); - } - - @Test - public void testLongByteArrayConversion() { - // tests symmetry - assertEquals(1l, NumberUtils.toLong(NumberUtils.toByteArray(1l))); - - assertEquals(0, NumberUtils.toLong((byte[]) null)); - assertArrayEquals(new byte[] {(byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}, - NumberUtils.toByteArray(Long.MAX_VALUE)); - assertArrayEquals(new byte[8], - NumberUtils.toByteArray(0L)); - assertArrayEquals(new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}, - NumberUtils.toByteArray(-1L)); - assertArrayEquals(new byte[] {(byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}, - NumberUtils.toByteArray(Long.MIN_VALUE)); - } - - @Test - public void testDoubleByteArrayConversion() { - // tests symmetry - assertEquals(0, NumberUtils.toDouble((byte[]) null), 0); - assertEquals(0, Double.compare(NumberUtils.toDouble(NumberUtils.toByteArray(1.1d)), 1.1d)); - } - - - @Test - public void testFloatByteArrayConversion() { - // tests symmetry - assertEquals(0, NumberUtils.toFloat((byte[]) null), 0); - assertEquals(0, Float.compare(NumberUtils.toFloat(NumberUtils.toByteArray(1.1f)), 1.1f)); - } - }