Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
7c4f178c
Commit
7c4f178c
authored
Jun 10, 2021
by
Phillip Webb
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.3.x' into 2.4.x
parents
2aa1c976
3b485267
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
31 deletions
+34
-31
RandomValuePropertySource.java
...g/springframework/boot/env/RandomValuePropertySource.java
+34
-31
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/RandomValuePropertySource.java
View file @
7c4f178c
...
@@ -20,9 +20,7 @@ import java.util.OptionalInt;
...
@@ -20,9 +20,7 @@ import java.util.OptionalInt;
import
java.util.OptionalLong
;
import
java.util.OptionalLong
;
import
java.util.Random
;
import
java.util.Random
;
import
java.util.UUID
;
import
java.util.UUID
;
import
java.util.function.BiPredicate
;
import
java.util.function.Function
;
import
java.util.function.Function
;
import
java.util.function.Predicate
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.apache.commons.logging.LogFactory
;
...
@@ -98,11 +96,11 @@ public class RandomValuePropertySource extends PropertySource<Random> {
...
@@ -98,11 +96,11 @@ public class RandomValuePropertySource extends PropertySource<Random> {
}
}
String
range
=
getRange
(
type
,
"int"
);
String
range
=
getRange
(
type
,
"int"
);
if
(
range
!=
null
)
{
if
(
range
!=
null
)
{
return
getNextIntInRange
(
range
);
return
getNextIntInRange
(
Range
.
of
(
range
,
Integer:
:
parseInt
)
);
}
}
range
=
getRange
(
type
,
"long"
);
range
=
getRange
(
type
,
"long"
);
if
(
range
!=
null
)
{
if
(
range
!=
null
)
{
return
getNextLongInRange
(
range
);
return
getNextLongInRange
(
Range
.
of
(
range
,
Long:
:
parseLong
)
);
}
}
if
(
type
.
equals
(
"uuid"
))
{
if
(
type
.
equals
(
"uuid"
))
{
return
UUID
.
randomUUID
().
toString
();
return
UUID
.
randomUUID
().
toString
();
...
@@ -120,24 +118,22 @@ public class RandomValuePropertySource extends PropertySource<Random> {
...
@@ -120,24 +118,22 @@ public class RandomValuePropertySource extends PropertySource<Random> {
return
null
;
return
null
;
}
}
private
int
getNextIntInRange
(
String
range
)
{
private
int
getNextIntInRange
(
Range
<
Integer
>
range
)
{
Range
<
Integer
>
intRange
=
Range
.
get
(
range
,
Integer:
:
parseInt
,
(
t
)
->
t
>
0
,
0
,
(
t1
,
t2
)
->
t1
<
t2
);
OptionalInt
first
=
getSource
().
ints
(
1
,
range
.
getMin
(),
range
.
getMax
()).
findFirst
();
OptionalInt
first
=
getSource
().
ints
(
1
,
intRange
.
getMin
(),
intRange
.
getMax
()).
findFirst
();
assertPresent
(
first
.
isPresent
(),
range
);
if
(!
first
.
isPresent
())
{
throw
new
RuntimeException
(
"Could not get random number for range '"
+
range
+
"'"
);
}
return
first
.
getAsInt
();
return
first
.
getAsInt
();
}
}
private
long
getNextLongInRange
(
String
range
)
{
private
long
getNextLongInRange
(
Range
<
Long
>
range
)
{
Range
<
Long
>
longRange
=
Range
.
get
(
range
,
Long:
:
parseLong
,
(
t
)
->
t
>
0L
,
0L
,
(
t1
,
t2
)
->
t1
<
t2
);
OptionalLong
first
=
getSource
().
longs
(
1
,
range
.
getMin
(),
range
.
getMax
()).
findFirst
();
OptionalLong
first
=
getSource
().
longs
(
1
,
longRange
.
getMin
(),
longRange
.
getMax
()).
findFirst
();
assertPresent
(
first
.
isPresent
(),
range
);
if
(!
first
.
isPresent
())
{
throw
new
RuntimeException
(
"Could not get random number for range '"
+
range
+
"'"
);
}
return
first
.
getAsLong
();
return
first
.
getAsLong
();
}
}
private
void
assertPresent
(
boolean
present
,
Range
<?>
range
)
{
Assert
.
state
(
present
,
()
->
"Could not get random number for range '"
+
range
+
"'"
);
}
private
Object
getRandomBytes
()
{
private
Object
getRandomBytes
()
{
byte
[]
bytes
=
new
byte
[
32
];
byte
[]
bytes
=
new
byte
[
32
];
getSource
().
nextBytes
(
bytes
);
getSource
().
nextBytes
(
bytes
);
...
@@ -167,27 +163,16 @@ public class RandomValuePropertySource extends PropertySource<Random> {
...
@@ -167,27 +163,16 @@ public class RandomValuePropertySource extends PropertySource<Random> {
static
final
class
Range
<
T
extends
Number
>
{
static
final
class
Range
<
T
extends
Number
>
{
private
final
String
value
;
private
final
T
min
;
private
final
T
min
;
private
final
T
max
;
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
.
min
=
min
;
this
.
max
=
max
;
this
.
max
=
max
;
}
static
<
T
extends
Number
>
Range
<
T
>
get
(
String
range
,
Function
<
String
,
T
>
parse
,
Predicate
<
T
>
boundValidator
,
T
defaultMin
,
BiPredicate
<
T
,
T
>
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
()
{
T
getMin
()
{
...
@@ -198,6 +183,24 @@ public class RandomValuePropertySource extends PropertySource<Random> {
...
@@ -198,6 +183,24 @@ public class RandomValuePropertySource extends PropertySource<Random> {
return
this
.
max
;
return
this
.
max
;
}
}
@Override
public
String
toString
()
{
return
this
.
value
;
}
static
<
T
extends
Number
&
Comparable
<
T
>>
Range
<
T
>
of
(
String
value
,
Function
<
String
,
T
>
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
);
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment