001 
002 /*
003  *  JScripter Emulation 1.0 - To Script Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package org.jscripter.emu.java.lang;
021 
022 import org.jscripter.emu.java.JavaObject;
023 
024 import js.*;
025 import js.core.JsDate;
026 import js.core.JsGlobal;
027 
028 /**
029  * <p>An <b>Internal</b> utility class containing several useful fields and methods
030  * about the underlying system, emulating a standard <tt>java.lang</tt>
031  * interface or class with the same simple name as this one.</p>
032  * <p>This interface or class is only used internally by JS re-compiler implementations.</p>
033  * <p>Please refer to <a href="http://java.sun.com/docs/">the Java API Standards</a> for detail description of the original class or interface.</p>
034  * 
035  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
036  * 
037  * @javascript Re-compilers must redirect the resolution of the emulated original Java class or interface to this one.
038  */
039 public final class System
040 {
041     /**
042      * <p>Copies an array from the specified source array, beginning at the
043      * specified position, to the specified position of the destination array.</p>
044      * @param src The source array.
045      * @param srcPos Starting position in the source array.
046      * @param dest The destination array.
047      * @param destPos Starting position in the destination data.
048      * @param length The number of array elements to be copied.
049      * @throws java.lang.IndexOutOfBoundsException if copying would cause access of data 
050      * outside array bounds.
051      * @throws java.lang.ArrayStoreException if an element in the <tt>src</tt> array could not 
052      * be storedinto the <tt>dest</tt> array because of a type mismatch.
053      * @throws java.lang.NullPointerException if either <tt>src</tt> or <tt>dest</tt> is <tt>null</tt>.
054      * @since 1.0
055      * @javascript Re-compilers must report error on end-users directly using this method.
056      */
057     @SuppressWarnings("unchecked")
058     public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) {
059         for (int i = 0; i < length; i++) {
060             ((ArrayLike<Object>)dest).set(destPos + i, ((ArrayLike<Object>)src).get(srcPos + i));
061         }
062     }
063 
064     /**
065      * <p>Terminates the current execution.</p>
066      * <p>This method just closes the current window in for emulation.</p>
067      * @param status The exit status. This argument is simply ignored in the emulation mode.
068      * @since 1.0
069      * @javascript Re-compilers must report error on end-users directly using this method.
070      */
071     public static void exit(int status) {
072         Js.win().close();
073     }
074 
075     private final static int booleanHashCode(java.lang.Object o) {
076         return ((Boolean) o).hashCode();
077     }
078 
079     private final static int numberHashCode(java.lang.Object o) {
080         return ((Double) o).hashCode();
081     }
082 
083     private final static int stringHashCode(java.lang.Object o) {
084         return ((String) o).hashCode();
085     }
086 
087     /**
088      * <p>Returns the same hash code for the given object as would be returned by the default method 
089      * <tt>hashCode()</tt>, whether or not the given object's class overrides {@link Object#hashCode()}.
090      * The hash code for the <tt>null</tt> reference is zero.</p>
091      * @param o An object for which the hash code is to be calculated.
092      * @return The hash code.
093      * @since 1.0
094      * @javascript Re-compilers must report error on end-users directly using this method.
095      */
096     public final static int identityHashCode(java.lang.Object o) {
097         switch (Js.cases().add("boolean").add("number").add("string")
098                 .add("object").indexOf(Js.typeof(o))) {
099         case 0:
100             return booleanHashCode(o);
101         case 1:
102             return numberHashCode(o);
103         case 2:
104             return stringHashCode(o);
105         case 3:
106             if (Js.instanceOf(o, JsGlobal.Date.with())) {
107                 return ((JsDate) o).valueOf().hashCode();
108             } else {
109                 return JavaObject.hashCode(o);
110             }
111         default:
112             return 0;
113         }
114     }
115 
116     /**
117      * <p>Returns the current time in milliseconds.</p>
118      * @return The difference, measured in milliseconds, between the current time and 
119      * midnight, January 1, 1970 UTC.
120      * @since 1.0
121      * @javascript Re-compilers must report error on end-users directly using this method.
122      */
123     public final static long currentTimeMillis() {
124         return JsGlobal.Date.with().create().valueOf().longValue();
125     }
126 
127     /**
128      * <p>Runs the garbage collector.</p>
129      * <p>This method does nothing in emulation.</p>
130      * @since 1.0
131      * @javascript Re-compilers must report error on end-users directly using this method.
132      */
133     public static void gc() {}
134 
135     /**
136      * <p>Runs the finalization methods of any objects pending finalization.</p>
137      * <p>This method does nothing in emulation.</p>
138      * @since 1.0
139      * @javascript Re-compilers must report error on end-users directly using this method.
140      */
141     public static void runFinalization() {}
142 }