Use more efficient Short cache: Use "Short.valueOf(int)" instead of "new Short(short)"

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1153488 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2011-08-03 13:47:49 +00:00
parent 4b1702489c
commit 16d92dc923
4 changed files with 26 additions and 26 deletions

View File

@ -42,11 +42,11 @@ public class NumberUtils {
/** Reusable Integer constant for minus one. */
public static final Integer INTEGER_MINUS_ONE = Integer.valueOf(-1);
/** Reusable Short constant for zero. */
public static final Short SHORT_ZERO = new Short((short) 0);
public static final Short SHORT_ZERO = Short.valueOf((short) 0);
/** Reusable Short constant for one. */
public static final Short SHORT_ONE = new Short((short) 1);
public static final Short SHORT_ONE = Short.valueOf((short) 1);
/** Reusable Short constant for minus one. */
public static final Short SHORT_MINUS_ONE = new Short((short) -1);
public static final Short SHORT_MINUS_ONE = Short.valueOf((short) -1);
/** Reusable Byte constant for zero. */
public static final Byte BYTE_ZERO = Byte.valueOf((byte) 0);
/** Reusable Byte constant for one. */
@ -404,7 +404,7 @@ public static short toShort(String str, short defaultValue) {
// Long.getLong(String,Integer)
// Long.valueOf(String,int)
// Long.valueOf(String)
// new Short(String)
// Short.valueOf(String)
// Short.decode(String)
// Short.valueOf(String,int)
// Short.valueOf(String)

View File

@ -82,7 +82,7 @@ public MutableShort(String value) throws NumberFormatException {
* @return the value as a Short, never null
*/
public Short getValue() {
return new Short(this.value);
return Short.valueOf(this.value);
}
/**

View File

@ -2438,12 +2438,12 @@ public void testToPrimitive_short() {
assertTrue(Arrays.equals(
new short[] {Short.MIN_VALUE, Short.MAX_VALUE, (short)9999999},
ArrayUtils.toPrimitive(new Short[] {new Short(Short.MIN_VALUE),
new Short(Short.MAX_VALUE), new Short((short)9999999)}))
ArrayUtils.toPrimitive(new Short[] {Short.valueOf(Short.MIN_VALUE),
Short.valueOf(Short.MAX_VALUE), Short.valueOf((short)9999999)}))
);
try {
ArrayUtils.toPrimitive(new Short[] {new Short(Short.MIN_VALUE), null});
ArrayUtils.toPrimitive(new Short[] {Short.valueOf(Short.MIN_VALUE), null});
fail();
} catch (NullPointerException ex) {}
}
@ -2457,14 +2457,14 @@ public void testToPrimitive_short_short() {
assertTrue(Arrays.equals(
new short[] {Short.MIN_VALUE, Short.MAX_VALUE, (short)9999999},
ArrayUtils.toPrimitive(new Short[] {new Short(Short.MIN_VALUE),
new Short(Short.MAX_VALUE), new Short((short)9999999)}, Short.MIN_VALUE))
ArrayUtils.toPrimitive(new Short[] {Short.valueOf(Short.MIN_VALUE),
Short.valueOf(Short.MAX_VALUE), Short.valueOf((short)9999999)}, Short.MIN_VALUE))
);
assertTrue(Arrays.equals(
new short[] {Short.MIN_VALUE, Short.MAX_VALUE, (short)9999999},
ArrayUtils.toPrimitive(new Short[] {new Short(Short.MIN_VALUE), null,
new Short((short)9999999)}, Short.MAX_VALUE))
ArrayUtils.toPrimitive(new Short[] {Short.valueOf(Short.MIN_VALUE), null,
Short.valueOf((short)9999999)}, Short.MAX_VALUE))
);
}
@ -2476,8 +2476,8 @@ public void testToObject_short() {
ArrayUtils.toObject(new short[0]));
assertTrue(Arrays.equals(
new Short[] {new Short(Short.MIN_VALUE), new Short(Short.MAX_VALUE),
new Short((short)9999999)},
new Short[] {Short.valueOf(Short.MIN_VALUE), Short.valueOf(Short.MAX_VALUE),
Short.valueOf((short)9999999)},
ArrayUtils.toObject(new short[] {Short.MIN_VALUE, Short.MAX_VALUE,
(short)9999999}))
);

View File

@ -36,7 +36,7 @@ public void testConstructors() {
assertEquals((short) 1, new MutableShort((short) 1).shortValue());
assertEquals((short) 2, new MutableShort(new Short((short) 2)).shortValue());
assertEquals((short) 2, new MutableShort(Short.valueOf((short) 2)).shortValue());
assertEquals((short) 3, new MutableShort(new MutableShort((short) 3)).shortValue());
assertEquals((short) 2, new MutableShort("2").shortValue());
@ -50,19 +50,19 @@ public void testConstructors() {
public void testGetSet() {
final MutableShort mutNum = new MutableShort((short) 0);
assertEquals((short) 0, new MutableShort().shortValue());
assertEquals(new Short((short) 0), new MutableShort().getValue());
assertEquals(Short.valueOf((short) 0), new MutableShort().getValue());
mutNum.setValue((short) 1);
assertEquals((short) 1, mutNum.shortValue());
assertEquals(new Short((short) 1), mutNum.getValue());
assertEquals(Short.valueOf((short) 1), mutNum.getValue());
mutNum.setValue(new Short((short) 2));
mutNum.setValue(Short.valueOf((short) 2));
assertEquals((short) 2, mutNum.shortValue());
assertEquals(new Short((short) 2), mutNum.getValue());
assertEquals(Short.valueOf((short) 2), mutNum.getValue());
mutNum.setValue(new MutableShort((short) 3));
assertEquals((short) 3, mutNum.shortValue());
assertEquals(new Short((short) 3), mutNum.getValue());
assertEquals(Short.valueOf((short) 3), mutNum.getValue());
try {
mutNum.setValue(null);
fail();
@ -82,7 +82,7 @@ public void testEquals() {
assertEquals(false, mutNumB.equals(mutNumC));
assertEquals(true, mutNumC.equals(mutNumC));
assertEquals(false, mutNumA.equals(null));
assertEquals(false, mutNumA.equals(new Short((short) 0)));
assertEquals(false, mutNumA.equals(Short.valueOf((short) 0)));
assertEquals(false, mutNumA.equals("0"));
}
@ -94,7 +94,7 @@ public void testHashCode() {
assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode());
assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode());
assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode());
assertEquals(true, mutNumA.hashCode() == new Short((short) 0).hashCode());
assertEquals(true, mutNumA.hashCode() == Short.valueOf((short) 0).hashCode());
}
public void testCompareTo() {
@ -121,8 +121,8 @@ public void testPrimitiveValues() {
}
public void testToShort() {
assertEquals(new Short((short) 0), new MutableShort((short) 0).toShort());
assertEquals(new Short((short) 123), new MutableShort((short) 123).toShort());
assertEquals(Short.valueOf((short) 0), new MutableShort((short) 0).toShort());
assertEquals(Short.valueOf((short) 123), new MutableShort((short) 123).toShort());
}
public void testIncrement() {
@ -150,7 +150,7 @@ public void testAddValuePrimitive() {
public void testAddValueObject() {
MutableShort mutNum = new MutableShort((short) 1);
mutNum.add(new Short((short) 1));
mutNum.add(Short.valueOf((short) 1));
assertEquals((short) 2, mutNum.shortValue());
}
@ -164,7 +164,7 @@ public void testSubtractValuePrimitive() {
public void testSubtractValueObject() {
MutableShort mutNum = new MutableShort((short) 1);
mutNum.subtract(new Short((short) 1));
mutNum.subtract(Short.valueOf((short) 1));
assertEquals((short) 0, mutNum.shortValue());
}