com.jashmore.sqs.util.string.StringUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-utils Show documentation
Show all versions of common-utils Show documentation
Utility methods for dealing with basic functionality for this library, split out so so it can be consumed by other extensions
package com.jashmore.sqs.util.string;
import lombok.experimental.UtilityClass;
@UtilityClass
public class StringUtils {
/**
* Given an identifier that may be camel case with a range of characters, it will create an identifier that only contains lowercase letters, numbers
* and hyphens.
*
* For example given the following inputs, the following outputs will be generated:
*
* - MyClassNameWithMethod: my-class-name-with-method
* - MySQSNameWithMethod: my-sqs-name-with-method (note that it did not create my-sqsn-ame-with-method
* - MySQS-method: my-sqs-method
* - MySQS-?$$-method: my-sqs-method
*
*
* @param identifier the identifier to transform
* @return the identifier in hyphen case
*/
@SuppressWarnings("checkstyle:LocalVariableName")
public String toLowerHyphenCase(final String identifier) {
final StringBuilder stringBuilder = new StringBuilder();
char[] characters = identifier.toCharArray();
boolean previousCharacterIsSeparator = false;
boolean previousCharacterIsUpperCase = false;
for (int i = 0; i < characters.length; ++i) {
char c = characters[i];
if (Character.isLetter(c)) {
if (Character.isUpperCase(c)) {
if (i > 0 && !previousCharacterIsSeparator && !previousCharacterIsUpperCase) {
stringBuilder.append('-');
}
if (previousCharacterIsUpperCase && i + 1 < characters.length && Character.isLowerCase(characters[i + 1])) {
stringBuilder.append('-');
}
stringBuilder.append(Character.toLowerCase(c));
previousCharacterIsUpperCase = true;
} else {
stringBuilder.append(c);
previousCharacterIsUpperCase = false;
}
previousCharacterIsSeparator = false;
} else if (Character.isDigit(c)) {
stringBuilder.append(c);
previousCharacterIsSeparator = false;
previousCharacterIsUpperCase = false;
} else {
if (!previousCharacterIsSeparator) {
stringBuilder.append('-');
}
previousCharacterIsSeparator = true;
previousCharacterIsUpperCase = false;
}
}
return stringBuilder.toString();
}
}