rt/emul/mini/src/main/java/java/lang/Number.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 27 Feb 2013 17:50:47 +0100
brancharithmetic
changeset 783 8264f07b1f46
parent 772 d382dacfd73f
child 791 af4001c85438
permissions -rw-r--r--
All JavaScript numbers will have doubleValue__D and co. methods. One can also use valueOf() on any Number to get its primitive value
     1 /*
     2  * Copyright (c) 1994, 2001, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 
    26 package java.lang;
    27 
    28 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
    29 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    30 import org.apidesign.bck2brwsr.core.JavaScriptOnly;
    31 import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
    32 
    33 /**
    34  * The abstract class <code>Number</code> is the superclass of classes
    35  * <code>BigDecimal</code>, <code>BigInteger</code>,
    36  * <code>Byte</code>, <code>Double</code>, <code>Float</code>,
    37  * <code>Integer</code>, <code>Long</code>, and <code>Short</code>.
    38  * <p>
    39  * Subclasses of <code>Number</code> must provide methods to convert
    40  * the represented numeric value to <code>byte</code>, <code>double</code>,
    41  * <code>float</code>, <code>int</code>, <code>long</code>, and
    42  * <code>short</code>.
    43  *
    44  * @author      Lee Boynton
    45  * @author      Arthur van Hoff
    46  * @see     java.lang.Byte
    47  * @see     java.lang.Double
    48  * @see     java.lang.Float
    49  * @see     java.lang.Integer
    50  * @see     java.lang.Long
    51  * @see     java.lang.Short
    52  * @since   JDK1.0
    53  */
    54 @ExtraJavaScript(
    55     resource="/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js",
    56     processByteCode=true
    57 )
    58 @JavaScriptPrototype(container = "Number.prototype", prototype = "new Number")
    59 public abstract class Number implements java.io.Serializable {
    60     /**
    61      * Returns the value of the specified number as an <code>int</code>.
    62      * This may involve rounding or truncation.
    63      *
    64      * @return  the numeric value represented by this object after conversion
    65      *          to type <code>int</code>.
    66      */
    67     @JavaScriptBody(args = {}, body = "return this | 0;")
    68     public abstract int intValue();
    69 
    70     /**
    71      * Returns the value of the specified number as a <code>long</code>.
    72      * This may involve rounding or truncation.
    73      *
    74      * @return  the numeric value represented by this object after conversion
    75      *          to type <code>long</code>.
    76      */
    77     @JavaScriptBody(args = {}, body = "return this.toLong();")
    78     public abstract long longValue();
    79 
    80     /**
    81      * Returns the value of the specified number as a <code>float</code>.
    82      * This may involve rounding.
    83      *
    84      * @return  the numeric value represented by this object after conversion
    85      *          to type <code>float</code>.
    86      */
    87     @JavaScriptBody(args = {}, body = "return this;")
    88     public abstract float floatValue();
    89 
    90     /**
    91      * Returns the value of the specified number as a <code>double</code>.
    92      * This may involve rounding.
    93      *
    94      * @return  the numeric value represented by this object after conversion
    95      *          to type <code>double</code>.
    96      */
    97     @JavaScriptBody(args = {}, body = "return this;")
    98     public abstract double doubleValue();
    99 
   100     /**
   101      * Returns the value of the specified number as a <code>byte</code>.
   102      * This may involve rounding or truncation.
   103      *
   104      * @return  the numeric value represented by this object after conversion
   105      *          to type <code>byte</code>.
   106      * @since   JDK1.1
   107      */
   108     public byte byteValue() {
   109         return (byte)intValue();
   110     }
   111 
   112     /**
   113      * Returns the value of the specified number as a <code>short</code>.
   114      * This may involve rounding or truncation.
   115      *
   116      * @return  the numeric value represented by this object after conversion
   117      *          to type <code>short</code>.
   118      * @since   JDK1.1
   119      */
   120     public short shortValue() {
   121         return (short)intValue();
   122     }
   123 
   124     /** use serialVersionUID from JDK 1.0.2 for interoperability */
   125     private static final long serialVersionUID = -8742448824652078965L;
   126     
   127     static {
   128         // as last step of initialization, initialize valueOf method
   129         initValueOf();
   130     }
   131     @JavaScriptBody(args = {  }, body = 
   132         "var p = vm.java_lang_Number(false);\n" +
   133         "p.valueOf = function() { return this.doubleValue__D(); };\n" +
   134         "p.toString = function() { return this.toString__Ljava_lang_String_2(); };"
   135     )
   136     private native static void initValueOf();
   137 }