diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/RandomValuePropertySource.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/RandomValuePropertySource.java index 4c374ce79ba6..c4085a8f6fea 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/RandomValuePropertySource.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/RandomValuePropertySource.java @@ -20,9 +20,7 @@ import java.util.OptionalLong; import java.util.Random; import java.util.UUID; -import java.util.function.BiPredicate; import java.util.function.Function; -import java.util.function.Predicate; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -98,11 +96,11 @@ private Object getRandomValue(String type) { } String range = getRange(type, "int"); if (range != null) { - return getNextIntInRange(range); + return getNextIntInRange(Range.of(range, Integer::parseInt)); } range = getRange(type, "long"); if (range != null) { - return getNextLongInRange(range); + return getNextLongInRange(Range.of(range, Long::parseLong)); } if (type.equals("uuid")) { return UUID.randomUUID().toString(); @@ -120,24 +118,22 @@ private String getRange(String type, String prefix) { return null; } - private int getNextIntInRange(String range) { - Range intRange = Range.get(range, Integer::parseInt, (t) -> t > 0, 0, (t1, t2) -> t1 < t2); - OptionalInt first = getSource().ints(1, intRange.getMin(), intRange.getMax()).findFirst(); - if (!first.isPresent()) { - throw new RuntimeException("Could not get random number for range '" + range + "'"); - } + private int getNextIntInRange(Range range) { + OptionalInt first = getSource().ints(1, range.getMin(), range.getMax()).findFirst(); + assertPresent(first.isPresent(), range); return first.getAsInt(); } - private long getNextLongInRange(String range) { - Range longRange = Range.get(range, Long::parseLong, (t) -> t > 0L, 0L, (t1, t2) -> t1 < t2); - OptionalLong first = getSource().longs(1, longRange.getMin(), longRange.getMax()).findFirst(); - if (!first.isPresent()) { - throw new RuntimeException("Could not get random number for range '" + range + "'"); - } + private long getNextLongInRange(Range range) { + OptionalLong first = getSource().longs(1, range.getMin(), range.getMax()).findFirst(); + assertPresent(first.isPresent(), range); return first.getAsLong(); } + private void assertPresent(boolean present, Range range) { + Assert.state(present, () -> "Could not get random number for range '" + range + "'"); + } + private Object getRandomBytes() { byte[] bytes = new byte[32]; getSource().nextBytes(bytes); @@ -167,27 +163,16 @@ static void addToEnvironment(ConfigurableEnvironment environment, Log logger) { static final class Range { + private final String value; + private final T min; private final T max; - private Range(T min, T max) { + private Range(String value, T min, T max) { + this.value = value; this.min = min; this.max = max; - - } - - static Range get(String range, Function parse, Predicate boundValidator, - T defaultMin, BiPredicate rangeValidator) { - String[] tokens = StringUtils.commaDelimitedListToStringArray(range); - T token1 = parse.apply(tokens[0]); - if (tokens.length == 1) { - Assert.isTrue(boundValidator.test(token1), "Bound must be positive."); - return new Range<>(defaultMin, token1); - } - T token2 = parse.apply(tokens[1]); - Assert.isTrue(rangeValidator.test(token1, token2), "Lower bound must be less than upper bound."); - return new Range<>(token1, token2); } T getMin() { @@ -198,6 +183,24 @@ T getMax() { return this.max; } + @Override + public String toString() { + return this.value; + } + + static > Range of(String value, Function parse) { + T zero = parse.apply("0"); + String[] tokens = StringUtils.commaDelimitedListToStringArray(value); + T min = parse.apply(tokens[0]); + if (tokens.length == 1) { + Assert.isTrue(min.compareTo(zero) > 0, "Bound must be positive."); + return new Range<>(value, zero, min); + } + T max = parse.apply(tokens[1]); + Assert.isTrue(min.compareTo(max) < 0, "Lower bound must be less than upper bound."); + return new Range<>(value, min, max); + } + } }