forked from eclipse-vertx/vertx-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use classes instead of enum because MVEL does not like enum with abst…
…ract methods
- Loading branch information
Showing
5 changed files
with
180 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.vertx.codegen; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Julien Viet</a> | ||
*/ | ||
public class CamelCase extends Case { | ||
|
||
@Override | ||
public String name() { | ||
return "CAMEL"; | ||
} | ||
|
||
@Override | ||
public String format(Iterable<String> atoms) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (String atom : atoms) { | ||
if (atom.length() > 0) { | ||
char c = atom.charAt(0); | ||
if (Character.isLowerCase(c)) { | ||
sb.append(Character.toUpperCase(c)); | ||
sb.append(atom, 1, atom.length()); | ||
} else { | ||
sb.append(atom); | ||
} | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
@Override | ||
public List<String> parse(String name) { | ||
String[] atoms = name.split("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])"); | ||
if (atoms.length == 1 && atoms[0].isEmpty()) { | ||
return Collections.emptyList(); | ||
} else { | ||
return Arrays.asList(atoms); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,123 +5,39 @@ | |
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Julien Viet</a> | ||
*/ | ||
public enum Case { | ||
public abstract class Case { | ||
|
||
/** | ||
* Camel case, for instance {@literal FooBar}. | ||
*/ | ||
CAMEL() { | ||
@Override | ||
public String format(Iterable<String> atoms) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (String atom : atoms) { | ||
if (atom.length() > 0) { | ||
char c = atom.charAt(0); | ||
if (Character.isLowerCase(c)) { | ||
sb.append(Character.toUpperCase(c)); | ||
sb.append(atom, 1, atom.length()); | ||
} else { | ||
sb.append(atom); | ||
} | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
@Override | ||
public List<String> parse(String name) { | ||
String[] atoms = name.split("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])"); | ||
if (atoms.length == 1 && atoms[0].isEmpty()) { | ||
return Collections.emptyList(); | ||
} else { | ||
return Arrays.asList(atoms); | ||
} | ||
} | ||
}, | ||
public static final Case CAMEL = new CamelCase(); | ||
|
||
QUALIFIED() { | ||
@Override | ||
public String format(Iterable<String> atoms) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (String atom : atoms) { | ||
if (atom.length() > 0) { | ||
if (sb.length() > 0) { | ||
sb.append('.'); | ||
} | ||
sb.append(atom); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
private final Pattern validator = Pattern.compile("(?:\\p{Alnum}|(?:(?<=\\p{Alnum})\\.(?=\\p{Alnum})))*"); | ||
@Override | ||
public List<String> parse(String name) { | ||
if (!validator.matcher(name).matches()) { | ||
throw new IllegalArgumentException("Invalid qualified case:" + name); | ||
} | ||
return split(name, "\\."); | ||
} | ||
}, | ||
/** | ||
* Java full qualified case, for instance {@literal foo.bar} | ||
*/ | ||
public static final Case QUALIFIED = new QualifiedCase(); | ||
|
||
/** | ||
* Kebab case, for instance {@literal foo-bar}. | ||
*/ | ||
KEBAB() { | ||
@Override | ||
public String format(Iterable<String> atoms) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (String atom : atoms) { | ||
if (atom.length() > 0) { | ||
if (sb.length() > 0) { | ||
sb.append('-'); | ||
} | ||
sb.append(atom.toLowerCase()); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
private final Pattern validator = Pattern.compile("(?:\\p{Alnum}|(?:(?<=\\p{Alnum})-(?=\\p{Alnum})))*"); | ||
@Override | ||
public List<String> parse(String name) { | ||
if (!validator.matcher(name).matches()) { | ||
throw new IllegalArgumentException("Invalid kebab case:" + name); | ||
} | ||
return split(name, "\\-"); | ||
} | ||
}, | ||
public static final Case KEBAB = new KebabCase(); | ||
|
||
/** | ||
* Snake case, for instance {@literal foo_bar}. | ||
*/ | ||
SNAKE() { | ||
@Override | ||
public String format(Iterable<String> atoms) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (String atom : atoms) { | ||
if (atom.length() > 0) { | ||
if (sb.length() > 0) { | ||
sb.append('_'); | ||
} | ||
sb.append(atom.toLowerCase()); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
private final Pattern validator = Pattern.compile("(?:\\p{Alnum}|(?:(?<=\\p{Alnum})_(?=\\p{Alnum})))*"); | ||
@Override | ||
public List<String> parse(String name) { | ||
if (!validator.matcher(name).matches()) { | ||
throw new IllegalArgumentException("Invalid snake case:" + name); | ||
} | ||
return split(name, "_"); | ||
} | ||
}; | ||
public static final Case SNAKE = new SnakeCase(); | ||
|
||
public abstract String format(Iterable<String> atoms); | ||
public String name() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public String format(Iterable<String> atoms) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
/** | ||
* Parse the {@code name} argument and returns a list of the name atoms. | ||
|
@@ -130,9 +46,11 @@ public List<String> parse(String name) { | |
* @return the name atoms | ||
* @throws IllegalArgumentException if the name has a syntax error | ||
*/ | ||
public abstract List<String> parse(String name); | ||
public List<String> parse(String name) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
private static List<String> split(String s, String regex) { | ||
protected static List<String> split(String s, String regex) { | ||
String[] atoms = s.split(regex); | ||
if (atoms.length == 1 && atoms[0].isEmpty()) { | ||
return Collections.emptyList(); | ||
|
@@ -141,12 +59,14 @@ private static List<String> split(String s, String regex) { | |
} | ||
} | ||
|
||
private static final Case[] values = { CAMEL, QUALIFIED, SNAKE, KEBAB }; | ||
|
||
/** | ||
* Useful for testing the method kind, allows to do method.kind == METHOD_HANDLER instead of method.kind.name() == "HANDLER" | ||
*/ | ||
public static Map<String, Case> vars() { | ||
HashMap<String, Case> vars = new HashMap<>(); | ||
for (Case _case : Case.values()) { | ||
for (Case _case : Arrays.asList(CAMEL, QUALIFIED, SNAKE, KEBAB)) { | ||
vars.put("CASE_" + _case.name(), _case); | ||
} | ||
return vars; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.vertx.codegen; | ||
|
||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Julien Viet</a> | ||
*/ | ||
public class KebabCase extends Case { | ||
|
||
private final Pattern validator = Pattern.compile("(?:\\p{Alnum}|(?:(?<=\\p{Alnum})-(?=\\p{Alnum})))*"); | ||
|
||
@Override | ||
public String name() { | ||
return "KEBAB"; | ||
} | ||
|
||
@Override | ||
public String format(Iterable<String> atoms) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (String atom : atoms) { | ||
if (atom.length() > 0) { | ||
if (sb.length() > 0) { | ||
sb.append('-'); | ||
} | ||
sb.append(atom.toLowerCase()); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
@Override | ||
public List<String> parse(String name) { | ||
if (!validator.matcher(name).matches()) { | ||
throw new IllegalArgumentException("Invalid kebab case:" + name); | ||
} | ||
return split(name, "\\-"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.vertx.codegen; | ||
|
||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Julien Viet</a> | ||
*/ | ||
public class QualifiedCase extends Case { | ||
|
||
private final Pattern validator = Pattern.compile("(?:\\p{Alnum}|(?:(?<=\\p{Alnum})\\.(?=\\p{Alnum})))*"); | ||
|
||
@Override | ||
public String name() { | ||
return "QUALIFIED"; | ||
} | ||
|
||
@Override | ||
public String format(Iterable<String> atoms) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (String atom : atoms) { | ||
if (atom.length() > 0) { | ||
if (sb.length() > 0) { | ||
sb.append('.'); | ||
} | ||
sb.append(atom); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
@Override | ||
public List<String> parse(String name) { | ||
if (!validator.matcher(name).matches()) { | ||
throw new IllegalArgumentException("Invalid qualified case:" + name); | ||
} | ||
return split(name, "\\."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.vertx.codegen; | ||
|
||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Julien Viet</a> | ||
*/ | ||
public class SnakeCase extends Case { | ||
|
||
@Override | ||
public String name() { | ||
return "SNAKE"; | ||
} | ||
|
||
@Override | ||
public String format(Iterable<String> atoms) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (String atom : atoms) { | ||
if (atom.length() > 0) { | ||
if (sb.length() > 0) { | ||
sb.append('_'); | ||
} | ||
sb.append(atom.toLowerCase()); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
private final Pattern validator = Pattern.compile("(?:\\p{Alnum}|(?:(?<=\\p{Alnum})_(?=\\p{Alnum})))*"); | ||
@Override | ||
public List<String> parse(String name) { | ||
if (!validator.matcher(name).matches()) { | ||
throw new IllegalArgumentException("Invalid snake case:" + name); | ||
} | ||
return split(name, "_"); | ||
} | ||
} |