1 package io.atlassian.fugue.quickcheck;
2
3 import com.pholser.junit.quickcheck.generator.ComponentizedGenerator;
4 import com.pholser.junit.quickcheck.generator.GenerationStatus;
5 import com.pholser.junit.quickcheck.random.SourceOfRandomness;
6 import io.atlassian.fugue.Either;
7
8 import java.util.List;
9 import java.util.stream.Collectors;
10
11
12
13
14 public class EitherGenerator extends ComponentizedGenerator<Either<?, ?>> {
15
16 @SuppressWarnings("unchecked") public EitherGenerator() {
17 super((Class) Either.class);
18 }
19
20 @Override public Either<?, ?> generate(SourceOfRandomness random, GenerationStatus status) {
21 boolean trial = random.nextBoolean();
22 if (trial) {
23 Object left;
24 do {
25 left = componentGenerators().get(0).generate(random, status);
26 } while (left == null);
27 return Either.left(left);
28 } else {
29 Object right;
30 do {
31 right = componentGenerators().get(1).generate(random, status);
32 } while (right == null);
33 return Either.right(right);
34 }
35 }
36
37 @Override public List<Either<?, ?>> doShrink(SourceOfRandomness random, Either<?, ?> larger) {
38 return larger.fold(left -> componentGenerators().get(0).shrink(random, left).stream().filter(l -> l != null).map(Either::left),
39 right -> componentGenerators().get(1).shrink(random, right).stream().filter(r -> r != null).map(Either::right)).collect(Collectors.toList());
40 }
41
42 @Override public int numberOfNeededComponents() {
43 return 2;
44 }
45
46 }