Skip to content

Commit

Permalink
Use classes instead of enum because MVEL does not like enum with abst…
Browse files Browse the repository at this point in the history
…ract methods
  • Loading branch information
vietj committed Jan 27, 2015
1 parent da82f75 commit fa84d57
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 102 deletions.
43 changes: 43 additions & 0 deletions src/main/java/io/vertx/codegen/CamelCase.java
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);
}
}
}
124 changes: 22 additions & 102 deletions src/main/java/io/vertx/codegen/Case.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
Expand All @@ -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;
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/io/vertx/codegen/KebabCase.java
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, "\\-");
}
}
39 changes: 39 additions & 0 deletions src/main/java/io/vertx/codegen/QualifiedCase.java
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, "\\.");
}
}
37 changes: 37 additions & 0 deletions src/main/java/io/vertx/codegen/SnakeCase.java
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, "_");
}
}

0 comments on commit fa84d57

Please sign in to comment.