Skip to content

Commit e391ab2

Browse files
Capitalize method
1 parent fedaf55 commit e391ab2

File tree

7 files changed

+181
-93
lines changed

7 files changed

+181
-93
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Project
22
.settings/
3+
.vscode/
34

45
# Maven
56
target/

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ System.out.println( StringUtils.implode(words, " ") );
5959
## 📖 Documentation
6060
| Property |Type|Description|Default|
6161
|----------------|-------------------------------|-----------------------------|--------|
62-
|implode |`list: List<T>, delimiter: String: String`|Converts elements of a list into a string by separating each element with a delimiter| - |
62+
|implode |`(list: List<T>, delimiter: String): String`|Converts elements of a list into a string by separating each element with a delimiter| - |
63+
|capitalize |`(text: String): String`|Converts elements of a list into a string by separating each element with a delimiter| - |
6364

6465

6566
## 🚩 Changelog

dist/1.x/string-utils-1.1.0.jar

5.56 KB
Binary file not shown.

pom-mavencentral.xml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
<groupId>io.github.wniemiec-util-java</groupId>
88
<artifactId>string-utils</artifactId>
9-
<version>1.0.0</version>
10-
9+
<version>1.1.0</version>
1110
<name>StringUtils</name>
1211
<description>Contains methods that perform string manipulation.</description>
1312
<url>https://github.com/wniemiec-util-java/string-utils</url>
@@ -52,11 +51,11 @@
5251

5352
<dependencies>
5453
<dependency>
55-
<groupId>junit</groupId>
56-
<artifactId>junit</artifactId>
57-
<version>4.13.2</version>
58-
<scope>test</scope>
59-
</dependency>
54+
<groupId>org.junit.jupiter</groupId>
55+
<artifactId>junit-jupiter</artifactId>
56+
<version>5.7.0</version>
57+
<scope>test</scope>
58+
</dependency>
6059
</dependencies>
6160

6261
<build>
@@ -144,6 +143,9 @@
144143
<groupId>org.apache.maven.plugins</groupId>
145144
<artifactId>maven-javadoc-plugin</artifactId>
146145
<version>3.2.0</version>
146+
<configuration>
147+
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
148+
</configuration>
147149
<executions>
148150
<execution>
149151
<id>attach-javadocs</id>

pom.xml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
<groupId>wniemiec.util.java</groupId>
88
<artifactId>string-utils</artifactId>
9-
<version>1.0.0</version>
10-
9+
<version>1.1.0</version>
1110
<name>StringUtils</name>
1211
<description>Contains methods that perform string manipulation.</description>
1312
<url>https://github.com/wniemiec-util-java/string-utils</url>
@@ -49,11 +48,11 @@
4948

5049
<dependencies>
5150
<dependency>
52-
<groupId>junit</groupId>
53-
<artifactId>junit</artifactId>
54-
<version>4.13.2</version>
55-
<scope>test</scope>
56-
</dependency>
51+
<groupId>org.junit.jupiter</groupId>
52+
<artifactId>junit-jupiter</artifactId>
53+
<version>5.7.0</version>
54+
<scope>test</scope>
55+
</dependency>
5756
</dependencies>
5857

5958
<build>
@@ -141,6 +140,9 @@
141140
<groupId>org.apache.maven.plugins</groupId>
142141
<artifactId>maven-javadoc-plugin</artifactId>
143142
<version>3.2.0</version>
143+
<configuration>
144+
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
145+
</configuration>
144146
<executions>
145147
<execution>
146148
<id>attach-javadocs</id>

src/main/java/wniemiec/util/java/StringUtils.java

Lines changed: 94 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,98 @@
1313
* Contains methods that perform string manipulation.
1414
*/
1515
public class StringUtils {
16-
17-
//-------------------------------------------------------------------------
18-
// Constructor
19-
//-------------------------------------------------------------------------
20-
private StringUtils() {
21-
}
22-
23-
24-
//-------------------------------------------------------------------------
25-
// Methods
26-
//-------------------------------------------------------------------------
27-
/**
28-
* Converts elements of a list into a string by separating each element
29-
* with a delimiter.
30-
*
31-
* @param list List to be converted
32-
*
33-
* @return List elements separated by the given delimiter
34-
*
35-
* @throws IllegalArgumentException If list or delimiter is null
36-
*/
37-
public static <T> String implode(List<T> list, String delimiter) {
38-
if (list == null)
39-
throw new IllegalArgumentException("List cannot be null");
40-
41-
if (delimiter == null)
42-
throw new IllegalArgumentException("Delimiter cannot be null");
43-
44-
StringBuilder response = new StringBuilder();
45-
46-
for (T p : list) {
47-
response.append(p);
48-
response.append(delimiter);
49-
}
50-
51-
// Removes last delimiter
52-
if (response.length() > 1) {
53-
response.deleteCharAt(response.length()-1);
54-
}
55-
56-
return response.toString();
57-
}
16+
17+
//-------------------------------------------------------------------------
18+
// Constructor
19+
//-------------------------------------------------------------------------
20+
private StringUtils() {
21+
}
22+
23+
24+
//-------------------------------------------------------------------------
25+
// Methods
26+
//-------------------------------------------------------------------------
27+
/**
28+
* Converts elements of a list into a string by separating each element
29+
* with a delimiter.
30+
*
31+
* @param list List to be converted
32+
*
33+
* @return List elements separated by the given delimiter
34+
*
35+
* @throws IllegalArgumentException If list or delimiter is null
36+
*/
37+
public static <T> String implode(List<T> list, String delimiter) {
38+
validateImplodeArgs(list, delimiter);
39+
40+
StringBuilder response = new StringBuilder();
41+
42+
for (T p : list) {
43+
response.append(p);
44+
response.append(delimiter);
45+
}
46+
47+
removeLastCharacter(response);
48+
49+
return response.toString();
50+
}
51+
52+
private static <T> void validateImplodeArgs(List<T> list, String delimiter) {
53+
if (list == null) {
54+
throw new IllegalArgumentException("List cannot be null");
55+
}
56+
57+
if (delimiter == null) {
58+
throw new IllegalArgumentException("Delimiter cannot be null");
59+
}
60+
}
61+
62+
private static void removeLastCharacter(StringBuilder response) {
63+
if (response.length() <= 1) {
64+
return;
65+
}
66+
67+
response.deleteCharAt(response.length()-1);
68+
}
69+
70+
/**
71+
* Converts first character of a text to uppercase letter and convert to
72+
* lowercase all other characters (if any).
73+
*
74+
* @param text Text to be capitalized
75+
*
76+
* @return Capitalized text
77+
*/
78+
public static String capitalize(String text) {
79+
validateCapitalizeArg(text);
80+
81+
if (text.isEmpty()) {
82+
return text;
83+
}
84+
85+
if (text.length() == 1) {
86+
return text.toUpperCase();
87+
}
88+
89+
StringBuilder capitalizedText = new StringBuilder();
90+
91+
for (String term : text.split(" ")) {
92+
String firstCharacter = term.substring(0, 1);
93+
String remainingText = term.substring(1);
94+
95+
capitalizedText.append(firstCharacter.toUpperCase());
96+
capitalizedText.append(remainingText.toLowerCase());
97+
capitalizedText.append(" ");
98+
}
99+
100+
removeLastCharacter(capitalizedText);
101+
102+
return capitalizedText.toString();
103+
}
104+
105+
private static void validateCapitalizeArg(String text) {
106+
if (text == null) {
107+
throw new IllegalArgumentException("Text cannot be null");
108+
}
109+
}
58110
}
Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,71 @@
11
package wniemiec.util.java;
22

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.fail;
5-
63
import java.util.List;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
7+
8+
class StringUtilsTest {
9+
10+
@Test
11+
void testHelloWorld() {
12+
List<String> items = List.of("hello", "world");
13+
String obtained = StringUtils.implode(items, " ");
14+
15+
Assertions.assertEquals("hello world", obtained);
16+
}
17+
18+
@Test
19+
void testNullList() {
20+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
21+
StringUtils.implode(null, " ");
22+
});
23+
}
24+
25+
@Test
26+
void testNullDelimiter() {
27+
List<String> items = List.of("hello", "world");
28+
29+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
30+
StringUtils.implode(items, null);
31+
});
32+
}
33+
34+
@Test
35+
void testNullDelimiterAndList() {
36+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
37+
StringUtils.implode(null, null);
38+
});
39+
}
40+
41+
@Test
42+
void testCapitalizeTextWith1Character() {
43+
String text = "h";
44+
String obtained = StringUtils.capitalize(text);
45+
46+
Assertions.assertEquals("H", obtained);
47+
}
48+
49+
@Test
50+
void testCapitalizeTextWithMoreThan1Characters() {
51+
String text = "hello wOrLd";
52+
String obtained = StringUtils.capitalize(text);
53+
54+
Assertions.assertEquals("Hello World", obtained);
55+
}
56+
57+
@Test
58+
void testCapitalizeEmptyText() {
59+
String text = "";
60+
String obtained = StringUtils.capitalize(text);
61+
62+
Assertions.assertEquals("", obtained);
63+
}
764

8-
import org.junit.Test;
9-
10-
public class StringUtilsTest {
11-
12-
@Test
13-
public void testHelloWorld() {
14-
List<String> items = List.of("hello", "world");
15-
16-
assertEquals("hello world", StringUtils.implode(items, " "));
17-
}
18-
19-
@Test(expected = IllegalArgumentException.class)
20-
public void testNullList() {
21-
StringUtils.implode(null, " ");
22-
23-
fail();
24-
}
25-
26-
@Test(expected = IllegalArgumentException.class)
27-
public void testNullDelimiter() {
28-
List<String> items = List.of("hello", "world");
29-
30-
StringUtils.implode(items, null);
31-
32-
fail();
33-
}
34-
35-
@Test(expected = IllegalArgumentException.class)
36-
public void testNullDelimiterAndList() {
37-
StringUtils.implode(null, null);
38-
39-
fail();
40-
}
65+
@Test
66+
void testCapitalizeNullText() {
67+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
68+
StringUtils.capitalize(null);
69+
});
70+
}
4171
}

0 commit comments

Comments
 (0)