ShortRangeGenerator.java

  1. package net.morimekta.providence.testing.generator.extra;

  2. import net.morimekta.providence.testing.generator.GeneratorContext;
  3. import net.morimekta.providence.testing.generator.Generator;

  4. import static java.lang.Math.abs;

  5. /**
  6.  * Default generator for selecting one of a set of values of the same type..
  7.  */
  8. public class ShortRangeGenerator<Context extends GeneratorContext<Context>>
  9.         implements Generator<Context,Short> {
  10.     private final short fromIncluding;
  11.     private final short toExcluding;

  12.     public ShortRangeGenerator(short fromIncluding, short toExcluding) {
  13.         this.fromIncluding = fromIncluding;
  14.         this.toExcluding = toExcluding;

  15.         if (fromIncluding >= toExcluding) {
  16.             throw new AssertionError("Invalid range [ " + fromIncluding + " .. " + toExcluding + " >");
  17.         }
  18.     }

  19.     @Override
  20.     public Short generate(Context ctx) {
  21.         return (short) (fromIncluding + abs(ctx.getRandom().nextInt() % (toExcluding - fromIncluding)));
  22.     }
  23. }