2003/11/28 03:06:56
[org.ibex.core.git] / src / org / xwt / js / JS.java
index 957b9ad..28622a3 100644 (file)
@@ -114,12 +114,25 @@ public class JS extends org.xwt.util.BalancedTree {
     public static final Object T = Boolean.TRUE;
     public static final Object F = Boolean.FALSE;
 
-    public static final Number N(int i) { return new Integer(i); }
-    public static final Number N(long l) { return new Long(l); }
-    public static final Number N(double d) { return new Double(d); }
-    public static final Number N(String s) { return new Double(s); }
     public static final Boolean B(boolean b) { return b ? Boolean.TRUE : Boolean.FALSE; }
     public static final Boolean B(int i) { return i==0 ? Boolean.FALSE : Boolean.TRUE; }
+    public static final Number N(String s) { return s.indexOf('.') == -1 ? N(Integer.parseInt(s)) : new Double(s); }
+    public static final Number N(double d) { return (int)d == d ? N((int)d) : new Double(d); }
+    public static final Number N(long l) { return N((int)l); }
+
+    private static final Integer[] smallIntCache = new Integer[65535 / 4];
+    private static final Integer[] largeIntCache = new Integer[65535 / 4];
+    public static final Number N(int i) {
+        Integer ret = null;
+        if (i < smallIntCache.length) { ret = smallIntCache[i]; if (ret != null) return ret; }
+        else ret = largeIntCache[i % largeIntCache.length];
+        if (ret == null || ret.intValue() != i) {
+            ret = new Integer(i);
+            if (i < smallIntCache.length) smallIntCache[i] = ret;
+            else largeIntCache[i % largeIntCache.length] = ret;
+        }
+        return ret;
+    }
     
     private static Enumeration emptyEnumeration = new Enumeration() {
             public boolean hasMoreElements() { return false; }