Implement ThreadLocalRandom, implement some of missing Random methods

This commit is contained in:
Alexey Andreev 2020-04-14 12:07:56 +03:00
parent a074906ea7
commit 061bb859d5
3 changed files with 345 additions and 2 deletions

View File

@ -15,10 +15,19 @@
*/ */
package org.teavm.classlib.java.util; 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.PlatformDetector;
import org.teavm.classlib.java.io.TSerializable; import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.lang.TMath; import org.teavm.classlib.java.lang.TMath;
import org.teavm.classlib.java.lang.TObject; 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.Import;
import org.teavm.interop.Unmanaged; import org.teavm.interop.Unmanaged;
import org.teavm.jso.JSBody; import org.teavm.jso.JSBody;
@ -123,4 +132,306 @@ public class TRandom extends TObject implements TSerializable {
@Import(module = "teavmMath", name = "random") @Import(module = "teavmMath", name = "random")
@Unmanaged @Unmanaged
private static native double random(); 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;
}
}
}
};
}
} }

View File

@ -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();
}
}

View File

@ -17,8 +17,6 @@ package org.teavm.classlib.java.util.concurrent.atomic;
import java.io.Serializable; import java.io.Serializable;
import java.util.function.BinaryOperator; import java.util.function.BinaryOperator;
import java.util.function.LongBinaryOperator;
import java.util.function.LongUnaryOperator;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
@SuppressWarnings("NonAtomicOperationOnVolatileField") @SuppressWarnings("NonAtomicOperationOnVolatileField")