Skip to content

Commit 17ecdbf

Browse files
author
Ashley Frieze
committed
BAEL-3905 - replacing tokens tests and implementation
1 parent e208f24 commit 17ecdbf

3 files changed

Lines changed: 148 additions & 0 deletions

File tree

core-java-modules/core-java-regex/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
<artifactId>jmh-generator-annprocess</artifactId>
2626
<version>${jmh-core.version}</version>
2727
</dependency>
28+
<dependency>
29+
<groupId>org.assertj</groupId>
30+
<artifactId>assertj-core</artifactId>
31+
<version>3.15.0</version>
32+
<scope>test</scope>
33+
</dependency>
2834
</dependencies>
2935

3036
<build>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.replacetokens;
2+
3+
import java.util.function.Function;
4+
import java.util.regex.Matcher;
5+
import java.util.regex.Pattern;
6+
7+
public class ReplacingTokens {
8+
public static final Pattern TITLE_CASE_PATTERN = Pattern.compile("(?<=^|[^A-Za-z])([A-Z][a-z]*)(?=[^A-Za-z]|$)");
9+
10+
/**
11+
* Iterate over the title case tokens in the input and replace them with lowercase
12+
* @param original the original string
13+
* @return a string with words replaced with their lowercase equivalents
14+
*/
15+
public static String replaceTitleCaseWithLowerCase(String original) {
16+
int lastIndex = 0;
17+
StringBuilder output = new StringBuilder();
18+
Matcher matcher = TITLE_CASE_PATTERN.matcher(original);
19+
while (matcher.find()) {
20+
output.append(original, lastIndex, matcher.start())
21+
.append(convert(matcher.group(1)));
22+
23+
lastIndex = matcher.end();
24+
}
25+
if (lastIndex < original.length()) {
26+
output.append(original, lastIndex, original.length());
27+
}
28+
return output.toString();
29+
}
30+
31+
/**
32+
* Convert a token found into its desired lowercase
33+
* @param token the token to convert
34+
* @return the converted token
35+
*/
36+
private static String convert(String token) {
37+
return token.toLowerCase();
38+
}
39+
40+
/**
41+
* Replace all the tokens in an input using the algorithm provided for each
42+
* @param original original string
43+
* @param tokenPattern the pattern to match with
44+
* @param converter the conversion to apply
45+
* @return the substituted string
46+
*/
47+
public static String replaceTokens(String original, Pattern tokenPattern,
48+
Function<Matcher, String> converter) {
49+
int lastIndex = 0;
50+
StringBuilder output = new StringBuilder();
51+
Matcher matcher = tokenPattern.matcher(original);
52+
while (matcher.find()) {
53+
output.append(original, lastIndex, matcher.start())
54+
.append(converter.apply(matcher));
55+
56+
lastIndex = matcher.end();
57+
}
58+
if (lastIndex < original.length()) {
59+
output.append(original, lastIndex, original.length());
60+
}
61+
return output.toString();
62+
}
63+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.baeldung.replacetokens;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.regex.Matcher;
10+
import java.util.regex.Pattern;
11+
12+
import static com.baeldung.replacetokens.ReplacingTokens.TITLE_CASE_PATTERN;
13+
import static com.baeldung.replacetokens.ReplacingTokens.replaceTokens;
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
16+
public class ReplacingTokensUnitTest {
17+
private static final String EXAMPLE_INPUT = "First 3 Capital Words! then 10 TLAs, I Found";
18+
private static final String EXAMPLE_INPUT_PROCESSED = "first 3 capital words! then 10 TLAs, i found";
19+
20+
@Test
21+
public void whenFindMatches_thenTitleWordsFound() {
22+
Matcher matcher = TITLE_CASE_PATTERN.matcher(EXAMPLE_INPUT);
23+
List<String> matches = new ArrayList<>();
24+
while (matcher.find()) {
25+
matches.add(matcher.group(1));
26+
}
27+
28+
assertThat(matches)
29+
.containsExactly("First", "Capital", "Words", "I", "Found");
30+
}
31+
32+
@Test
33+
public void exploreMatches() {
34+
Matcher matcher = TITLE_CASE_PATTERN.matcher(EXAMPLE_INPUT);
35+
while (matcher.find()) {
36+
System.out.println("Match: " + matcher.group(0));
37+
System.out.println("Start: " + matcher.start());
38+
System.out.println("End: " + matcher.end());
39+
}
40+
}
41+
42+
@Test
43+
public void whenReplaceTokensWithLowerCase() {
44+
assertThat(ReplacingTokens.replaceTitleCaseWithLowerCase(EXAMPLE_INPUT))
45+
.isEqualTo(EXAMPLE_INPUT_PROCESSED);
46+
}
47+
48+
@Test
49+
public void whenReplaceTokensWithLowerCaseUsingGeneralPurpose() {
50+
assertThat(replaceTokens("First 3 Capital Words! then 10 TLAs, I Found",
51+
TITLE_CASE_PATTERN,
52+
match -> match.group(1).toLowerCase()))
53+
.isEqualTo("first 3 capital words! then 10 TLAs, i found");
54+
}
55+
56+
@Test
57+
public void escapeRegexCharacters() {
58+
Pattern regexCharacters = Pattern.compile("[<(\\[{\\\\^\\-=$!|\\]})?*+.>]");
59+
60+
assertThat(replaceTokens("A regex character like [",
61+
regexCharacters,
62+
match -> "\\" + match.group()))
63+
.isEqualTo("A regex character like \\[");
64+
}
65+
66+
@Test
67+
public void replacePlaceholders() {
68+
Pattern placeholderPattern = Pattern.compile("\\$\\{(?<placeholder>[A-Za-z0-9-_]+)}");
69+
70+
Map<String, String> placeholderValues = new HashMap<>();
71+
placeholderValues.put("name", "Bill");
72+
placeholderValues.put("company", "Baeldung");
73+
74+
assertThat(replaceTokens("Hi ${name} at ${company}",
75+
placeholderPattern,
76+
match -> placeholderValues.get(match.group("placeholder"))))
77+
.isEqualTo("Hi Bill at Baeldung");
78+
}
79+
}

0 commit comments

Comments
 (0)