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
This commit is contained in:
Duncan Jones 2014-02-01 07:53:54 +00:00
parent a3de60835e
commit 954280893c
4 changed files with 10 additions and 335 deletions

View File

@ -364,9 +364,6 @@
<contributor>
<name>Travis Reeder</name>
</contributor>
<contributor>
<name>Vincent Ricard</name>
</contributor>
<contributor>
<name>Antony Riley</name>
</contributor>

View File

@ -23,7 +23,6 @@
<release version="3.3" date="TBA" description="Bugfix and Feature release">
<action issue="LANG-962" type="add" dev="ggregory">Add SerializationUtils.roundtrip(T extends Serializable) to serialize then deserialize</action>
<action issue="LANG-341" type="add" due-to="Vincent Ricard" dev="djones">Please add number to byte[] methods</action>
<action issue="LANG-961" type="update" dev="ggregory">org.apache.commons.lang3.reflect.FieldUtils.removeFinalModifier(Field) does not clean up after itself</action>
<action issue="LANG-958" type="update" dev="chas">FastDateParser javadoc incorrectly states that SimpleDateFormat is used internally</action>
<action issue="LANG-637" type="add" dev="djones">There should be a DifferenceBuilder with a ReflectionDifferenceBuilder implementation</action>

View File

@ -21,7 +21,6 @@ import java.math.BigDecimal;
import java.math.BigInteger;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
/**
* <p>Provides extra functionality for Java Number classes.</p>
@ -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
/**
* <p>
* Converts a byte array to a {@code short}.
* </p>
* <p>
* The first two bytes of the
* array are converted from two's complement representation to a {@code short}.
* Any additional bytes are ignored.
* </p>
* <p>
* If the array is {@code null}, {@code 0} is returned.
* </p>
*
* @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));
}
/**
* <p>
* Converts a byte array to an {@code int}.
* </p>
* <p>
* The first four bytes of the
* array are converted from two's complement representation to an {@code int}.
* Any additional bytes are ignored.
* </p>
* <p>
* If the array is {@code null}, {@code 0} is returned.
* </p>
*
* @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);
}
/**
* <p>
* Converts a byte array to a {@code long}.
* </p>
* <p>
* The first eight bytes of the
* array are converted from two's complement representation to a {@code long}.
* Any additional bytes are ignored.
* </p>
* <p>
* If the array is {@code null}, {@code 0} is returned.
* </p>
*
* @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);
}
/**
* <p>
* Converts a byte array to a {@code float}.
* </p>
* <p>
* The first four bytes of the array are converted from IEEE 754 floating-point
* "single format" bit layout to a {@code float}.
* </p>
* <p>
* If the array is {@code null}, {@code 0} is returned.
* </p>
*
* @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));
}
/**
* <p>
* Converts a byte array to a {@code double}.
* </p>
* <p>
* The first eight bytes of the array are converted from IEEE 754 floating-point
* "double format" bit layout to a {@code double}.
* </p>
* <p>
* If the array is {@code null}, {@code 0} is returned.
* </p>
*
* @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));
}
/**
* <p>
* Converts a {@code short} to a two-byte array in two's complement format.
* </p>
*
* @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;
}
/**
* <p>
* Converts an {@code int} to a four-byte array in two's complement format.
* </p>
*
* @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;
}
/**
* <p>
* Converts a {@code float} to a four-byte array in IEEE 754 floating-point
* "single format" bit layout.
* </p>
*
* @param value the value to convert
* @return the byte array
*/
public static byte[] toByteArray(float value) {
return toByteArray(Float.floatToIntBits(value));
}
/**
* <p>
* Converts a {@code long} to an eight-byte array in two's complement format.
* </p>
*
* @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;
}
/**
* <p>
* Converts a {@code double} to an eight-byte array in IEEE 754 floating-point
* "double format" bit layout.
* </p>
*
* @param value the value to convert
* @return the byte array
*/
public static byte[] toByteArray(double val) {
return toByteArray(Double.doubleToLongBits(val));
}
}

View File

@ -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));
}
}