-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add configurable random number generator #135
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,6 @@ | |
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
import com.hubspot.jinjava.doc.annotations.JinjavaDoc; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaParam; | ||
|
@@ -52,7 +51,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar | |
if (size == 0) { | ||
return null; | ||
} | ||
int index = ThreadLocalRandom.current().nextInt(size); | ||
int index = interpreter.getRandom().nextInt(size); | ||
while (index-- > 0) { | ||
it.next(); | ||
} | ||
|
@@ -64,7 +63,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar | |
if (size == 0) { | ||
return null; | ||
} | ||
int index = ThreadLocalRandom.current().nextInt(size); | ||
int index = interpreter.getRandom().nextInt(size); | ||
return Array.get(object, index); | ||
} | ||
// map | ||
|
@@ -75,20 +74,20 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar | |
if (size == 0) { | ||
return null; | ||
} | ||
int index = ThreadLocalRandom.current().nextInt(size); | ||
int index = interpreter.getRandom().nextInt(size); | ||
while (index-- > 0) { | ||
it.next(); | ||
} | ||
return it.next(); | ||
} | ||
// number | ||
if (object instanceof Number) { | ||
return ThreadLocalRandom.current().nextLong(((Number) object).longValue()); | ||
return interpreter.getRandom().nextInt(((Number) object).intValue()); | ||
} | ||
// string | ||
if (object instanceof String) { | ||
try { | ||
return ThreadLocalRandom.current().nextLong(new BigDecimal((String) object).longValue()); | ||
return interpreter.getRandom().nextInt(new BigDecimal((String) object).intValue()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
} catch (Exception e) { | ||
return 0; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.hubspot.jinjava.random; | ||
|
||
import java.util.Random; | ||
import java.util.stream.DoubleStream; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.LongStream; | ||
|
||
/** | ||
* A random number generator that always returns 0. Useful for testing code when you want the output to be constant. | ||
*/ | ||
public class ConstantZeroRandomNumberGenerator extends Random { | ||
|
||
@Override | ||
protected int next(int bits) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int nextInt() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int nextInt(int bound) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public long nextLong() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public boolean nextBoolean() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public float nextFloat() { | ||
return 0f; | ||
} | ||
|
||
@Override | ||
public double nextDouble() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public synchronized double nextGaussian() { | ||
return 0; | ||
} | ||
|
||
@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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.hubspot.jinjava.random; | ||
|
||
public enum RandomNumberGeneratorStrategy { | ||
THREAD_LOCAL, | ||
CONSTANT_ZERO | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,27 +2,31 @@ | |
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown; | ||
import static org.mockito.Mockito.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import static org.mockito.Mockito.when should be enough. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our style is to always collapse imports for Mockito and a few other deps. |
||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.hubspot.jinjava.interpret.JinjavaInterpreter; | ||
import com.hubspot.jinjava.random.ConstantZeroRandomNumberGenerator; | ||
|
||
public class ShuffleFilterTest { | ||
|
||
ShuffleFilter filter; | ||
ShuffleFilter filter = new ShuffleFilter(); | ||
|
||
@Before | ||
public void setup() { | ||
this.filter = new ShuffleFilter(); | ||
} | ||
JinjavaInterpreter interpreter = mock(JinjavaInterpreter.class); | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
public void shuffleItems() { | ||
public void itShufflesItems() { | ||
|
||
when(interpreter.getRandom()).thenReturn(ThreadLocalRandom.current()); | ||
|
||
List<String> before = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9"); | ||
List<String> after = (List<String>) filter.filter(before, null); | ||
List<String> after = (List<String>) filter.filter(before, interpreter); | ||
|
||
assertThat(before).isSorted(); | ||
assertThat(after).containsAll(before); | ||
|
@@ -35,4 +39,19 @@ public void shuffleItems() { | |
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
public void itShufflesConsistentlyWithConstantRandom() { | ||
|
||
when(interpreter.getRandom()).thenReturn(new ConstantZeroRandomNumberGenerator()); | ||
|
||
List<String> before = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9"); | ||
List<String> after = (List<String>) filter.filter(before, interpreter); | ||
|
||
assertThat(before).isSorted(); | ||
assertThat(after).containsAll(before); | ||
|
||
assertThat(after).containsExactly("2", "3", "4", "5", "6", "7", "8", "9", "1"); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why changing long to int here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no
nextLong()
that takes a bound injava.util.Random
. I figured random ints are probably big enough.