mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
classlib: add cache to Byte and Short valueOf; fix Byte.parseByte (#832)
This commit is contained in:
parent
4b6c4bd3d3
commit
3ac0078e2a
|
@ -21,6 +21,7 @@ public class TByte extends TNumber implements TComparable<TByte> {
|
|||
public static final Class<Byte> TYPE = byte.class;
|
||||
public static final int SIZE = 8;
|
||||
public static final int BYTES = 1;
|
||||
private static TByte[] byteCache = ensureByteCache();
|
||||
private final byte value;
|
||||
|
||||
public TByte(byte value) {
|
||||
|
@ -56,9 +57,16 @@ public class TByte extends TNumber implements TComparable<TByte> {
|
|||
return value;
|
||||
}
|
||||
|
||||
public static TByte valueOf(byte value) {
|
||||
// TODO: add caching
|
||||
return new TByte(value);
|
||||
public static TByte valueOf(byte i) {
|
||||
return byteCache[i + 128];
|
||||
}
|
||||
|
||||
private static TByte[] ensureByteCache() {
|
||||
TByte[] byteCache = new TByte[256];
|
||||
for (int j = 0; j < byteCache.length; ++j) {
|
||||
byteCache[j] = new TByte((byte) (j - 128));
|
||||
}
|
||||
return byteCache;
|
||||
}
|
||||
|
||||
public static String toString(byte value) {
|
||||
|
@ -111,7 +119,7 @@ public class TByte extends TNumber implements TComparable<TByte> {
|
|||
|
||||
public static byte parseByte(String s, int radix) throws TNumberFormatException {
|
||||
int value = TInteger.parseInt(s, radix);
|
||||
if (value < MIN_VALUE || value >= MAX_VALUE) {
|
||||
if (value < MIN_VALUE || value > MAX_VALUE) {
|
||||
throw new TNumberFormatException();
|
||||
}
|
||||
return (byte) value;
|
||||
|
|
|
@ -21,6 +21,7 @@ public class TShort extends TNumber implements TComparable<TShort> {
|
|||
public static final Class<Short> TYPE = short.class;
|
||||
public static final int SIZE = 16;
|
||||
public static final int BYTES = SIZE / Byte.SIZE;
|
||||
private static TShort[] shortCache;
|
||||
private final short value;
|
||||
|
||||
public TShort(short value) {
|
||||
|
@ -56,8 +57,21 @@ public class TShort extends TNumber implements TComparable<TShort> {
|
|||
return value;
|
||||
}
|
||||
|
||||
public static TShort valueOf(short value) {
|
||||
return new TShort(value);
|
||||
public static TShort valueOf(short i) {
|
||||
if (i >= -128 && i <= 127) {
|
||||
ensureShortCache();
|
||||
return shortCache[i + 128];
|
||||
}
|
||||
return new TShort(i);
|
||||
}
|
||||
|
||||
private static void ensureShortCache() {
|
||||
if (shortCache == null) {
|
||||
shortCache = new TShort[256];
|
||||
for (int j = 0; j < shortCache.length; ++j) {
|
||||
shortCache[j] = new TShort((short) (j - 128));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String toString(short value) {
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
package org.teavm.classlib.java.lang;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
@ -48,4 +49,11 @@ public class ByteTest {
|
|||
public void bytes() {
|
||||
assertEquals(1, Byte.BYTES);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cache() {
|
||||
for (int b = Byte.MIN_VALUE; b <= Byte.MAX_VALUE; b++) {
|
||||
assertSame(Byte.valueOf((byte) b), (byte) b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user