From 061bb859d50c71b0611e9b06ac7445c5a1b9c199 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 14 Apr 2020 12:07:56 +0300 Subject: [PATCH] Implement ThreadLocalRandom, implement some of missing Random methods --- .../org/teavm/classlib/java/util/TRandom.java | 311 ++++++++++++++++++ .../util/concurrent/TThreadLocalRandom.java | 34 ++ .../concurrent/atomic/TAtomicReference.java | 2 - 3 files changed, 345 insertions(+), 2 deletions(-) create mode 100644 classlib/src/main/java/org/teavm/classlib/java/util/concurrent/TThreadLocalRandom.java diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TRandom.java b/classlib/src/main/java/org/teavm/classlib/java/util/TRandom.java index f398d8ab8..da18c4683 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TRandom.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TRandom.java @@ -15,10 +15,19 @@ */ package org.teavm.classlib.java.util; +import java.util.function.DoublePredicate; +import java.util.function.IntPredicate; +import java.util.function.LongPredicate; import org.teavm.classlib.PlatformDetector; import org.teavm.classlib.java.io.TSerializable; import org.teavm.classlib.java.lang.TMath; import org.teavm.classlib.java.lang.TObject; +import org.teavm.classlib.java.util.stream.TDoubleStream; +import org.teavm.classlib.java.util.stream.TIntStream; +import org.teavm.classlib.java.util.stream.TLongStream; +import org.teavm.classlib.java.util.stream.doubleimpl.TSimpleDoubleStreamImpl; +import org.teavm.classlib.java.util.stream.intimpl.TSimpleIntStreamImpl; +import org.teavm.classlib.java.util.stream.longimpl.TSimpleLongStreamImpl; import org.teavm.interop.Import; import org.teavm.interop.Unmanaged; import org.teavm.jso.JSBody; @@ -123,4 +132,306 @@ public class TRandom extends TObject implements TSerializable { @Import(module = "teavmMath", name = "random") @Unmanaged private static native double random(); + + public TIntStream ints(long streamSize) { + if (streamSize < 0) { + throw new IllegalArgumentException(); + } + return new TSimpleIntStreamImpl() { + private long remaining = streamSize; + + @Override + public boolean next(IntPredicate consumer) { + while (remaining > 0) { + --remaining; + if (!consumer.test(nextInt())) { + return true; + } + } + return false; + } + }; + } + + public TIntStream ints() { + return new TSimpleIntStreamImpl() { + @Override + public boolean next(IntPredicate consumer) { + while (consumer.test(nextInt())) { + // go on + } + return true; + } + }; + } + + public TIntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound) { + if (streamSize < 0 || randomNumberOrigin >= randomNumberBound) { + throw new IllegalArgumentException(); + } + + int range = randomNumberBound - randomNumberOrigin; + if (range > 0) { + return new TSimpleIntStreamImpl() { + long remaining = streamSize; + + @Override + public boolean next(IntPredicate consumer) { + while (remaining > 0) { + --remaining; + if (!consumer.test(nextInt(range) + randomNumberOrigin)) { + return true; + } + } + return false; + } + }; + } else { + return new TSimpleIntStreamImpl() { + long remaining = streamSize; + + @Override + public boolean next(IntPredicate consumer) { + while (remaining > 0) { + --remaining; + int n; + do { + n = nextInt(); + } while (n < randomNumberOrigin || n >= randomNumberBound); + if (!consumer.test(n)) { + return true; + } + } + return false; + } + }; + } + } + + public TIntStream ints(int randomNumberOrigin, int randomNumberBound) { + if (randomNumberOrigin >= randomNumberBound) { + throw new IllegalArgumentException(); + } + + int range = randomNumberBound - randomNumberOrigin; + if (range > 0) { + return new TSimpleIntStreamImpl() { + @Override + public boolean next(IntPredicate consumer) { + while (true) { + if (!consumer.test(nextInt(range) + randomNumberOrigin)) { + return true; + } + } + } + }; + } else { + return new TSimpleIntStreamImpl() { + @Override + public boolean next(IntPredicate consumer) { + while (true) { + int n; + do { + n = nextInt(); + } while (n < randomNumberOrigin || n >= randomNumberBound); + if (!consumer.test(n)) { + return true; + } + } + } + }; + } + } + + public TLongStream longs(long streamSize) { + if (streamSize < 0) { + throw new IllegalArgumentException(); + } + return new TSimpleLongStreamImpl() { + private long remaining = streamSize; + + @Override + public boolean next(LongPredicate consumer) { + while (remaining > 0) { + --remaining; + if (!consumer.test(nextLong())) { + return true; + } + } + return false; + } + }; + } + + public TLongStream longs() { + return new TSimpleLongStreamImpl() { + @Override + public boolean next(LongPredicate consumer) { + while (consumer.test(nextLong())) { + // go on + } + return true; + } + }; + } + + private long nextLong(long limit) { + while (true) { + long value = nextLong(); + long result = value % limit; + if (value - result + (limit - 1) < 0) { + return result; + } + } + } + + public TLongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound) { + if (streamSize < 0 || randomNumberOrigin >= randomNumberBound) { + throw new IllegalArgumentException(); + } + + long range = randomNumberBound - randomNumberOrigin; + if (range > 0) { + return new TSimpleLongStreamImpl() { + long remaining = streamSize; + + @Override + public boolean next(LongPredicate consumer) { + while (remaining > 0) { + --remaining; + if (!consumer.test(nextLong(range) + randomNumberOrigin)) { + return true; + } + } + return false; + } + }; + } else { + return new TSimpleLongStreamImpl() { + long remaining = streamSize; + + @Override + public boolean next(LongPredicate consumer) { + while (remaining > 0) { + --remaining; + long n; + do { + n = nextLong(); + } while (n < randomNumberOrigin || n >= randomNumberBound); + if (!consumer.test(n)) { + return true; + } + } + return false; + } + }; + } + } + + public TLongStream longs(long randomNumberOrigin, long randomNumberBound) { + if (randomNumberOrigin >= randomNumberBound) { + throw new IllegalArgumentException(); + } + + long range = randomNumberBound - randomNumberOrigin; + if (range > 0) { + return new TSimpleLongStreamImpl() { + @Override + public boolean next(LongPredicate consumer) { + while (true) { + if (!consumer.test(nextLong(range) + randomNumberOrigin)) { + return true; + } + } + } + }; + } else { + return new TSimpleLongStreamImpl() { + @Override + public boolean next(LongPredicate consumer) { + while (true) { + long n; + do { + n = nextLong(); + } while (n < randomNumberOrigin || n >= randomNumberBound); + if (!consumer.test(n)) { + return true; + } + } + } + }; + } + } + + public TDoubleStream doubles(long streamSize) { + if (streamSize < 0) { + throw new IllegalArgumentException(); + } + return new TSimpleDoubleStreamImpl() { + private long remaining = streamSize; + + @Override + public boolean next(DoublePredicate consumer) { + while (remaining > 0) { + --remaining; + if (!consumer.test(nextDouble())) { + return true; + } + } + return false; + } + }; + } + + public TDoubleStream doubles() { + return new TSimpleDoubleStreamImpl() { + @Override + public boolean next(DoublePredicate consumer) { + while (consumer.test(nextDouble())) { + // go on + } + return true; + } + }; + } + + public TDoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) { + if (streamSize < 0 || randomNumberOrigin >= randomNumberBound) { + throw new IllegalArgumentException(); + } + + double range = randomNumberBound - randomNumberOrigin; + return new TSimpleDoubleStreamImpl() { + long remaining = streamSize; + + @Override + public boolean next(DoublePredicate consumer) { + while (remaining > 0) { + --remaining; + if (!consumer.test(nextDouble() * range + randomNumberOrigin)) { + return true; + } + } + return false; + } + }; + } + + public TDoubleStream doubles(double randomNumberOrigin, double randomNumberBound) { + if (randomNumberOrigin >= randomNumberBound) { + throw new IllegalArgumentException(); + } + + double range = randomNumberBound - randomNumberOrigin; + return new TSimpleDoubleStreamImpl() { + @Override + public boolean next(DoublePredicate consumer) { + while (true) { + if (!consumer.test(nextDouble() * range + randomNumberOrigin)) { + return true; + } + } + } + }; + } } diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/concurrent/TThreadLocalRandom.java b/classlib/src/main/java/org/teavm/classlib/java/util/concurrent/TThreadLocalRandom.java new file mode 100644 index 000000000..57134cab8 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/util/concurrent/TThreadLocalRandom.java @@ -0,0 +1,34 @@ +/* + * Copyright 2020 konsoletyper. + * + * 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 org.teavm.classlib.java.util.concurrent; + +import java.util.Random; + +public class TThreadLocalRandom extends Random { + private static final TThreadLocalRandom current = new TThreadLocalRandom(); + + private TThreadLocalRandom() { + } + + public static TThreadLocalRandom current() { + return current; + } + + @Override + public synchronized void setSeed(long seed) { + throw new UnsupportedOperationException(); + } +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/concurrent/atomic/TAtomicReference.java b/classlib/src/main/java/org/teavm/classlib/java/util/concurrent/atomic/TAtomicReference.java index 8f71b93d3..851490947 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/concurrent/atomic/TAtomicReference.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/concurrent/atomic/TAtomicReference.java @@ -17,8 +17,6 @@ package org.teavm.classlib.java.util.concurrent.atomic; import java.io.Serializable; import java.util.function.BinaryOperator; -import java.util.function.LongBinaryOperator; -import java.util.function.LongUnaryOperator; import java.util.function.UnaryOperator; @SuppressWarnings("NonAtomicOperationOnVolatileField")