Skip to content

Instantly share code, notes, and snippets.

@Earthcomputer
Last active April 16, 2018 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Earthcomputer/7e62cc02c629c84c8513c6ba99636ab8 to your computer and use it in GitHub Desktop.
Save Earthcomputer/7e62cc02c629c84c8513c6ba99636ab8 to your computer and use it in GitHub Desktop.
package test;
import java.util.Arrays;
import java.util.Random;
import java.util.stream.Collectors;
public class TestRandom extends Random {
private static final long serialVersionUID = 1L;
public static enum DebugLevel {
OFF, SEED_ONLY, FINE, FINER, FINEST;
public boolean canLog(DebugLevel level) {
return ordinal() >= level.ordinal();
}
}
private DebugLevel debugLevel = DebugLevel.OFF;
private int sequenceCount = 0;
public TestRandom() {
super();
}
public TestRandom(long seed) {
super(seed);
}
public int getSequenceCount() {
return sequenceCount;
}
public void resetSequenceCount() {
sequenceCount = 0;
}
public void setDebugLevel(DebugLevel level) {
this.debugLevel = level;
}
public DebugLevel getDebugLevel() {
return debugLevel;
}
@Override
protected int next(int bits) {
sequenceCount++;
return super.next(bits);
}
@Override
public int nextInt() {
if (debugLevel.canLog(DebugLevel.FINER))
System.out.println(getCaller() + " Random.nextInt()");
return super.nextInt();
}
@Override
public int nextInt(int bound) {
if (debugLevel.canLog(DebugLevel.FINER))
System.out.println(getCaller() + " Random.nextInt(" + bound + ")");
return super.nextInt(bound);
}
@Override
public long nextLong() {
if (debugLevel.canLog(DebugLevel.FINER))
System.out.println(getCaller() + " Random.nextLong()");
return super.nextLong();
}
@Override
public boolean nextBoolean() {
if (debugLevel.canLog(DebugLevel.FINER))
System.out.println(getCaller() + " Random.nextBoolean()");
return super.nextBoolean();
}
@Override
public float nextFloat() {
if (debugLevel.canLog(DebugLevel.FINER))
System.out.println(getCaller() + " Random.nextFloat()");
return super.nextFloat();
}
@Override
public double nextDouble() {
if (debugLevel.canLog(DebugLevel.FINER))
System.out.println(getCaller() + " Random.nextDouble()");
return super.nextDouble();
}
@Override
public synchronized double nextGaussian() {
if (debugLevel.canLog(DebugLevel.FINER))
System.out.println(getCaller() + " Random.nextGaussian()");
return super.nextGaussian();
}
@Override
public void setSeed(long seed) {
if (debugLevel != null && debugLevel.canLog(DebugLevel.SEED_ONLY))
System.out.println(getCaller() + " Random.setSeed(" + seed + ")");
super.setSeed(seed);
resetSequenceCount();
}
private String getCaller() {
if (!debugLevel.canLog(DebugLevel.FINEST))
return "";
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
for (int i = 0; i < stackTrace.length / 2; i++) {
StackTraceElement tmp = stackTrace[i];
stackTrace[i] = stackTrace[stackTrace.length - i - 1];
stackTrace[stackTrace.length - i - 1] = tmp;
}
return Arrays.stream(stackTrace).limit(stackTrace.length - 3).map(String::valueOf)
.collect(Collectors.joining(" ||| "));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment