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