Use more efficient Long cache: Use "Long.valueOf(int)" instead of "new Long(long)"
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1153487 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ddeba76d0b
commit
4b1702489c
|
@ -30,11 +30,11 @@ import org.apache.commons.lang3.StringUtils;
|
|||
public class NumberUtils {
|
||||
|
||||
/** Reusable Long constant for zero. */
|
||||
public static final Long LONG_ZERO = new Long(0L);
|
||||
public static final Long LONG_ZERO = Long.valueOf(0L);
|
||||
/** Reusable Long constant for one. */
|
||||
public static final Long LONG_ONE = new Long(1L);
|
||||
public static final Long LONG_ONE = Long.valueOf(1L);
|
||||
/** Reusable Long constant for minus one. */
|
||||
public static final Long LONG_MINUS_ONE = new Long(-1L);
|
||||
public static final Long LONG_MINUS_ONE = Long.valueOf(-1L);
|
||||
/** Reusable Integer constant for zero. */
|
||||
public static final Integer INTEGER_ZERO = Integer.valueOf(0);
|
||||
/** Reusable Integer constant for one. */
|
||||
|
@ -398,7 +398,7 @@ public class NumberUtils {
|
|||
// Integer.valueOf(String)
|
||||
// new Double(String)
|
||||
// new Byte(String)
|
||||
// new Long(String)
|
||||
// Long.valueOf(String)
|
||||
// Long.getLong(String)
|
||||
// Long.getLong(String,int)
|
||||
// Long.getLong(String,Integer)
|
||||
|
|
|
@ -82,7 +82,7 @@ public class MutableLong extends Number implements Comparable<MutableLong>, Muta
|
|||
* @return the value as a Long, never null
|
||||
*/
|
||||
public Long getValue() {
|
||||
return new Long(this.value);
|
||||
return Long.valueOf(this.value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -2551,12 +2551,12 @@ public class ArrayUtilsTest extends TestCase {
|
|||
|
||||
assertTrue(Arrays.equals(
|
||||
new long[] {Long.MIN_VALUE, Long.MAX_VALUE, 9999999},
|
||||
ArrayUtils.toPrimitive(new Long[] {new Long(Long.MIN_VALUE),
|
||||
new Long(Long.MAX_VALUE), new Long(9999999)}))
|
||||
ArrayUtils.toPrimitive(new Long[] {Long.valueOf(Long.MIN_VALUE),
|
||||
Long.valueOf(Long.MAX_VALUE), Long.valueOf(9999999)}))
|
||||
);
|
||||
|
||||
try {
|
||||
ArrayUtils.toPrimitive(new Long[] {new Long(Long.MIN_VALUE), null});
|
||||
ArrayUtils.toPrimitive(new Long[] {Long.valueOf(Long.MIN_VALUE), null});
|
||||
fail();
|
||||
} catch (NullPointerException ex) {}
|
||||
}
|
||||
|
@ -2570,13 +2570,13 @@ public class ArrayUtilsTest extends TestCase {
|
|||
|
||||
assertTrue(Arrays.equals(
|
||||
new long[] {Long.MIN_VALUE, Long.MAX_VALUE, 9999999},
|
||||
ArrayUtils.toPrimitive(new Long[] {new Long(Long.MIN_VALUE),
|
||||
new Long(Long.MAX_VALUE), new Long(9999999)},1)));
|
||||
ArrayUtils.toPrimitive(new Long[] {Long.valueOf(Long.MIN_VALUE),
|
||||
Long.valueOf(Long.MAX_VALUE), Long.valueOf(9999999)},1)));
|
||||
|
||||
assertTrue(Arrays.equals(
|
||||
new long[] {Long.MIN_VALUE, Long.MAX_VALUE, 9999999},
|
||||
ArrayUtils.toPrimitive(new Long[] {new Long(Long.MIN_VALUE),
|
||||
null, new Long(9999999)}, Long.MAX_VALUE))
|
||||
ArrayUtils.toPrimitive(new Long[] {Long.valueOf(Long.MIN_VALUE),
|
||||
null, Long.valueOf(9999999)}, Long.MAX_VALUE))
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -2591,9 +2591,9 @@ public class ArrayUtilsTest extends TestCase {
|
|||
assertTrue(
|
||||
Arrays.equals(
|
||||
new Long[] {
|
||||
new Long(Long.MIN_VALUE),
|
||||
new Long(Long.MAX_VALUE),
|
||||
new Long(9999999)},
|
||||
Long.valueOf(Long.MIN_VALUE),
|
||||
Long.valueOf(Long.MAX_VALUE),
|
||||
Long.valueOf(9999999)},
|
||||
ArrayUtils.toObject(
|
||||
new long[] { Long.MIN_VALUE, Long.MAX_VALUE, 9999999 })));
|
||||
}
|
||||
|
|
|
@ -187,10 +187,10 @@ public class NumberUtilsTest {
|
|||
assertEquals("createNumber(String) 3 failed", new Double("1234.5"), NumberUtils.createNumber("1234.5d"));
|
||||
assertEquals("createNumber(String) 4 failed", new Float("1234.5"), NumberUtils.createNumber("1234.5F"));
|
||||
assertEquals("createNumber(String) 4 failed", new Float("1234.5"), NumberUtils.createNumber("1234.5f"));
|
||||
assertEquals("createNumber(String) 5 failed", new Long(Integer.MAX_VALUE + 1L), NumberUtils.createNumber(""
|
||||
assertEquals("createNumber(String) 5 failed", Long.valueOf(Integer.MAX_VALUE + 1L), NumberUtils.createNumber(""
|
||||
+ (Integer.MAX_VALUE + 1L)));
|
||||
assertEquals("createNumber(String) 6 failed", new Long(12345), NumberUtils.createNumber("12345L"));
|
||||
assertEquals("createNumber(String) 6 failed", new Long(12345), NumberUtils.createNumber("12345l"));
|
||||
assertEquals("createNumber(String) 6 failed", Long.valueOf(12345), NumberUtils.createNumber("12345L"));
|
||||
assertEquals("createNumber(String) 6 failed", Long.valueOf(12345), NumberUtils.createNumber("12345l"));
|
||||
assertEquals("createNumber(String) 7 failed", new Float("-1234.5"), NumberUtils.createNumber("-1234.5"));
|
||||
assertEquals("createNumber(String) 8 failed", Integer.valueOf("-12345"), NumberUtils.createNumber("-12345"));
|
||||
assertTrue("createNumber(String) 9 failed", 0xFADE == NumberUtils.createNumber("0xFADE").intValue());
|
||||
|
@ -208,9 +208,9 @@ public class NumberUtilsTest {
|
|||
assertEquals("createNumber(String) 15 failed", new BigDecimal("1.1E-700"), NumberUtils
|
||||
.createNumber("1.1E-700F"));
|
||||
}
|
||||
assertEquals("createNumber(String) 16 failed", new Long("10" + Integer.MAX_VALUE), NumberUtils
|
||||
assertEquals("createNumber(String) 16 failed", Long.valueOf("10" + Integer.MAX_VALUE), NumberUtils
|
||||
.createNumber("10" + Integer.MAX_VALUE + "L"));
|
||||
assertEquals("createNumber(String) 17 failed", new Long("10" + Integer.MAX_VALUE), NumberUtils
|
||||
assertEquals("createNumber(String) 17 failed", Long.valueOf("10" + Integer.MAX_VALUE), NumberUtils
|
||||
.createNumber("10" + Integer.MAX_VALUE));
|
||||
assertEquals("createNumber(String) 18 failed", new BigInteger("10" + Long.MAX_VALUE), NumberUtils
|
||||
.createNumber("10" + Long.MAX_VALUE));
|
||||
|
@ -288,7 +288,7 @@ public class NumberUtilsTest {
|
|||
|
||||
@Test
|
||||
public void testCreateLong() {
|
||||
assertEquals("createLong(String) failed", new Long("12345"), NumberUtils.createLong("12345"));
|
||||
assertEquals("createLong(String) failed", Long.valueOf("12345"), NumberUtils.createLong("12345"));
|
||||
assertEquals("createLong(null) failed", null, NumberUtils.createLong(null));
|
||||
this.testCreateLongFailure("");
|
||||
this.testCreateLongFailure(" ");
|
||||
|
|
|
@ -36,7 +36,7 @@ public class MutableLongTest extends TestCase {
|
|||
|
||||
assertEquals(1, new MutableLong(1).longValue());
|
||||
|
||||
assertEquals(2, new MutableLong(new Long(2)).longValue());
|
||||
assertEquals(2, new MutableLong(Long.valueOf(2)).longValue());
|
||||
assertEquals(3, new MutableLong(new MutableLong(3)).longValue());
|
||||
|
||||
assertEquals(2, new MutableLong("2").longValue());
|
||||
|
@ -50,19 +50,19 @@ public class MutableLongTest extends TestCase {
|
|||
public void testGetSet() {
|
||||
final MutableLong mutNum = new MutableLong(0);
|
||||
assertEquals(0, new MutableLong().longValue());
|
||||
assertEquals(new Long(0), new MutableLong().getValue());
|
||||
assertEquals(Long.valueOf(0), new MutableLong().getValue());
|
||||
|
||||
mutNum.setValue(1);
|
||||
assertEquals(1, mutNum.longValue());
|
||||
assertEquals(new Long(1), mutNum.getValue());
|
||||
assertEquals(Long.valueOf(1), mutNum.getValue());
|
||||
|
||||
mutNum.setValue(new Long(2));
|
||||
mutNum.setValue(Long.valueOf(2));
|
||||
assertEquals(2, mutNum.longValue());
|
||||
assertEquals(new Long(2), mutNum.getValue());
|
||||
assertEquals(Long.valueOf(2), mutNum.getValue());
|
||||
|
||||
mutNum.setValue(new MutableLong(3));
|
||||
assertEquals(3, mutNum.longValue());
|
||||
assertEquals(new Long(3), mutNum.getValue());
|
||||
assertEquals(Long.valueOf(3), mutNum.getValue());
|
||||
try {
|
||||
mutNum.setValue(null);
|
||||
fail();
|
||||
|
@ -82,7 +82,7 @@ public class MutableLongTest extends TestCase {
|
|||
assertEquals(false, mutNumB.equals(mutNumC));
|
||||
assertEquals(true, mutNumC.equals(mutNumC));
|
||||
assertEquals(false, mutNumA.equals(null));
|
||||
assertEquals(false, mutNumA.equals(new Long(0)));
|
||||
assertEquals(false, mutNumA.equals(Long.valueOf(0)));
|
||||
assertEquals(false, mutNumA.equals("0"));
|
||||
}
|
||||
|
||||
|
@ -94,7 +94,7 @@ public class MutableLongTest extends TestCase {
|
|||
assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode());
|
||||
assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode());
|
||||
assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode());
|
||||
assertEquals(true, mutNumA.hashCode() == new Long(0).hashCode());
|
||||
assertEquals(true, mutNumA.hashCode() == Long.valueOf(0).hashCode());
|
||||
}
|
||||
|
||||
public void testCompareTo() {
|
||||
|
@ -121,8 +121,8 @@ public class MutableLongTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testToLong() {
|
||||
assertEquals(new Long(0L), new MutableLong(0L).toLong());
|
||||
assertEquals(new Long(123L), new MutableLong(123L).toLong());
|
||||
assertEquals(Long.valueOf(0L), new MutableLong(0L).toLong());
|
||||
assertEquals(Long.valueOf(123L), new MutableLong(123L).toLong());
|
||||
}
|
||||
|
||||
public void testIncrement() {
|
||||
|
@ -151,7 +151,7 @@ public class MutableLongTest extends TestCase {
|
|||
|
||||
public void testAddValueObject() {
|
||||
MutableLong mutNum = new MutableLong(1);
|
||||
mutNum.add(new Long(1));
|
||||
mutNum.add(Long.valueOf(1));
|
||||
|
||||
assertEquals(2, mutNum.intValue());
|
||||
assertEquals(2L, mutNum.longValue());
|
||||
|
@ -167,7 +167,7 @@ public class MutableLongTest extends TestCase {
|
|||
|
||||
public void testSubtractValueObject() {
|
||||
MutableLong mutNum = new MutableLong(1);
|
||||
mutNum.subtract(new Long(1));
|
||||
mutNum.subtract(Long.valueOf(1));
|
||||
|
||||
assertEquals(0, mutNum.intValue());
|
||||
assertEquals(0L, mutNum.longValue());
|
||||
|
|
Loading…
Reference in New Issue