#162 Allow hash range functions to span single value ranges

This commit is contained in:
Jonathan Shook 2020-06-17 13:32:57 -05:00
parent 21be4c1987
commit 2a7406e0fa
4 changed files with 19 additions and 12 deletions

View File

@ -21,12 +21,11 @@ public class HashRange implements LongToIntFunction {
@Example({"HashRange(35L,39L)","map the input to a number in the range 35-38, inclusive, of type int"}) @Example({"HashRange(35L,39L)","map the input to a number in the range 35-38, inclusive, of type int"})
public HashRange(int minValue, int maxValue) { public HashRange(int minValue, int maxValue) {
this.minValue = minValue; if (maxValue<minValue) {
if (maxValue<=minValue) {
throw new RuntimeException("HashRange must have min and max value in that order."); throw new RuntimeException("HashRange must have min and max value in that order.");
} }
this.width = maxValue - minValue; this.minValue = minValue;
this.width = (maxValue - minValue) +1;
} }
@Override @Override

View File

@ -8,6 +8,9 @@ import java.util.function.LongUnaryOperator;
* Return a value within a range, pseudo-randomly. This is equivalent to * Return a value within a range, pseudo-randomly. This is equivalent to
* returning a value with in range between 0 and some maximum value, but * returning a value with in range between 0 and some maximum value, but
* with a minimum value added. * with a minimum value added.
*
* You can specify hash ranges as small as a single-element range, like
* (5,5), or as wide as the relevant data type allows.
*/ */
@ThreadSafeMapper @ThreadSafeMapper
public class HashRange implements LongUnaryOperator { public class HashRange implements LongUnaryOperator {
@ -22,12 +25,11 @@ public class HashRange implements LongUnaryOperator {
} }
public HashRange(long minValue, long maxValue) { public HashRange(long minValue, long maxValue) {
this.minValue = minValue; if (maxValue<minValue) {
if (maxValue<=minValue) {
throw new RuntimeException("HashRange must have min and max value in that order."); throw new RuntimeException("HashRange must have min and max value in that order.");
} }
this.width = maxValue - minValue; this.minValue = minValue;
this.width = (maxValue - minValue)+1;
} }
@Override @Override

View File

@ -17,12 +17,11 @@ public class HashRange implements IntUnaryOperator {
} }
public HashRange(int minValue, int maxValue) { public HashRange(int minValue, int maxValue) {
this.minValue = minValue; if (maxValue<minValue) {
if (maxValue<=minValue) {
throw new RuntimeException("HashRange must have min and max value in that order."); throw new RuntimeException("HashRange must have min and max value in that order.");
} }
this.width = maxValue - minValue; this.minValue = minValue;
this.width = (maxValue - minValue) +1;
} }
@Override @Override

View File

@ -13,4 +13,11 @@ public class HashRangeTest {
assertThat(hashRange.applyAsLong(32L)).isEqualTo(11L); assertThat(hashRange.applyAsLong(32L)).isEqualTo(11L);
} }
@Test
public void testSingleElementRange() {
HashRange hashRange = new HashRange(33L,33L);
long l = hashRange.applyAsLong(93874L);
assertThat(l).isEqualTo(33L);
}
} }