rt/emul/mini/src/main/java/java/lang/Number.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 24 Apr 2016 14:17:09 +0200
changeset 1935 81a7a4fcaf46
parent 791 af4001c85438
permissions -rw-r--r--
Directly referencing base classes as they have to be visible in the mini module
     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.JavaScriptPrototype;
    31 
    32 /**
    33  * The abstract class <code>Number</code> is the superclass of classes
    34  * <code>BigDecimal</code>, <code>BigInteger</code>,
    35  * <code>Byte</code>, <code>Double</code>, <code>Float</code>,
    36  * <code>Integer</code>, <code>Long</code>, and <code>Short</code>.
    37  * <p>
    38  * Subclasses of <code>Number</code> must provide methods to convert
    39  * the represented numeric value to <code>byte</code>, <code>double</code>,
    40  * <code>float</code>, <code>int</code>, <code>long</code>, and
    41  * <code>short</code>.
    42  *
    43  * @author      Lee Boynton
    44  * @author      Arthur van Hoff
    45  * @see     java.lang.Byte
    46  * @see     java.lang.Double
    47  * @see     java.lang.Float
    48  * @see     java.lang.Integer
    49  * @see     java.lang.Long
    50  * @see     java.lang.Short
    51  * @since   JDK1.0
    52  */
    53 @ExtraJavaScript(
    54     resource="/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js",
    55     processByteCode=true
    56 )
    57 @JavaScriptPrototype(container = "Number.prototype", prototype = "new Number")
    58 public abstract class Number implements java.io.Serializable {
    59     /**
    60      * Returns the value of the specified number as an <code>int</code>.
    61      * This may involve rounding or truncation.
    62      *
    63      * @return  the numeric value represented by this object after conversion
    64      *          to type <code>int</code>.
    65      */
    66     @JavaScriptBody(args = {}, body = "return this | 0;")
    67     public abstract int intValue();
    68 
    69     /**
    70      * Returns the value of the specified number as a <code>long</code>.
    71      * This may involve rounding or truncation.
    72      *
    73      * @return  the numeric value represented by this object after conversion
    74      *          to type <code>long</code>.
    75      */
    76     @JavaScriptBody(args = {}, body = "return this.toLong();")
    77     public abstract long longValue();
    78 
    79     /**
    80      * Returns the value of the specified number as a <code>float</code>.
    81      * This may involve rounding.
    82      *
    83      * @return  the numeric value represented by this object after conversion
    84      *          to type <code>float</code>.
    85      */
    86     @JavaScriptBody(args = {}, body = "return this;")
    87     public abstract float floatValue();
    88 
    89     /**
    90      * Returns the value of the specified number as a <code>double</code>.
    91      * This may involve rounding.
    92      *
    93      * @return  the numeric value represented by this object after conversion
    94      *          to type <code>double</code>.
    95      */
    96     @JavaScriptBody(args = {}, body = "return this;")
    97     public abstract double doubleValue();
    98 
    99     /**
   100      * Returns the value of the specified number as a <code>byte</code>.
   101      * This may involve rounding or truncation.
   102      *
   103      * @return  the numeric value represented by this object after conversion
   104      *          to type <code>byte</code>.
   105      * @since   JDK1.1
   106      */
   107     public byte byteValue() {
   108         return (byte)intValue();
   109     }
   110 
   111     /**
   112      * Returns the value of the specified number as a <code>short</code>.
   113      * This may involve rounding or truncation.
   114      *
   115      * @return  the numeric value represented by this object after conversion
   116      *          to type <code>short</code>.
   117      * @since   JDK1.1
   118      */
   119     public short shortValue() {
   120         return (short)intValue();
   121     }
   122 
   123     /** use serialVersionUID from JDK 1.0.2 for interoperability */
   124     private static final long serialVersionUID = -8742448824652078965L;
   125     
   126     static {
   127         // as last step of initialization, initialize valueOf method
   128         initValueOf();
   129     }
   130     @JavaScriptBody(args = {  }, body = 
   131         "var p = java_lang_Number(false);\n" +
   132         "p.valueOf = function() { return this.doubleValue__D(); };\n" +
   133         "p.toString = function() { return this.toString__Ljava_lang_String_2(); };"
   134     )
   135     private native static void initValueOf();
   136 }