Skip to content

Commit

Permalink
Move NullAlowed to non-internal pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
pholser committed Nov 21, 2020
1 parent b3eddb0 commit c91e25f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pholser.junit.quickcheck.internal.generator;
package com.pholser.junit.quickcheck.generator;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
Expand All @@ -20,5 +20,5 @@
/**
* @return probability of generating {@code null}, in the range [0,1]
*/
float probability() default 0.2f;
double probability() default 0.2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ a copy of this software and associated documentation files (the
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

import com.pholser.junit.quickcheck.generator.Ctor;
import com.pholser.junit.quickcheck.generator.Fields;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.generator.Generators;
import com.pholser.junit.quickcheck.generator.*;
import com.pholser.junit.quickcheck.internal.ParameterTypeContext;
import com.pholser.junit.quickcheck.internal.Weighted;
import com.pholser.junit.quickcheck.internal.Zilch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ a copy of this software and associated documentation files (the
import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.generator.Generators;
import com.pholser.junit.quickcheck.generator.NullAllowed;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.AnnotatedType;
Expand All @@ -39,11 +40,12 @@ a copy of this software and associated documentation files (the

class NullableGenerator<T> extends Generator<T> {
private final Generator<T> delegate;
private float probabilityOfNull =
(Float) defaultValueOf(NullAllowed.class, "probability");
private double probabilityOfNull =
(Double) defaultValueOf(NullAllowed.class, "probability");

NullableGenerator(Generator<T> delegate) {
super(delegate.types());

this.delegate = delegate;
}

Expand All @@ -56,15 +58,6 @@ class NullableGenerator<T> extends Generator<T> {
: delegate.generate(random, status);
}

private void configure(NullAllowed nullAllowed) {
if (nullAllowed.probability() >= 0 && nullAllowed.probability() <= 1) {
this.probabilityOfNull = nullAllowed.probability();
} else {
throw new IllegalArgumentException(
"NullAllowed probability must be in the range [0, 1]");
}
}

@Override public boolean canRegisterAsType(Class<?> type) {
return delegate.canRegisterAsType(type);
}
Expand Down Expand Up @@ -115,4 +108,13 @@ private void configure(NullAllowed nullAllowed) {
@Override public BigDecimal magnitude(Object value) {
return delegate.magnitude(value);
}

private void configure(NullAllowed allowed) {
if (allowed.probability() >= 0 && allowed.probability() <= 1) {
this.probabilityOfNull = allowed.probability();
} else {
throw new IllegalArgumentException(
"NullAllowed probability must be in the range [0, 1]");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ a copy of this software and associated documentation files (the
import com.pholser.junit.quickcheck.Property;
import com.pholser.junit.quickcheck.examples.number.NonNegative;
import com.pholser.junit.quickcheck.generator.java.lang.IntegerGenerator;
import com.pholser.junit.quickcheck.internal.generator.NullAllowed;
import com.pholser.junit.quickcheck.generator.NullAllowed;
import com.pholser.junit.quickcheck.runner.JUnitQuickcheck;
import java.math.RoundingMode;
import javax.annotation.Nullable;
Expand Down

0 comments on commit c91e25f

Please sign in to comment.