Cut dependency from emul to JsDate. Change-Id: I768c8efdf616edf33a28819c7279cfd5c521bd9f
diff --git a/user/super/com/google/gwt/emul/java/lang/System.java b/user/super/com/google/gwt/emul/java/lang/System.java index 42fcd0e..c120dfa 100644 --- a/user/super/com/google/gwt/emul/java/lang/System.java +++ b/user/super/com/google/gwt/emul/java/lang/System.java
@@ -18,11 +18,10 @@ import static java.internal.InternalPreconditions.checkArrayType; import static java.internal.InternalPreconditions.checkNotNull; -import com.google.gwt.core.client.JsDate; - import java.internal.ArrayHelper; import java.io.PrintStream; +import javaemul.internal.DateUtil; import javaemul.internal.HashCodes; /** @@ -91,7 +90,7 @@ } public static long currentTimeMillis() { - return (long) JsDate.now(); + return (long) DateUtil.now(); } /**
diff --git a/user/super/com/google/gwt/emul/java/util/Date.java b/user/super/com/google/gwt/emul/java/util/Date.java index 2b22e85..88ab7a1 100644 --- a/user/super/com/google/gwt/emul/java/util/Date.java +++ b/user/super/com/google/gwt/emul/java/util/Date.java
@@ -15,10 +15,10 @@ */ package java.util; -import com.google.gwt.core.client.JsDate; - import java.io.Serializable; +import javaemul.internal.JsDate; + /** * Represents a date and time. */ @@ -66,14 +66,6 @@ } /** - * Package private factory for JSNI use, to allow cheap creation of dates from - * doubles. - */ - static Date createFrom(double milliseconds) { - return new Date(milliseconds, false); - } - - /** * JavaScript Date instance. */ private final JsDate jsdate; @@ -105,13 +97,6 @@ this(Date.parse(date)); } - /** - * For use by {@link #createFrom(double)}, should inline away. - */ - Date(double milliseconds, boolean dummyArgForOverloadResolution) { - jsdate = JsDate.create(milliseconds); - } - public boolean after(Date when) { return getTime() > when.getTime(); }
diff --git a/user/super/com/google/gwt/emul/java/util/Random.java b/user/super/com/google/gwt/emul/java/util/Random.java index 40629f4..33d798e 100644 --- a/user/super/com/google/gwt/emul/java/util/Random.java +++ b/user/super/com/google/gwt/emul/java/util/Random.java
@@ -1,12 +1,12 @@ /* * Copyright 2009 Google Inc. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -21,29 +21,29 @@ * licenses this file to You under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. - * + * * INCLUDES MODIFICATIONS BY RICHARD ZSCHECH AS WELL AS GOOGLE. */ package java.util; -import com.google.gwt.core.client.JsDate; - import static java.internal.InternalPreconditions.checkCriticalArgument; import static java.internal.InternalPreconditions.checkNotNull; +import javaemul.internal.DateUtil; + /** * This class provides methods that generates pseudo-random numbers of different * types, such as {@code int}, {@code long}, {@code double}, and {@code float}. * It follows the algorithms specified in the JRE javadoc. - * + * * This emulated version of Random is not serializable. */ public class Random { @@ -77,7 +77,7 @@ twoToTheXMinus48[i] = twoToTheXMinus48Tmp; twoToTheXMinus48Tmp *= 0.5; } - + double twoToTheXMinus24Tmp = 1.0; for (int i = 24; i >= 0; i--) { twoToTheXMinus24[i] = twoToTheXMinus24Tmp; @@ -94,7 +94,7 @@ * The second Gaussian generated number. */ private double nextNextGaussian; - + /** * The high 24 bits of the 48=bit seed value. */ @@ -104,17 +104,17 @@ * The low 24 bits of the 48=bit seed value. */ private double seedlo; - + /** * Construct a random generator with the current time of day in milliseconds * plus a unique counter value as the initial state. - * + * * @see #setSeed */ public Random() { // JsDate.now used instead of System.currentTimeMillis() // as it returns a double in order to avoid the use os LongLib. - double seed = uniqueSeed++ + JsDate.now(); + double seed = uniqueSeed++ + DateUtil.now(); int hi = (int) Math.floor(seed * twoToTheMinus24) & 0xffffff; int lo = (int) (seed - (hi * twoToThe24)); setSeed(hi, lo); @@ -123,7 +123,7 @@ /** * Construct a random generator with the given {@code seed} as the initial * state. - * + * * @param seed the seed that will determine the initial state of this random * number generator. * @see #setSeed @@ -135,7 +135,7 @@ /** * Returns the next pseudo-random, uniformly distributed {@code boolean} value * generated by this generator. - * + * * @return a pseudo-random, uniformly distributed boolean value. */ public boolean nextBoolean() { @@ -145,7 +145,7 @@ /** * Modifies the {@code byte} array by a random sequence of {@code byte}s * generated by this random number generator. - * + * * @param buf non-null array to contain the new random {@code byte}s. * @see #next */ @@ -168,7 +168,7 @@ /** * Generates a normally distributed random {@code double} number between 0.0 * inclusively and 1.0 exclusively. - * + * * @return a random {@code double} in the range [0.0 - 1.0) * @see #nextFloat */ @@ -180,7 +180,7 @@ /** * Generates a normally distributed random {@code float} number between 0.0 * inclusively and 1.0 exclusively. - * + * * @return float a random {@code float} number between [0.0 and 1.0) * @see #nextDouble */ @@ -195,7 +195,7 @@ * Marsaglia, as described by Donald E. Knuth in <i>The Art of Computer * Programming, Volume 2: Seminumerical Algorithms</i>, section 3.4.1, * subsection C, algorithm P. - * + * * @return a random {@code double} * @see #nextDouble */ @@ -205,7 +205,7 @@ haveNextNextGaussian = false; return nextNextGaussian; } - + double v1, v2, s; do { // Generates two independent random variables U1, U2 @@ -225,7 +225,7 @@ /** * Generates a uniformly distributed 32-bit {@code int} value from the random * number sequence. - * + * * @return a uniformly distributed {@code int} value. * @see java.lang.Integer#MAX_VALUE * @see java.lang.Integer#MIN_VALUE @@ -240,7 +240,7 @@ * Returns a new pseudo-random {@code int} value which is uniformly * distributed between 0 (inclusively) and the value of {@code n} * (exclusively). - * + * * @param n the exclusive upper border of the range [0 - n). * @return a random {@code int}. */ @@ -257,11 +257,11 @@ } while (bits - val + (n - 1) < 0); return (int) val; } - + /** * Generates a uniformly distributed 64-bit integer value from the random * number sequence. - * + * * @return 64-bit random integer. * @see java.lang.Integer#MAX_VALUE * @see java.lang.Integer#MIN_VALUE @@ -276,7 +276,7 @@ /** * Modifies the seed a using linear congruential formula presented in <i>The * Art of Computer Programming, Volume 2</i>, Section 3.2.1. - * + * * @param seed the seed that alters the state of the random number generator. * @see #next * @see #Random() @@ -285,13 +285,13 @@ public synchronized void setSeed(long seed) { setSeed((int) ((seed >> 24) & 0xffffff), (int) (seed & 0xffffff)); } - + /** * Returns a pseudo-random uniformly distributed {@code int} value of the * number of bits specified by the argument {@code bits} as described by * Donald E. Knuth in <i>The Art of Computer Programming, Volume 2: * Seminumerical Algorithms</i>, section 3.2.1. - * + * * @param bits number of bits of the returned value. * @return a pseudo-random generated int number. * @see #nextBytes @@ -305,7 +305,7 @@ protected synchronized int next(int bits) { return (int) nextInternal(bits); } - + private synchronized double nextInternal(int bits) { double hi = seedhi * multiplierLo + seedlo * multiplierHi; double lo = seedlo * multiplierLo + 0xb; @@ -313,10 +313,10 @@ hi += carry; lo -= carry * twoToThe24; hi %= twoToThe24; - + seedhi = hi; seedlo = lo; - + if (bits <= 24) { // High bits of seedhi, shifted right return Math.floor(seedhi * twoToTheXMinus24[bits]); @@ -334,7 +334,7 @@ return dval; } } - + // seedhi and seedlo should be integers from 0 to 2^24 - 1 private synchronized void setSeed(int seedhi, int seedlo) { this.seedhi = seedhi ^ 0x5de;
diff --git a/user/super/com/google/gwt/emul/javaemul/internal/DateUtil.java b/user/super/com/google/gwt/emul/javaemul/internal/DateUtil.java new file mode 100644 index 0000000..321bf95 --- /dev/null +++ b/user/super/com/google/gwt/emul/javaemul/internal/DateUtil.java
@@ -0,0 +1,36 @@ +/* + * Copyright 2015 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package javaemul.internal; + +/** + * Simple Helper class to return Date.now. + */ +public class DateUtil { + /** + * Returns the numeric value corresponding to the current time - + * the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC. + */ + public static native double now() /*-{ + // IE8 does not have Date.now + // when removing IE8 support we change this to Date.now() + if (Date.now) { + // Date.now vs Date.getTime() performance comparison: + // http://jsperf.com/date-now-vs-new-date/8 + return Date.now(); + } + return (new Date()).getTime(); + }-*/; +} \ No newline at end of file
diff --git a/user/super/com/google/gwt/emul/javaemul/internal/JsDate.java b/user/super/com/google/gwt/emul/javaemul/internal/JsDate.java new file mode 100644 index 0000000..8c3f439 --- /dev/null +++ b/user/super/com/google/gwt/emul/javaemul/internal/JsDate.java
@@ -0,0 +1,275 @@ +/* + * Copyright 2015 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package javaemul.internal; + +import com.google.gwt.core.client.JavaScriptObject; + +/** + * A simple wrapper around a native JS Date object. + */ +public class JsDate extends JavaScriptObject { + + /** + * Creates a new date with the current time. + */ + public static native JsDate create() /*-{ + return new Date(); + }-*/; + + /** + * Creates a new date with the specified internal representation, which is the + * number of milliseconds since midnight on January 1st, 1970. This is the + * same representation returned by {@link #getTime()}. + */ + public static native JsDate create(double milliseconds) /*-{ + return new Date(milliseconds); + }-*/; + + /** + * Creates a new date using the specified values. + */ + public static native JsDate create(int year, int month, int dayOfMonth, int hours, + int minutes, int seconds, int millis) /*-{ + return new Date(year, month, dayOfMonth, hours, minutes, seconds, millis); + }-*/; + + /** + * Parses a string representation of a date and time and returns the internal + * millisecond representation. If the string cannot be parsed, the returned + * value will be <code>NaN</code>. Use {@link Double#isNaN(double)} to check + * the result. + */ + public static native double parse(String dateString) /*-{ + return Date.parse(dateString); + }-*/; + + // CHECKSTYLE_OFF: Matching the spec. + /** + * Returns the internal millisecond representation of the specified UTC date + * and time. + */ + public static native double UTC(int year, int month, int dayOfMonth, int hours, + int minutes, int seconds, int millis) /*-{ + return Date.UTC(year, month, dayOfMonth, hours, minutes, seconds, millis); + }-*/; + + // CHECKSTYLE_ON + + /** + * Non directly instantiable. + */ + protected JsDate() { + } + + /** + * Returns the day of the month. + */ + public final native int getDate() /*-{ + return this.getDate(); + }-*/; + + /** + * Returns the day of the week, from <code>0</code> (Sunday) to <code>6</code> + * Saturday. + */ + public final native int getDay() /*-{ + return this.getDay(); + }-*/; + + /** + * Returns the four-digit year. + */ + public final native int getFullYear() /*-{ + return this.getFullYear(); + }-*/; + + /** + * Returns the hour, between <code>0</code> (midnight) and <code>23</code>. + */ + public final native int getHours() /*-{ + return this.getHours(); + }-*/; + + /** + * Returns the milliseconds, between <code>0</code> and <code>999</code>. + */ + public final native int getMilliseconds() /*-{ + return this.getMilliseconds(); + }-*/; + + /** + * Returns the minutes, between <code>0</code> and <code>59</code>. + */ + public final native int getMinutes() /*-{ + return this.getMinutes(); + }-*/; + + /** + * Returns the month, from <code>0</code> (January) to <code>11</code> + * December. + */ + public final native int getMonth() /*-{ + return this.getMonth(); + }-*/; + + /** + * Returns the seconds, between <code>0</code> and <code>59</code>. + */ + public final native int getSeconds() /*-{ + return this.getSeconds(); + }-*/; + + /** + * Returns the internal millisecond representation of the date, the number of + * milliseconds since midnight on January 1st, 1970. This is the same + * representation returned by {@link #getTime()}. + */ + public final native double getTime() /*-{ + return this.getTime(); + }-*/; + + /** + * Returns the difference, in minutes, between the local and UTC + * representations of this date. The value returned is affected by whether or + * not daylight savings time would be in effect on specified date. + */ + public final native int getTimezoneOffset() /*-{ + return this.getTimezoneOffset(); + }-*/; + + /** + * Returns the day of the month, in UTC. + */ + public final native int getUTCDate() /*-{ + return this.getUTCDate(); + }-*/; + + /** + * Returns the four-digit year, in UTC. + */ + public final native int getUTCFullYear() /*-{ + return this.getUTCFullYear(); + }-*/; + + /** + * Returns the hour, between <code>0</code> (midnight) and <code>23</code>, in + * UTC. + */ + public final native int getUTCHours() /*-{ + return this.getUTCHours(); + }-*/; + + /** + * Returns the minutes, between <code>0</code> and <code>59</code>, in UTC. + */ + public final native int getUTCMinutes() /*-{ + return this.getUTCMinutes(); + }-*/; + + /** + * Returns the month, from <code>0</code> (January) to <code>11</code> + * December, in UTC. + */ + public final native int getUTCMonth() /*-{ + return this.getUTCMonth(); + }-*/; + + /** + * Returns the seconds, between <code>0</code> and <code>59</code>, in UTC. + */ + public final native int getUTCSeconds() /*-{ + return this.getUTCSeconds(); + }-*/; + + /** + * Sets the day of the month. Returns the millisecond representation of the + * adjusted date. + */ + public final native void setDate(int dayOfMonth) /*-{ + this.setDate(dayOfMonth); + }-*/; + + /** + * Sets the year. Returns the millisecond representation of the adjusted date. + */ + public final native void setFullYear(int year) /*-{ + this.setFullYear(year); + }-*/; + + /** + * Sets the year, month, and day. Returns the millisecond representation of + * the adjusted date. + */ + public final native void setFullYear(int year, int month, int day) /*-{ + this.setFullYear(year, month, day); + }-*/; + + /** + * Sets the hour. Returns the millisecond representation of the adjusted date. + */ + public final native void setHours(int hours) /*-{ + this.setHours(hours); + }-*/; + + /** + * Sets the hour, minutes, seconds, and milliseconds. Returns the millisecond + * representation of the adjusted date. + */ + public final native void setHours(int hours, int mins, int secs, int ms) /*-{ + this.setHours(hours, mins, secs, ms); + }-*/; + + /** + * Sets the minutes. Returns the millisecond representation of the adjusted + * date. + */ + public final native void setMinutes(int minutes) /*-{ + this.setMinutes(minutes); + }-*/; + + /** + * Sets the month. Returns the millisecond representation of the adjusted + * date. + */ + public final native void setMonth(int month) /*-{ + this.setMonth(month); + }-*/; + + /** + * Sets the seconds. Returns the millisecond representation of the adjusted + * date. + */ + public final native void setSeconds(int seconds) /*-{ + this.setSeconds(seconds); + }-*/; + + /** + * Sets the internal date representation. Returns the + * <code>milliseconds</code> argument. + */ + public final native void setTime(double milliseconds) /*-{ + this.setTime(milliseconds); + }-*/; + + /** + * Returns a date and time string in the local time zone according to local + * formatting conventions. + */ + public final native String toLocaleString() /*-{ + return this.toLocaleString(); + }-*/; +} +
diff --git a/user/super/com/google/gwt/emul/javaemul/internal/package-info.java b/user/super/com/google/gwt/emul/javaemul/internal/package-info.java new file mode 100644 index 0000000..5db00ae --- /dev/null +++ b/user/super/com/google/gwt/emul/javaemul/internal/package-info.java
@@ -0,0 +1,25 @@ +/* + * Copyright 2015 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +/** + * Contains internal classes used for GWT's JRE implementation. + * + * <p>The classes in here are not meant to be used by GWT users at all and can change at any time. + * Classes that go in here play an important supporting role in GWT's JRE implementation. They + * should also be mostly transpilable with other Java to JavaScript compilers. This means that they + * can not refer to any GWT specific implementations (no classes outside of the JRE). + */ +package javaemul.internal; \ No newline at end of file