changeset 7548:74949845c660

restore lambda bits lost in jsr166 merge Contributed-by: Akhil Arora <akhil.arora@oracle.com>
author mduigou
date Thu, 28 Feb 2013 12:59:41 -0800
parents 9ccdccc3c754
children 3f73ae4dc13f
files src/share/classes/java/util/concurrent/ThreadLocalRandom.java test-ng/tests/org/openjdk/tests/java/util/RandomStreamTest.java
diffstat 2 files changed, 24 insertions(+), 80 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/concurrent/ThreadLocalRandom.java	Thu Feb 28 18:35:14 2013 +0100
+++ b/src/share/classes/java/util/concurrent/ThreadLocalRandom.java	Thu Feb 28 12:59:41 2013 -0800
@@ -35,10 +35,13 @@
 
 package java.util.concurrent;
 
-import java.io.ObjectStreamField;
 import java.util.Random;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.stream.DoubleStream;
+import java.util.stream.IntStream;
+import java.util.stream.LongStream;
+import java.util.stream.Streams;
 
 /**
  * A random number generator isolated to the current thread.  Like the
@@ -242,6 +245,26 @@
         return offset + nextInt((int) n);
     }
 
+    @Override
+    public IntStream ints() {
+        return Streams.generateInt(() -> current().nextInt());
+    }
+
+    @Override
+    public LongStream longs() {
+        return Streams.generateLong(() -> current().nextLong());
+    }
+
+    @Override
+    public DoubleStream doubles() {
+        return Streams.generateDouble(() -> current().nextDouble());
+    }
+
+    @Override
+    public DoubleStream gaussians() {
+        return Streams.generateDouble(() -> current().nextGaussian());
+    }
+
     /**
      * Returns a pseudorandom, uniformly distributed value between the
      * given least value (inclusive) and bound (exclusive).
--- a/test-ng/tests/org/openjdk/tests/java/util/RandomStreamTest.java	Thu Feb 28 18:35:14 2013 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,79 +0,0 @@
-/*
- * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code 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
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package org.openjdk.tests.java.util;
-
-import org.testng.annotations.Test;
-
-import java.util.Arrays;
-import java.util.Random;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ThreadLocalRandom;
-import java.util.concurrent.TimeUnit;
-
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertFalse;
-
-/**
- * RandomStreamTest
- *
- * @author Brian Goetz
- */
-@Test
-public class RandomStreamTest {
-    public void testIntStream() {
-        long seed = System.currentTimeMillis();
-        Random r = new Random(seed);
-        int[] a = new int[100];
-        for (int i=0; i<100; i++)
-            a[i] = r.nextInt();
-
-        r = new Random(seed); // same seed
-        int[] b = r.ints().limit(100).toArray();
-        assertEquals(a, b);
-    }
-
-    public void testThreadLocalIntStream() throws InterruptedException {
-        ExecutorService e = Executors.newFixedThreadPool(10);
-
-        class RandomTask implements Runnable {
-            int[] randoms;
-
-            @Override
-            public void run() {
-                randoms = ThreadLocalRandom.current().ints().limit(100).toArray();
-            }
-        }
-        RandomTask[] tasks = new RandomTask[10];
-        for (int i=0; i<10; i++)
-            tasks[i] = new RandomTask();
-        for (int i=0; i<10; i++)
-            e.submit(tasks[i]);
-        e.shutdown();
-        e.awaitTermination(3, TimeUnit.SECONDS);
-        for (int i=1; i<10; i++)
-            assertFalse(Arrays.equals(tasks[0].randoms, tasks[i].randoms));
-    }
-}