Skip to content

Commit 6c3654d

Browse files
Improve singular method
1 parent dad30c6 commit 6c3654d

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

modello-core/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
<artifactId>junit-jupiter-api</artifactId>
5555
<scope>test</scope>
5656
</dependency>
57+
<dependency>
58+
<groupId>org.junit.jupiter</groupId>
59+
<artifactId>junit-jupiter-params</artifactId>
60+
<scope>test</scope>
61+
</dependency>
5762
<dependency>
5863
<groupId>org.codehaus.plexus</groupId>
5964
<artifactId>plexus-testing</artifactId>

modello-core/src/main/java/org/codehaus/modello/plugin/AbstractModelloGenerator.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.nio.file.Path;
3333
import java.util.ArrayList;
3434
import java.util.Arrays;
35+
import java.util.HashMap;
3536
import java.util.List;
3637
import java.util.Map;
3738

@@ -57,6 +58,14 @@
5758
public abstract class AbstractModelloGenerator implements ModelloGenerator {
5859
private final Logger logger = LoggerFactory.getLogger(getClass());
5960

61+
private static final Map<String, String> PLURAL_EXCEPTION = new HashMap<>();
62+
63+
static {
64+
PLURAL_EXCEPTION.put("children", "child");
65+
PLURAL_EXCEPTION.put("licenses", "license");
66+
PLURAL_EXCEPTION.put("series", "series");
67+
}
68+
6069
private Model model;
6170

6271
private File outputDirectory;
@@ -150,6 +159,7 @@ protected boolean isClassInModel(String fieldType, Model model) {
150159

151160
/**
152161
* Return the child fields of this class.
162+
*
153163
* @param modelClass current class
154164
* @return the list of fields of this class
155165
*/
@@ -198,11 +208,14 @@ public static String singular(String name) {
198208
return name;
199209
}
200210

201-
if (name.endsWith("ies")) {
211+
if (PLURAL_EXCEPTION.containsKey(name)) {
212+
return PLURAL_EXCEPTION.get(name);
213+
} else if (name.endsWith("ies")) {
202214
return name.substring(0, name.length() - 3) + "y";
203-
} else if (name.endsWith("es") && name.endsWith("ches")) {
204-
return name.substring(0, name.length() - 2);
205-
} else if (name.endsWith("xes")) {
215+
} else if (name.endsWith("zzes")) {
216+
return name.substring(0, name.length() - 3);
217+
} else if (name.endsWith("ches") || name.endsWith("xes") || name.endsWith("ses") || name.endsWith("oes")
218+
|| name.endsWith("shes")) {
206219
return name.substring(0, name.length() - 2);
207220
} else if (name.endsWith("s") && (name.length() != 1)) {
208221
return name.substring(0, name.length() - 1);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.codehaus.modello.plugin;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.CsvSource;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
class AbstractModelloGeneratorTest {
9+
10+
@CsvSource( {
11+
",",
12+
"'',''",
13+
"s,s",
14+
"aliases, alias",
15+
"babies, baby",
16+
"fezzes, fez",
17+
"foxes, fox",
18+
"ids, id",
19+
"licenses, license",
20+
"lunches, lunch",
21+
"potatoes, potato",
22+
"roles, role",
23+
"rushes, rush",
24+
"series, series"
25+
26+
})
27+
@ParameterizedTest
28+
public void testSingular(String plural, String singular) {
29+
assertEquals(
30+
singular,
31+
AbstractModelloGenerator.singular(plural),
32+
"singular of: " + plural + " should be: " + singular);
33+
}
34+
}

modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/JavaModelloGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,7 +1665,7 @@ private void createAdder(ModelAssociation modelAssociation, JClass jClass, boole
16651665

16661666
jClass.addMethod(adder);
16671667
} else {
1668-
String adderName = "add" + singular(capitalise(singular(fieldName)));
1668+
String adderName = "add" + capitalise(singular(fieldName));
16691669

16701670
JMethod adder;
16711671
if (isBuilderMethod) {
@@ -1711,7 +1711,7 @@ private void createAdder(ModelAssociation modelAssociation, JClass jClass, boole
17111711
return;
17121712
}
17131713

1714-
JMethod remover = new JMethod("remove" + singular(capitalise(fieldName)));
1714+
JMethod remover = new JMethod("remove" + capitalise(singular(fieldName)));
17151715

17161716
remover.addParameter(new JParameter(addType, parameterName));
17171717

0 commit comments

Comments
 (0)