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
b948e614
Commit
b948e614
authored
Sep 03, 2014
by
David Liu
Committed by
Phillip Webb
Oct 09, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add CAMELCASE_TO_HYPHEN in RelaxedNames
Fixes gh-1499
parent
c906524d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
10 deletions
+40
-10
RelaxedNames.java
...main/java/org/springframework/boot/bind/RelaxedNames.java
+25
-10
RelaxedNamesTests.java
...java/org/springframework/boot/bind/RelaxedNamesTests.java
+15
-0
No files found.
spring-boot/src/main/java/org/springframework/boot/bind/RelaxedNames.java
View file @
b948e614
...
...
@@ -19,6 +19,8 @@ package org.springframework.boot.bind;
import
java.util.Iterator
;
import
java.util.LinkedHashSet
;
import
java.util.Set
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
org.springframework.util.StringUtils
;
...
...
@@ -32,6 +34,8 @@ import org.springframework.util.StringUtils;
*/
public
final
class
RelaxedNames
implements
Iterable
<
String
>
{
private
static
final
Pattern
CAMEL_CASE_PATTERN
=
Pattern
.
compile
(
"([^A-Z-])([A-Z])"
);
private
final
String
name
;
private
final
Set
<
String
>
values
=
new
LinkedHashSet
<
String
>();
...
...
@@ -126,17 +130,28 @@ public final class RelaxedNames implements Iterable<String> {
CAMELCASE_TO_UNDERSCORE
{
@Override
public
String
apply
(
String
value
)
{
value
=
value
.
replaceAll
(
"([^A-Z-])([A-Z])"
,
"$1_$2"
);
StringBuilder
builder
=
new
StringBuilder
();
for
(
String
field
:
value
.
split
(
"_"
))
{
if
(
builder
.
length
()
==
0
)
{
builder
.
append
(
field
);
}
else
{
builder
.
append
(
"_"
).
append
(
StringUtils
.
uncapitalize
(
field
));
}
Matcher
matcher
=
CAMEL_CASE_PATTERN
.
matcher
(
value
);
StringBuffer
result
=
new
StringBuffer
();
while
(
matcher
.
find
())
{
matcher
.
appendReplacement
(
result
,
matcher
.
group
(
1
)
+
'_'
+
StringUtils
.
uncapitalize
(
matcher
.
group
(
2
)));
}
return
builder
.
toString
();
matcher
.
appendTail
(
result
);
return
result
.
toString
();
}
},
CAMELCASE_TO_HYPHEN
{
@Override
public
String
apply
(
String
value
)
{
Matcher
matcher
=
CAMEL_CASE_PATTERN
.
matcher
(
value
);
StringBuffer
result
=
new
StringBuffer
();
while
(
matcher
.
find
())
{
matcher
.
appendReplacement
(
result
,
matcher
.
group
(
1
)
+
'-'
+
StringUtils
.
uncapitalize
(
matcher
.
group
(
2
)));
}
matcher
.
appendTail
(
result
);
return
result
.
toString
();
}
},
...
...
spring-boot/src/test/java/org/springframework/boot/bind/RelaxedNamesTests.java
View file @
b948e614
...
...
@@ -72,9 +72,24 @@ public class RelaxedNamesTests {
Iterator
<
String
>
iterator
=
new
RelaxedNames
(
"caMel"
).
iterator
();
assertThat
(
iterator
.
next
(),
equalTo
(
"caMel"
));
assertThat
(
iterator
.
next
(),
equalTo
(
"ca_mel"
));
assertThat
(
iterator
.
next
(),
equalTo
(
"ca-mel"
));
assertThat
(
iterator
.
next
(),
equalTo
(
"camel"
));
assertThat
(
iterator
.
next
(),
equalTo
(
"CAMEL"
));
assertThat
(
iterator
.
next
(),
equalTo
(
"CA_MEL"
));
assertThat
(
iterator
.
next
(),
equalTo
(
"CA-MEL"
));
assertThat
(
iterator
.
hasNext
(),
equalTo
(
false
));
}
@Test
public
void
fromCompoundCamelCase
()
throws
Exception
{
Iterator
<
String
>
iterator
=
new
RelaxedNames
(
"caMelCase"
).
iterator
();
assertThat
(
iterator
.
next
(),
equalTo
(
"caMelCase"
));
assertThat
(
iterator
.
next
(),
equalTo
(
"ca_mel_case"
));
assertThat
(
iterator
.
next
(),
equalTo
(
"ca-mel-case"
));
assertThat
(
iterator
.
next
(),
equalTo
(
"camelcase"
));
assertThat
(
iterator
.
next
(),
equalTo
(
"CAMELCASE"
));
assertThat
(
iterator
.
next
(),
equalTo
(
"CA_MEL_CASE"
));
assertThat
(
iterator
.
next
(),
equalTo
(
"CA-MEL-CASE"
));
assertThat
(
iterator
.
hasNext
(),
equalTo
(
false
));
}
...
...
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