-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also defer evaluation when encountering a random value
- Loading branch information
Padraig Farrell
committed
Feb 19, 2019
1 parent
1032c62
commit 95471b6
Showing
6 changed files
with
159 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/main/java/com/hubspot/jinjava/random/DeferredRandomNumberGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.hubspot.jinjava.random; | ||
|
||
import java.util.Random; | ||
import java.util.stream.DoubleStream; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.LongStream; | ||
|
||
import com.hubspot.jinjava.interpret.DeferredValueException; | ||
|
||
/** | ||
* A random number generator that always returns 0. Useful for testing code when you want the output to be constant. | ||
*/ | ||
public class DeferredRandomNumberGenerator extends Random { | ||
|
||
private static final String EXCEPTION_MESSAGE = "Generating random number"; | ||
|
||
@Override | ||
protected int next(int bits) { | ||
throw new DeferredValueException(EXCEPTION_MESSAGE); | ||
} | ||
|
||
@Override | ||
public int nextInt() { | ||
throw new DeferredValueException(EXCEPTION_MESSAGE); | ||
} | ||
|
||
@Override | ||
public int nextInt(int bound) { | ||
throw new DeferredValueException(EXCEPTION_MESSAGE); | ||
} | ||
|
||
@Override | ||
public long nextLong() { | ||
throw new DeferredValueException(EXCEPTION_MESSAGE); | ||
} | ||
|
||
@Override | ||
public boolean nextBoolean() { | ||
throw new DeferredValueException(EXCEPTION_MESSAGE); | ||
} | ||
|
||
@Override | ||
public float nextFloat() { | ||
throw new DeferredValueException(EXCEPTION_MESSAGE); | ||
} | ||
|
||
@Override | ||
public double nextDouble() { | ||
throw new DeferredValueException(EXCEPTION_MESSAGE); | ||
} | ||
|
||
@Override | ||
public synchronized double nextGaussian() { | ||
throw new DeferredValueException(EXCEPTION_MESSAGE); | ||
} | ||
|
||
@Override | ||
public void nextBytes(byte[] bytes) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public IntStream ints(long streamSize) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public IntStream ints() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public IntStream ints(int randomNumberOrigin, int randomNumberBound) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public LongStream longs(long streamSize) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public LongStream longs() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public LongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public LongStream longs(long randomNumberOrigin, long randomNumberBound) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public DoubleStream doubles(long streamSize) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public DoubleStream doubles() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
|
||
public enum RandomNumberGeneratorStrategy { | ||
THREAD_LOCAL, | ||
CONSTANT_ZERO | ||
CONSTANT_ZERO, | ||
DEFERRED | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters