|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2020 the original author or authors. |
| 2 | + * Copyright 2012-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.env;
|
18 | 18 |
|
| 19 | +import java.util.OptionalInt; |
| 20 | +import java.util.OptionalLong; |
19 | 21 | import java.util.Random;
|
20 | 22 | import java.util.UUID;
|
| 23 | +import java.util.function.BiPredicate; |
| 24 | +import java.util.function.Function; |
| 25 | +import java.util.function.Predicate; |
21 | 26 |
|
22 | 27 | import org.apache.commons.logging.Log;
|
23 | 28 | import org.apache.commons.logging.LogFactory;
|
|
27 | 32 | import org.springframework.core.env.PropertySource;
|
28 | 33 | import org.springframework.core.env.StandardEnvironment;
|
29 | 34 | import org.springframework.core.log.LogMessage;
|
| 35 | +import org.springframework.util.Assert; |
30 | 36 | import org.springframework.util.DigestUtils;
|
31 | 37 | import org.springframework.util.StringUtils;
|
32 | 38 |
|
|
46 | 52 | * suffix whose syntax is:
|
47 | 53 | * <p>
|
48 | 54 | * {@code OPEN value (,max) CLOSE} where the {@code OPEN,CLOSE} are any character and
|
49 |
| - * {@code value,max} are integers. If {@code max} is provided then {@code value} is the |
50 |
| - * minimum value and {@code max} is the maximum (exclusive). |
| 55 | + * {@code value,max} are integers. If {@code max} is not provided, then 0 is used as the |
| 56 | + * lower bound and {@code value} is the upper bound. If {@code max} is provided then |
| 57 | + * {@code value} is the minimum value and {@code max} is the maximum (exclusive). |
51 | 58 | *
|
52 | 59 | * @author Dave Syer
|
53 | 60 | * @author Matt Benson
|
| 61 | + * @author Madhura Bhave |
54 | 62 | * @since 1.0.0
|
55 | 63 | */
|
56 | 64 | public class RandomValuePropertySource extends PropertySource<Random> {
|
@@ -113,22 +121,21 @@ private String getRange(String type, String prefix) {
|
113 | 121 | }
|
114 | 122 |
|
115 | 123 | private int getNextIntInRange(String range) {
|
116 |
| - String[] tokens = StringUtils.commaDelimitedListToStringArray(range); |
117 |
| - int start = Integer.parseInt(tokens[0]); |
118 |
| - if (tokens.length == 1) { |
119 |
| - return getSource().nextInt(start); |
| 124 | + Range<Integer> intRange = Range.get(range, Integer::parseInt, (t) -> t > 0, 0, (t1, t2) -> t1 < t2); |
| 125 | + OptionalInt first = getSource().ints(1, intRange.getMin(), intRange.getMax()).findFirst(); |
| 126 | + if (!first.isPresent()) { |
| 127 | + throw new RuntimeException("Could not get random number for range '" + range + "'"); |
120 | 128 | }
|
121 |
| - return start + getSource().nextInt(Integer.parseInt(tokens[1]) - start); |
| 129 | + return first.getAsInt(); |
122 | 130 | }
|
123 | 131 |
|
124 | 132 | private long getNextLongInRange(String range) {
|
125 |
| - String[] tokens = StringUtils.commaDelimitedListToStringArray(range); |
126 |
| - if (tokens.length == 1) { |
127 |
| - return Math.abs(getSource().nextLong() % Long.parseLong(tokens[0])); |
| 133 | + Range<Long> longRange = Range.get(range, Long::parseLong, (t) -> t > 0L, 0L, (t1, t2) -> t1 < t2); |
| 134 | + OptionalLong first = getSource().longs(1, longRange.getMin(), longRange.getMax()).findFirst(); |
| 135 | + if (!first.isPresent()) { |
| 136 | + throw new RuntimeException("Could not get random number for range '" + range + "'"); |
128 | 137 | }
|
129 |
| - long lowerBound = Long.parseLong(tokens[0]); |
130 |
| - long upperBound = Long.parseLong(tokens[1]) - lowerBound; |
131 |
| - return lowerBound + Math.abs(getSource().nextLong() % upperBound); |
| 138 | + return first.getAsLong(); |
132 | 139 | }
|
133 | 140 |
|
134 | 141 | private Object getRandomBytes() {
|
@@ -158,4 +165,39 @@ static void addToEnvironment(ConfigurableEnvironment environment, Log logger) {
|
158 | 165 | logger.trace("RandomValuePropertySource add to Environment");
|
159 | 166 | }
|
160 | 167 |
|
| 168 | + static final class Range<T extends Number> { |
| 169 | + |
| 170 | + private final T min; |
| 171 | + |
| 172 | + private final T max; |
| 173 | + |
| 174 | + private Range(T min, T max) { |
| 175 | + this.min = min; |
| 176 | + this.max = max; |
| 177 | + |
| 178 | + } |
| 179 | + |
| 180 | + static <T extends Number> Range<T> get(String range, Function<String, T> parse, Predicate<T> boundValidator, |
| 181 | + T defaultMin, BiPredicate<T, T> rangeValidator) { |
| 182 | + String[] tokens = StringUtils.commaDelimitedListToStringArray(range); |
| 183 | + T token1 = parse.apply(tokens[0]); |
| 184 | + if (tokens.length == 1) { |
| 185 | + Assert.isTrue(boundValidator.test(token1), "Bound must be positive."); |
| 186 | + return new Range<>(defaultMin, token1); |
| 187 | + } |
| 188 | + T token2 = parse.apply(tokens[1]); |
| 189 | + Assert.isTrue(rangeValidator.test(token1, token2), "Lower bound must be less than upper bound."); |
| 190 | + return new Range<>(token1, token2); |
| 191 | + } |
| 192 | + |
| 193 | + T getMin() { |
| 194 | + return this.min; |
| 195 | + } |
| 196 | + |
| 197 | + T getMax() { |
| 198 | + return this.max; |
| 199 | + } |
| 200 | + |
| 201 | + } |
| 202 | + |
161 | 203 | }
|
0 commit comments