Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup NamingHelper, simplified method toSQLNameDefault #401

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions library/src/main/java/com/orm/util/NamingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

public class NamingHelper {

private static final char UNDER_SCORE = '_';

/**
* Converts a given CamelCasedString to UPPER_CASE_UNDER_SCORE.
*
Expand All @@ -29,27 +31,18 @@ public static String toSQLNameDefault(String camelCased) {

for (int i = 0; i < buf.length; i++) {
char prevChar = (i > 0) ? buf[i - 1] : 0;
char c = buf[i];
char currentChar = buf[i];
char nextChar = (i < buf.length - 1) ? buf[i + 1] : 0;
boolean isFirstChar = (i == 0);

if (isFirstChar || Character.isLowerCase(c) || Character.isDigit(c)) {
sb.append(Character.toUpperCase(c));
} else if (Character.isUpperCase(c)) {
if (Character.isLetterOrDigit(prevChar)) {
if (Character.isLowerCase(prevChar)) {
sb.append('_').append(c);
} else if (nextChar > 0 && Character.isLowerCase(nextChar)) {
sb.append('_').append(c);
} else {
sb.append(c);
}
} else {
sb.append(c);
}
if (i == 0 || Character.isLowerCase(currentChar) || Character.isDigit(currentChar)) {
sb.append(Character.toUpperCase(currentChar));
} else if (Character.isUpperCase(currentChar) &&
(Character.isLowerCase(prevChar) || Character.isLowerCase(nextChar))) {
sb.append(UNDER_SCORE).append(currentChar);
} else {
sb.append(currentChar);
}
}

return sb.toString();
}

Expand All @@ -76,13 +69,13 @@ public static String toSQLName(Field field) {
*
* @param table the generic {@link java.lang.Class<T>} that defines a database table
* @return if the given class is annotated with {@link com.orm.dsl.Table} then the value for
* {@link com.orm.dsl.Table#name()} will be returned. Else, the class' simple name will
* {@link com.orm.dsl.Table#name()} will be returned. Else, the class' simple name will
* be converted from CamelCase to UNDER_SCORE notation
*/
public static String toSQLName(Class<?> table) {
if (table.isAnnotationPresent(Table.class)) {
Table annotation = table.getAnnotation(Table.class);
if ("".equals(annotation.name())) {
if (annotation.name().isEmpty()) {
return NamingHelper.toSQLNameDefault(table.getSimpleName());
}
return annotation.name();
Expand Down