# HG changeset patch # User Jaroslav Tulach # Date 1359377548 -3600 # Node ID d0f57d3ea898f0547d5ba748a92a34b6adf2c4d3 # Parent 41b8defdf15890f44c4009c9b4e671002c401b1d More java classes requested by FX guys diff -r 41b8defdf158 -r d0f57d3ea898 emul/compact/src/main/java/java/util/ArrayDeque.java --- a/emul/compact/src/main/java/java/util/ArrayDeque.java Mon Jan 28 13:30:53 2013 +0100 +++ b/emul/compact/src/main/java/java/util/ArrayDeque.java Mon Jan 28 13:52:28 2013 +0100 @@ -34,6 +34,7 @@ package java.util; import java.io.*; +import org.apidesign.bck2brwsr.emul.lang.System; /** * Resizable-array implementation of the {@link Deque} interface. Array @@ -827,40 +828,4 @@ */ private static final long serialVersionUID = 2340985798034038923L; - /** - * Serialize this deque. - * - * @serialData The current size (int) of the deque, - * followed by all of its elements (each an object reference) in - * first-to-last order. - */ - private void writeObject(ObjectOutputStream s) throws IOException { - s.defaultWriteObject(); - - // Write out size - s.writeInt(size()); - - // Write out elements in order. - int mask = elements.length - 1; - for (int i = head; i != tail; i = (i + 1) & mask) - s.writeObject(elements[i]); - } - - /** - * Deserialize this deque. - */ - private void readObject(ObjectInputStream s) - throws IOException, ClassNotFoundException { - s.defaultReadObject(); - - // Read in size and allocate array - int size = s.readInt(); - allocateElements(size); - head = 0; - tail = size; - - // Read in all elements in the proper order. - for (int i = 0; i < size; i++) - elements[i] = (E)s.readObject(); - } } diff -r 41b8defdf158 -r d0f57d3ea898 emul/compact/src/main/java/java/util/Collections.java --- a/emul/compact/src/main/java/java/util/Collections.java Mon Jan 28 13:30:53 2013 +0100 +++ b/emul/compact/src/main/java/java/util/Collections.java Mon Jan 28 13:52:28 2013 +0100 @@ -25,9 +25,9 @@ package java.util; import java.io.Serializable; -import java.io.ObjectOutputStream; import java.io.IOException; import java.lang.reflect.Array; +import org.apidesign.bck2brwsr.emul.lang.System; /** * This class consists exclusively of static methods that operate on or return @@ -1655,9 +1655,6 @@ public String toString() { synchronized (mutex) {return c.toString();} } - private void writeObject(ObjectOutputStream s) throws IOException { - synchronized (mutex) {s.defaultWriteObject();} - } } /** @@ -2081,9 +2078,6 @@ public String toString() { synchronized (mutex) {return m.toString();} } - private void writeObject(ObjectOutputStream s) throws IOException { - synchronized (mutex) {s.defaultWriteObject();} - } } /** @@ -3906,12 +3900,6 @@ private static final long serialVersionUID = 2454657854757543876L; - private void readObject(java.io.ObjectInputStream stream) - throws IOException, ClassNotFoundException - { - stream.defaultReadObject(); - s = m.keySet(); - } } /** diff -r 41b8defdf158 -r d0f57d3ea898 emul/compact/src/main/java/java/util/Hashtable.java --- a/emul/compact/src/main/java/java/util/Hashtable.java Mon Jan 28 13:30:53 2013 +0100 +++ b/emul/compact/src/main/java/java/util/Hashtable.java Mon Jan 28 13:52:28 2013 +0100 @@ -836,116 +836,6 @@ } /** - * Save the state of the Hashtable to a stream (i.e., serialize it). - * - * @serialData The capacity of the Hashtable (the length of the - * bucket array) is emitted (int), followed by the - * size of the Hashtable (the number of key-value - * mappings), followed by the key (Object) and value (Object) - * for each key-value mapping represented by the Hashtable - * The key-value mappings are emitted in no particular order. - */ - private void writeObject(java.io.ObjectOutputStream s) - throws IOException { - Entry entryStack = null; - - synchronized (this) { - // Write out the length, threshold, loadfactor - s.defaultWriteObject(); - - // Write out length, count of elements - s.writeInt(table.length); - s.writeInt(count); - - // Stack copies of the entries in the table - for (int index = 0; index < table.length; index++) { - Entry entry = table[index]; - - while (entry != null) { - entryStack = - new Entry<>(0, entry.key, entry.value, entryStack); - entry = entry.next; - } - } - } - - // Write out the key/value objects from the stacked entries - while (entryStack != null) { - s.writeObject(entryStack.key); - s.writeObject(entryStack.value); - entryStack = entryStack.next; - } - } - - /** - * Reconstitute the Hashtable from a stream (i.e., deserialize it). - */ - private void readObject(java.io.ObjectInputStream s) - throws IOException, ClassNotFoundException - { - // Read in the length, threshold, and loadfactor - s.defaultReadObject(); - - // Read the original length of the array and number of elements - int origlength = s.readInt(); - int elements = s.readInt(); - - // Compute new size with a bit of room 5% to grow but - // no larger than the original size. Make the length - // odd if it's large enough, this helps distribute the entries. - // Guard against the length ending up zero, that's not valid. - int length = (int)(elements * loadFactor) + (elements / 20) + 3; - if (length > elements && (length & 1) == 0) - length--; - if (origlength > 0 && length > origlength) - length = origlength; - - Entry[] table = new Entry[length]; - count = 0; - - // Read the number of elements and then all the key/value objects - for (; elements > 0; elements--) { - K key = (K)s.readObject(); - V value = (V)s.readObject(); - // synch could be eliminated for performance - reconstitutionPut(table, key, value); - } - this.table = table; - } - - /** - * The put method used by readObject. This is provided because put - * is overridable and should not be called in readObject since the - * subclass will not yet be initialized. - * - *

This differs from the regular put method in several ways. No - * checking for rehashing is necessary since the number of elements - * initially in the table is known. The modCount is not incremented - * because we are creating a new instance. Also, no return value - * is needed. - */ - private void reconstitutionPut(Entry[] tab, K key, V value) - throws StreamCorruptedException - { - if (value == null) { - throw new java.io.StreamCorruptedException(); - } - // Makes sure the key is not already in the hashtable. - // This should not happen in deserialized version. - int hash = key.hashCode(); - int index = (hash & 0x7FFFFFFF) % tab.length; - for (Entry e = tab[index] ; e != null ; e = e.next) { - if ((e.hash == hash) && e.key.equals(key)) { - throw new java.io.StreamCorruptedException(); - } - } - // Creates the new entry. - Entry e = tab[index]; - tab[index] = new Entry<>(hash, key, value, e); - count++; - } - - /** * Hashtable collision list. */ private static class Entry implements Map.Entry { diff -r 41b8defdf158 -r d0f57d3ea898 emul/compact/src/main/java/java/util/LinkedList.java --- a/emul/compact/src/main/java/java/util/LinkedList.java Mon Jan 28 13:30:53 2013 +0100 +++ b/emul/compact/src/main/java/java/util/LinkedList.java Mon Jan 28 13:52:28 2013 +0100 @@ -1097,42 +1097,4 @@ private static final long serialVersionUID = 876323262645176354L; - /** - * Saves the state of this {@code LinkedList} instance to a stream - * (that is, serializes it). - * - * @serialData The size of the list (the number of elements it - * contains) is emitted (int), followed by all of its - * elements (each an Object) in the proper order. - */ - private void writeObject(java.io.ObjectOutputStream s) - throws java.io.IOException { - // Write out any hidden serialization magic - s.defaultWriteObject(); - - // Write out size - s.writeInt(size); - - // Write out all elements in the proper order. - for (Node x = first; x != null; x = x.next) - s.writeObject(x.item); - } - - /** - * Reconstitutes this {@code LinkedList} instance from a stream - * (that is, deserializes it). - */ - @SuppressWarnings("unchecked") - private void readObject(java.io.ObjectInputStream s) - throws java.io.IOException, ClassNotFoundException { - // Read in any hidden serialization magic - s.defaultReadObject(); - - // Read in size - int size = s.readInt(); - - // Read in all elements in the proper order. - for (int i = 0; i < size; i++) - linkLast((E)s.readObject()); - } } diff -r 41b8defdf158 -r d0f57d3ea898 emul/compact/src/main/java/java/util/Random.java --- a/emul/compact/src/main/java/java/util/Random.java Mon Jan 28 13:30:53 2013 +0100 +++ b/emul/compact/src/main/java/java/util/Random.java Mon Jan 28 13:52:28 2013 +0100 @@ -24,9 +24,8 @@ */ package java.util; -import java.io.*; -import java.util.concurrent.atomic.AtomicLong; -import sun.misc.Unsafe; + +import org.apidesign.bck2brwsr.emul.lang.System; /** * An instance of this class is used to generate a stream of @@ -75,7 +74,7 @@ * (The specs for the methods in this class describe the ongoing * computation of this value.) */ - private final AtomicLong seed; + private long seed; private static final long multiplier = 0x5DEECE66DL; private static final long addend = 0xBL; @@ -89,20 +88,17 @@ public Random() { this(seedUniquifier() ^ System.nanoTime()); } - - private static long seedUniquifier() { + + private static synchronized long seedUniquifier() { // L'Ecuyer, "Tables of Linear Congruential Generators of // Different Sizes and Good Lattice Structure", 1999 - for (;;) { - long current = seedUniquifier.get(); - long next = current * 181783497276652981L; - if (seedUniquifier.compareAndSet(current, next)) - return next; - } + long current = seedUniquifier; + long next = current * 181783497276652981L; + seedUniquifier = next; + return next; } - private static final AtomicLong seedUniquifier - = new AtomicLong(8682522807148012L); + private static long seedUniquifier = 8682522807148012L; /** * Creates a new random number generator using a single {@code long} seed. @@ -118,7 +114,7 @@ * @see #setSeed(long) */ public Random(long seed) { - this.seed = new AtomicLong(initialScramble(seed)); + this.seed = initialScramble(seed); } private static long initialScramble(long seed) { @@ -145,7 +141,7 @@ * @param seed the initial seed */ synchronized public void setSeed(long seed) { - this.seed.set(initialScramble(seed)); + this.seed = initialScramble(seed); haveNextNextGaussian = false; } @@ -174,13 +170,12 @@ * generator's sequence * @since 1.1 */ - protected int next(int bits) { + protected synchronized int next(int bits) { long oldseed, nextseed; - AtomicLong seed = this.seed; - do { - oldseed = seed.get(); - nextseed = (oldseed * multiplier + addend) & mask; - } while (!seed.compareAndSet(oldseed, nextseed)); + long seed = this.seed; + oldseed = seed; + nextseed = (oldseed * multiplier + addend) & mask; + this.seed = nextseed; return (int)(nextseed >>> (48 - bits)); } @@ -499,77 +494,10 @@ v2 = 2 * nextDouble() - 1; // between -1 and 1 s = v1 * v1 + v2 * v2; } while (s >= 1 || s == 0); - double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); + double multiplier = Math.sqrt(-2 * Math.log(s)/s); nextNextGaussian = v2 * multiplier; haveNextNextGaussian = true; return v1 * multiplier; } } - - /** - * Serializable fields for Random. - * - * @serialField seed long - * seed for random computations - * @serialField nextNextGaussian double - * next Gaussian to be returned - * @serialField haveNextNextGaussian boolean - * nextNextGaussian is valid - */ - private static final ObjectStreamField[] serialPersistentFields = { - new ObjectStreamField("seed", Long.TYPE), - new ObjectStreamField("nextNextGaussian", Double.TYPE), - new ObjectStreamField("haveNextNextGaussian", Boolean.TYPE) - }; - - /** - * Reconstitute the {@code Random} instance from a stream (that is, - * deserialize it). - */ - private void readObject(java.io.ObjectInputStream s) - throws java.io.IOException, ClassNotFoundException { - - ObjectInputStream.GetField fields = s.readFields(); - - // The seed is read in as {@code long} for - // historical reasons, but it is converted to an AtomicLong. - long seedVal = fields.get("seed", -1L); - if (seedVal < 0) - throw new java.io.StreamCorruptedException( - "Random: invalid seed"); - resetSeed(seedVal); - nextNextGaussian = fields.get("nextNextGaussian", 0.0); - haveNextNextGaussian = fields.get("haveNextNextGaussian", false); - } - - /** - * Save the {@code Random} instance to a stream. - */ - synchronized private void writeObject(ObjectOutputStream s) - throws IOException { - - // set the values of the Serializable fields - ObjectOutputStream.PutField fields = s.putFields(); - - // The seed is serialized as a long for historical reasons. - fields.put("seed", seed.get()); - fields.put("nextNextGaussian", nextNextGaussian); - fields.put("haveNextNextGaussian", haveNextNextGaussian); - - // save them - s.writeFields(); - } - - // Support for resetting seed while deserializing - private static final Unsafe unsafe = Unsafe.getUnsafe(); - private static final long seedOffset; - static { - try { - seedOffset = unsafe.objectFieldOffset - (Random.class.getDeclaredField("seed")); - } catch (Exception ex) { throw new Error(ex); } - } - private void resetSeed(long seedVal) { - unsafe.putObjectVolatile(this, seedOffset, new AtomicLong(seedVal)); - } } diff -r 41b8defdf158 -r d0f57d3ea898 emul/compact/src/main/java/java/util/Vector.java --- a/emul/compact/src/main/java/java/util/Vector.java Mon Jan 28 13:30:53 2013 +0100 +++ b/emul/compact/src/main/java/java/util/Vector.java Mon Jan 28 13:52:28 2013 +0100 @@ -25,6 +25,8 @@ package java.util; +import org.apidesign.bck2brwsr.emul.lang.System; + /** * The {@code Vector} class implements a growable array of * objects. Like an array, it contains components that can be @@ -1055,25 +1057,6 @@ } /** - * Save the state of the {@code Vector} instance to a stream (that - * is, serialize it). - * This method performs synchronization to ensure the consistency - * of the serialized data. - */ - private void writeObject(java.io.ObjectOutputStream s) - throws java.io.IOException { - final java.io.ObjectOutputStream.PutField fields = s.putFields(); - final Object[] data; - synchronized (this) { - fields.put("capacityIncrement", capacityIncrement); - fields.put("elementCount", elementCount); - data = elementData.clone(); - } - fields.put("elementData", data); - s.writeFields(); - } - - /** * Returns a list iterator over the elements in this list (in proper * sequence), starting at the specified position in the list. * The specified index indicates the first element that would be diff -r 41b8defdf158 -r d0f57d3ea898 emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/CollectionsTest.java --- a/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/CollectionsTest.java Mon Jan 28 13:30:53 2013 +0100 +++ b/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/CollectionsTest.java Mon Jan 28 13:52:28 2013 +0100 @@ -25,6 +25,8 @@ import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Map.Entry; +import java.util.Vector; import org.apidesign.bck2brwsr.vmtest.Compare; import org.apidesign.bck2brwsr.vmtest.VMTest; import org.testng.annotations.Factory; @@ -92,10 +94,9 @@ map.put("nine", 9); map.put("ten", 10); - Map.Entry[] arr = map.entrySet().toArray(new Map.Entry[map.size()]); - Arrays.sort(arr, new C()); - - return Arrays.asList(arr).toString(); + List> arr = new Vector<>(); + arr.addAll(map.entrySet()); + return arr.toString(); } @Factory diff -r 41b8defdf158 -r d0f57d3ea898 emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/RandomTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/RandomTest.java Mon Jan 28 13:52:28 2013 +0100 @@ -0,0 +1,40 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.compact.tck; + +import java.util.Random; +import org.apidesign.bck2brwsr.vmtest.Compare; +import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.testng.annotations.Factory; + +/** + * + * @author Jaroslav Tulach + */ +public class RandomTest { + @Compare public boolean canInstantiateRandom() { + Random r = new Random(); + r.nextInt(); + return r != null; + } + + + @Factory public static Object[] create() { + return VMTest.create(RandomTest.class); + } +} diff -r 41b8defdf158 -r d0f57d3ea898 emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/lang/System.java --- a/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/lang/System.java Mon Jan 28 13:30:53 2013 +0100 +++ b/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/lang/System.java Mon Jan 28 13:52:28 2013 +0100 @@ -44,5 +44,7 @@ "while (expectedSize-- > arr.length) { arr.push(0); }; return arr;" ) public static native byte[] expandArray(byte[] arr, int expectedSize); - + + @JavaScriptBody(args = {}, body = "new Date().getMilliseconds() * 1000;") + public static native long nanoTime(); }