org.joinedworkz.common.helper.NameHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-base Show documentation
Show all versions of common-base Show documentation
DSL based modeling framework - facilities common base
package org.joinedworkz.common.helper;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class NameHelper {
@Inject
private Inflector inflector;
public boolean hasValue(String s) {
return s != null && s.length() > 0;
}
public String toFirstUpper(String s) {
if (hasValue(s)) {
char first = Character.toUpperCase(s.charAt(0));
return first + s.substring(1);
}
return s;
}
public String underLineSeperatedToCamelCase(String s) {
return charSeperatedToCamelCase(s, '_', false);
}
public String spaceSeperatedToCamelCase(String s) {
return charSeperatedToCamelCase(s, ' ', false);
}
public String charSeperatedToCamelCase(String s, char c, boolean keepExistingCamelCase) {
if (s == null || s.length() == 0) {
return s;
}
StringTokenizer columnNameTokenizer = new StringTokenizer(s,Character.valueOf(c).toString());
boolean first=true;
StringBuilder buffer = new StringBuilder();
while (columnNameTokenizer.hasMoreTokens()) {
String token = columnNameTokenizer.nextToken();
if (!keepExistingCamelCase) {
token = token.toLowerCase();
}
if (!first) {
token = toFirstUpper(token);
} else {
first = false;
}
buffer.append(token);
}
return buffer.toString();
}
public String camelCaseToUnderLineSeperated(String s) {
if (s == null || s.length() == 0) {
return s;
}
StringBuilder buffer = new StringBuilder();
String[] nameParts = splitByCamelCase(s);
boolean first=true;
boolean previousWasUnderscore = false;
for (String namePart : nameParts) {
boolean isUnderscore = "_".equals(namePart);
if (!first && !previousWasUnderscore && !isUnderscore) {
buffer.append('_');
} else {
first = false;
}
buffer.append(namePart.toLowerCase());
previousWasUnderscore = isUnderscore;
}
return buffer.toString();
}
public String camelCaseToDashSeperated(String s) {
if (s == null || s.length() == 0) {
return s;
}
StringBuilder buffer = new StringBuilder();
String[] nameParts = splitByCamelCase(s);
boolean first=true;
boolean previousWasUnderscore = false;
for (String namePart : nameParts) {
boolean isUnderscore = "-".equals(namePart);
if (!first && !previousWasUnderscore && !isUnderscore) {
buffer.append('-');
} else {
first = false;
}
buffer.append(namePart.toLowerCase());
previousWasUnderscore = isUnderscore;
}
return buffer.toString();
}
public String camelCaseToSpaceSeperated(String s) {
if (s == null || s.length() == 0) {
return s;
}
StringBuilder buffer = new StringBuilder();
String[] nameParts = splitByCamelCase(s);
boolean first=true;
for (String namePart : nameParts) {
if (!first) {
buffer.append(' ');
} else {
first = false;
}
buffer.append(namePart);
}
return buffer.toString();
}
public String dashSeperatedToCamelCase(String s) {
return charSeperatedToCamelCase(s, '-', false);
}
public String[] splitByCamelCase(String str) {
return splitByCharacterType(str, true);
}
protected String[] splitByCharacterType(String str, boolean camelCase) {
if (str == null) {
return null;
}
if (str.length() == 0) {
return new String[] {};
}
char[] c = str.toCharArray();
List list = new ArrayList();
int tokenStart = 0;
int currentType = Character.getType(c[tokenStart]);
for (int pos = tokenStart + 1; pos < c.length; pos++) {
int type = Character.getType(c[pos]);
if (type == currentType) {
continue;
}
if (camelCase && type == Character.LOWERCASE_LETTER && currentType == Character.UPPERCASE_LETTER) {
int newTokenStart = pos - 1;
if (newTokenStart != tokenStart) {
list.add(new String(c, tokenStart, newTokenStart - tokenStart));
tokenStart = newTokenStart;
}
} else {
list.add(new String(c, tokenStart, pos - tokenStart));
tokenStart = pos;
}
currentType = type;
}
list.add(new String(c, tokenStart, c.length - tokenStart));
return list.toArray(new String[list.size()]);
}
public String abbreviateCamelCase(String s, int maxLength) {
if (s == null || s.length() == 0) {
return s;
}
StringTokenizer columnNameTokenizer = new StringTokenizer(s,"_");
boolean first=true;
StringBuilder buffer = new StringBuilder();
while (columnNameTokenizer.hasMoreTokens()) {
String token = columnNameTokenizer.nextToken();
if (!first) {
buffer.append('_');
} else {
first = false;
}
buffer.append(limit(token, maxLength));
}
return buffer.toString();
}
public String camelCaseToSinglar(String s) {
if (s != null) {
String[] parts = splitByCamelCase(s);
String lastPart = parts[parts.length -1];
StringBuilder buffer = new StringBuilder();
for (int i=0;imaxLength) {
return s.substring(0, maxLength);
} else {
return s;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy