Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added SourceOfRandomness#oneOf() method

  • Loading branch information...
commit 15337d2792e8ddacd19258a886cb73469890866b 1 parent bbc6bbd
Marc Philipp authored
118 src/main/java/com/pholser/junit/parameters/random/AbstractJDKSourceOfRandomness.java
@@ -3,61 +3,65 @@
3 3 import java.util.Random;
4 4
5 5 abstract class AbstractJDKSourceOfRandomness implements SourceOfRandomness {
6   - private final Random random;
7   -
8   - protected AbstractJDKSourceOfRandomness(Random random) {
9   - this.random = random;
10   - }
11   -
12   - @Override
13   - public boolean nextBoolean() {
14   - return random.nextBoolean();
15   - }
16   -
17   - @Override
18   - public void nextBytes(byte[] bytes) {
19   - random.nextBytes(bytes);
20   - }
21   -
22   - @Override
23   - public byte[] nextBytes(int length) {
24   - byte[] bytes = new byte[length];
25   - nextBytes(bytes);
26   - return bytes;
27   - }
28   -
29   - @Override
30   - public double nextDouble() {
31   - return random.nextDouble();
32   - }
33   -
34   - @Override
35   - public float nextFloat() {
36   - return random.nextFloat();
37   - }
38   -
39   - @Override
40   - public double nextGaussian() {
41   - return random.nextGaussian();
42   - }
43   -
44   - @Override
45   - public int nextInt() {
46   - return random.nextInt();
47   - }
48   -
49   - @Override
50   - public int nextInt(int n) {
51   - return random.nextInt(n);
52   - }
53   -
54   - @Override
55   - public long nextLong() {
56   - return random.nextLong();
57   - }
58   -
59   - @Override
60   - public void setSeed(long seed) {
61   - random.setSeed(seed);
62   - }
  6 + private final Random random;
  7 +
  8 + protected AbstractJDKSourceOfRandomness(Random random) {
  9 + this.random = random;
  10 + }
  11 +
  12 + @Override
  13 + public boolean nextBoolean() {
  14 + return random.nextBoolean();
  15 + }
  16 +
  17 + @Override
  18 + public void nextBytes(byte[] bytes) {
  19 + random.nextBytes(bytes);
  20 + }
  21 +
  22 + @Override
  23 + public byte[] nextBytes(int length) {
  24 + byte[] bytes = new byte[length];
  25 + nextBytes(bytes);
  26 + return bytes;
  27 + }
  28 +
  29 + @Override
  30 + public double nextDouble() {
  31 + return random.nextDouble();
  32 + }
  33 +
  34 + @Override
  35 + public float nextFloat() {
  36 + return random.nextFloat();
  37 + }
  38 +
  39 + @Override
  40 + public double nextGaussian() {
  41 + return random.nextGaussian();
  42 + }
  43 +
  44 + @Override
  45 + public int nextInt() {
  46 + return random.nextInt();
  47 + }
  48 +
  49 + @Override
  50 + public int nextInt(int n) {
  51 + return random.nextInt(n);
  52 + }
  53 +
  54 + @Override
  55 + public long nextLong() {
  56 + return random.nextLong();
  57 + }
  58 +
  59 + @Override
  60 + public void setSeed(long seed) {
  61 + random.setSeed(seed);
  62 + }
  63 +
  64 + public <T> T oneOf(T... items) {
  65 + return items[nextInt(items.length)];
  66 + }
63 67 }
3  src/main/java/com/pholser/junit/parameters/random/SourceOfRandomness.java
... ... @@ -1,6 +1,7 @@
1 1 package com.pholser.junit.parameters.random;
2 2
3 3 public interface SourceOfRandomness {
  4 +
4 5 boolean nextBoolean();
5 6
6 7 void nextBytes(byte[] bytes);
@@ -20,4 +21,6 @@
20 21 long nextLong();
21 22
22 23 void setSeed(long seed);
  24 +
  25 + <T> T oneOf(T... items);
23 26 }

0 comments on commit 15337d2

Please sign in to comment.
Something went wrong with that request. Please try again.