Skip to content

Commit da2f785

Browse files
Restore singular method behavior like was in version 2.4.0
fix #502
1 parent 659570f commit da2f785

File tree

3 files changed

+16
-163
lines changed

3 files changed

+16
-163
lines changed

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

Lines changed: 8 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -61,60 +61,6 @@ public abstract class AbstractModelloGenerator implements ModelloGenerator {
6161

6262
private static final Map<String, String> PLURAL_EXCEPTIONS = new HashMap<>();
6363

64-
static {
65-
// Irregular names
66-
PLURAL_EXCEPTIONS.put("children", "child");
67-
PLURAL_EXCEPTIONS.put("feet", "foot");
68-
PLURAL_EXCEPTIONS.put("geese", "goose");
69-
PLURAL_EXCEPTIONS.put("indices", "index");
70-
PLURAL_EXCEPTIONS.put("men", "man");
71-
PLURAL_EXCEPTIONS.put("mice", "mouse");
72-
PLURAL_EXCEPTIONS.put("people", "person");
73-
PLURAL_EXCEPTIONS.put("teeth", "tooth");
74-
PLURAL_EXCEPTIONS.put("women", "woman");
75-
76-
// Invariant names
77-
PLURAL_EXCEPTIONS.put("aircraft", "aircraft");
78-
PLURAL_EXCEPTIONS.put("bison", "bison");
79-
PLURAL_EXCEPTIONS.put("deer", "deer");
80-
PLURAL_EXCEPTIONS.put("elk", "elk");
81-
PLURAL_EXCEPTIONS.put("fish", "fish");
82-
PLURAL_EXCEPTIONS.put("series", "series");
83-
PLURAL_EXCEPTIONS.put("sheep", "sheep");
84-
PLURAL_EXCEPTIONS.put("species", "species");
85-
86-
// Special "oes" exceptions
87-
PLURAL_EXCEPTIONS.put("buffaloes", "buffalo");
88-
PLURAL_EXCEPTIONS.put("cargoes", "cargo");
89-
PLURAL_EXCEPTIONS.put("echoes", "echo");
90-
PLURAL_EXCEPTIONS.put("goes", "go");
91-
PLURAL_EXCEPTIONS.put("haloes", "halo");
92-
PLURAL_EXCEPTIONS.put("heroes", "hero");
93-
PLURAL_EXCEPTIONS.put("mosquitoes", "mosquito");
94-
PLURAL_EXCEPTIONS.put("noes", "no");
95-
PLURAL_EXCEPTIONS.put("potatoes", "potato");
96-
PLURAL_EXCEPTIONS.put("tomatoes", "tomato");
97-
PLURAL_EXCEPTIONS.put("torpedoes", "torpedo");
98-
PLURAL_EXCEPTIONS.put("vetoes", "veto");
99-
PLURAL_EXCEPTIONS.put("volcanoes", "volcano");
100-
101-
// Special "ses" exceptions
102-
PLURAL_EXCEPTIONS.put("horses", "horse");
103-
PLURAL_EXCEPTIONS.put("licenses", "license");
104-
PLURAL_EXCEPTIONS.put("phases", "phase");
105-
106-
// Special "zzes" exceptions
107-
PLURAL_EXCEPTIONS.put("fezzes", "fez");
108-
PLURAL_EXCEPTIONS.put("whizzes", "whiz");
109-
110-
// Special "ies" exceptions
111-
PLURAL_EXCEPTIONS.put("movies", "movie");
112-
113-
// Special "ves" exceptions
114-
PLURAL_EXCEPTIONS.put("archives", "archive");
115-
PLURAL_EXCEPTIONS.put("relatives", "relative");
116-
}
117-
11864
private Model model;
11965

12066
private File outputDirectory;
@@ -257,67 +203,27 @@ protected String capitalise(String str) {
257203
}
258204

259205
public static String singular(String name) {
260-
if (name == null || name.isEmpty()) return name;
261-
262-
String lower = name.toLowerCase();
263-
264-
if (!lower.equals(name)) {
265-
// we can have a case like otherArchives
266-
String[] split = splitByUpperCase(name);
267-
if (split != null && PLURAL_EXCEPTIONS.containsKey(split[1])) {
268-
String plural = PLURAL_EXCEPTIONS.get(split[1]);
269-
return split[0] + Character.toUpperCase(plural.charAt(0)) + plural.substring(1);
270-
}
206+
if (StringUtils.isEmpty(name)) {
207+
return name;
271208
}
272209

273-
if (PLURAL_EXCEPTIONS.containsKey(lower)) {
274-
return PLURAL_EXCEPTIONS.get(lower);
210+
if (PLURAL_EXCEPTIONS.containsKey(name)) {
211+
return PLURAL_EXCEPTIONS.get(name);
275212
}
276213

277-
// Suffix-based rules
278-
if (lower.endsWith("ies") && name.length() > 3) {
214+
if (name.endsWith("ies")) {
279215
return name.substring(0, name.length() - 3) + "y";
280-
}
281-
if (lower.endsWith("aves") || lower.endsWith("lves") || lower.endsWith("rves")) {
282-
return name.substring(0, name.length() - 3) + "f";
283-
}
284-
if (lower.endsWith("ves") && !lower.endsWith("fves")) {
285-
return name.substring(0, name.length() - 3) + "fe";
286-
}
287-
if (lower.endsWith("zzes")) {
288-
return name.substring(0, name.length() - 2);
289-
}
290-
if (lower.endsWith("sses")) {
291-
return name.substring(0, name.length() - 2);
292-
}
293-
if (lower.endsWith("ses")) {
294-
return name.substring(0, name.length() - 2);
295-
}
296-
if (lower.endsWith("ches") || lower.endsWith("shes")) {
216+
} else if (name.endsWith("es") && name.endsWith("ches")) {
297217
return name.substring(0, name.length() - 2);
298-
}
299-
if (lower.endsWith("xes")) {
218+
} else if (name.endsWith("xes")) {
300219
return name.substring(0, name.length() - 2);
301-
}
302-
if (lower.endsWith("oes")) {
303-
return name.substring(0, name.length() - 1);
304-
}
305-
if (lower.endsWith("s") && name.length() > 1) {
220+
} else if (name.endsWith("s") && (name.length() != 1)) {
306221
return name.substring(0, name.length() - 1);
307222
}
308223

309224
return name;
310225
}
311226

312-
private static String[] splitByUpperCase(String name) {
313-
for (int i = name.length() - 1; i >= 0; i--) {
314-
if (Character.isUpperCase(name.charAt(i))) {
315-
return new String[] {name.substring(0, i), name.substring(i).toLowerCase()};
316-
}
317-
}
318-
return null;
319-
}
320-
321227
public static String uncapitalise(String str) {
322228
if (StringUtils.isEmpty(str)) {
323229
return str;

modello-core/src/test/java/org/codehaus/modello/plugin/AbstractModelloGeneratorTest.java

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,10 @@ class AbstractModelloGeneratorTest {
1313
"s,s",
1414

1515
// Known exceptions
16-
"men, man",
17-
"women, woman",
18-
"children,child",
19-
"mice, mouse",
20-
"people, person",
21-
"teeth, tooth",
22-
"feet, foot",
23-
"geese, goose",
24-
"series, series",
25-
"species, species",
2616
"sheep, sheep",
2717
"fish, fish",
2818
"deer, deer",
2919
"aircraft, aircraft",
30-
"heroes, hero",
31-
"potatoes, potato",
32-
"tomatoes, tomato",
33-
"echoes, echo",
34-
"vetoes, veto",
35-
"torpedoes, torpedo",
36-
"cargoes, cargo",
37-
"haloes, halo",
38-
"mosquitoes, mosquito",
39-
"buffaloes, buffalo",
4020
"bison, bison",
4121
"elk, elk",
4222

@@ -47,70 +27,44 @@ class AbstractModelloGeneratorTest {
4727
"toes, toe",
4828
"foes, foe",
4929
"oboes, oboe",
50-
"noes, no",
5130
"boxes, box",
52-
"wishes, wish",
53-
"dishes, dish",
54-
"brushes, brush",
55-
"classes, class",
56-
"buzzes, buzz",
5731
"cars, car",
5832
"dogs, dog",
5933
"cats, cat",
6034
"horses, horse",
61-
"fezzes, fez",
62-
"whizzes, whiz",
6335
"foxes, fox",
6436

6537
// Some test cases with different rules
6638
"archives, archive",
6739
"otherArchives, otherArchive",
6840
"Archives, Archive",
69-
"wolves, wolf",
70-
"knives, knife",
71-
"leaves, leaf",
72-
"wives, wife",
73-
"lives, life",
7441
"babies, baby",
7542
"parties, party",
7643
"cities, city",
77-
"buses, bus",
7844
"boxes, box",
7945
"churches, church",
8046
"matches, match",
8147
"watches, watch",
8248
"riches, rich",
83-
"dresses, dress",
84-
"crosses, cross",
8549
"lunches, lunch",
8650
"relatives, relative",
8751

8852
// More edge cases
89-
"heroes, hero",
90-
"vetoes, veto",
91-
"torpedoes, torpedo",
92-
"tomatoes, tomato",
93-
"potatoes, potato",
94-
"echoes, echo",
95-
"mosquitoes, mosquito",
96-
"buffaloes, buffalo",
97-
"volcanoes, volcano",
98-
"goes, go",
99-
"indices, index",
10053
"phases, phase",
101-
"kisses, kiss",
102-
"movies, movie",
10354
"shoes, shoe",
10455

10556
// other examples
106-
"aliases, alias",
10757
"ids, id",
10858
"licenses, license",
10959
"repositories, repository",
11060
"roles, role",
61+
62+
// existing non grammar conversions
63+
"superclasses, superclasse",
64+
"classes, classe"
11165
})
11266
@ParameterizedTest
113-
public void testSingular(String plural, String singular) {
67+
void testSingular(String plural, String singular) {
11468
assertEquals(
11569
singular,
11670
AbstractModelloGenerator.singular(plural),

modello-maven-plugin/src/main/java/org/codehaus/modello/maven/AbstractModelloGeneratorMojo.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ public abstract class AbstractModelloGeneratorMojo extends AbstractMojo {
118118
/**
119119
* Additional exceptions to the singularization rules, changing plural noun to singular.
120120
* <p>
121-
* As a kay we provide plural noun and as value we provide singular noun, eg:
121+
* As a key we provide plural noun and as value we provide singular noun, example:
122122
* <pre>
123-
* <kisses>kiss</kisses>
123+
* &lt;kisses&gt;kiss&lt;/kisses&gt;
124124
* </pre>
125125
*
126126
* @since 2.5.0
@@ -193,7 +193,7 @@ public void execute() throws MojoExecutionException {
193193

194194
parameters.put(ModelloParameterConstants.PACKAGE_WITH_VERSION, Boolean.toString(packageWithVersion));
195195

196-
parameters.put(ModelloParameterConstants.PLURAL_EXCEPTIONS, keysToLower(pluralExceptions));
196+
parameters.put(ModelloParameterConstants.PLURAL_EXCEPTIONS, pluralExceptions);
197197

198198
if (!packagedVersions.isEmpty()) {
199199
parameters.put(ModelloParameterConstants.ALL_VERSIONS, StringUtils.join(packagedVersions.iterator(), ","));
@@ -239,13 +239,6 @@ public void execute() throws MojoExecutionException {
239239
}
240240
}
241241

242-
private Object keysToLower(Map<String, String> maps) {
243-
if (maps == null) {
244-
return null;
245-
}
246-
return maps.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().toLowerCase(), Map.Entry::getValue));
247-
}
248-
249242
/**
250243
* Performs execute on a single specified model.
251244
*/

0 commit comments

Comments
 (0)