Skip to content

Commit

Permalink
Updated Comments + Added More Cases
Browse files Browse the repository at this point in the history
  • Loading branch information
MeAlam1 committed Oct 3, 2024
1 parent db02629 commit 9eff5e2
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
import software.bluelib.utils.logging.BaseLogger;

/**
* A {@code class} for converting strings between various naming conventions:
* camelCase, PascalCase, snake_case, and kebab-case.
* A {@code public class} for converting strings between various naming conventions:
* camelCase, PascalCase, snake_case, kebab-case, UPPER_SNAKE_CASE, Train-Case, flatcase, and COBOL-CASE.
* <p>
* Key Methods:
* <ul>
* <li>{@link #toCamelCase(String)} - Converts input to camelCase.</li>
* <li>{@link #toPascalCase(String)} - Converts input to PascalCase.</li>
* <li>{@link #toSnakeCase(String)} - Converts input to snake_case.</li>
* <li>{@link #toKebabCase(String)} - Converts input to kebab-case.</li>
* <li>{@link #toUpperSnakeCase(String)} - Converts input to UPPER_SNAKE_CASE.</li>
* <li>{@link #toTrainCase(String)} - Converts input to Train-Case.</li>
* <li>{@link #toFlatcase(String)} - Converts input to flatcase.</li>
* <li>{@link #toCobolCase(String)} - Converts input to COBOL-CASE.</li>
* </ul>
*
* @author MeAlam
Expand All @@ -34,26 +38,26 @@ public class CaseConverterUtils {
*/
public static String toCamelCase(String pInput) {
if (pInput == null || pInput.isEmpty()) {
BaseLogger.log(BaseLogLevel.INFO,"Input for toCamelCase is null or empty.");
BaseLogger.log(BaseLogLevel.INFO, "Input for toCamelCase is null or empty.");
return pInput;
}

if (Character.isUpperCase(pInput.charAt(0)) && !pInput.contains("_") && !pInput.contains("-")) {
BaseLogger.log(BaseLogLevel.INFO,"Input detected as PascalCase.");
BaseLogger.log(BaseLogLevel.INFO, "Input detected as PascalCase.");
return pInput.substring(0, 1).toLowerCase() + pInput.substring(1);
}

if (pInput.contains("_")) {
BaseLogger.log(BaseLogLevel.INFO,"Input detected as snake_case.");
BaseLogger.log(BaseLogLevel.INFO, "Input detected as snake_case.");
return convertUsingDelimiter(pInput, "_", true);
}

if (pInput.contains("-")) {
BaseLogger.log(BaseLogLevel.INFO,"Input detected as kebab-case.");
BaseLogger.log(BaseLogLevel.INFO, "Input detected as kebab-case.");
return convertUsingDelimiter(pInput, "-", true);
}

BaseLogger.log(BaseLogLevel.ERROR,"Input case is not recognized.");
BaseLogger.log(BaseLogLevel.ERROR, "Input case is not recognized.");
return pInput;
}

Expand All @@ -69,26 +73,26 @@ public static String toCamelCase(String pInput) {
*/
public static String toPascalCase(String pInput) {
if (pInput == null || pInput.isEmpty()) {
BaseLogger.log(BaseLogLevel.WARNING,"Input for toPascalCase is null or empty.");
BaseLogger.log(BaseLogLevel.WARNING, "Input for toPascalCase is null or empty.");
return pInput;
}

if (!pInput.contains("_") && !pInput.contains("-") && Character.isLowerCase(pInput.charAt(0))) {
BaseLogger.log(BaseLogLevel.INFO,"Input detected as camelCase.");
BaseLogger.log(BaseLogLevel.INFO, "Input detected as camelCase.");
return pInput.substring(0, 1).toUpperCase() + pInput.substring(1);
}

if (pInput.contains("_")) {
BaseLogger.log(BaseLogLevel.INFO,"Input detected as snake_case.");
BaseLogger.log(BaseLogLevel.INFO, "Input detected as snake_case.");
return convertUsingDelimiter(pInput, "_", false);
}

if (pInput.contains("-")) {
BaseLogger.log(BaseLogLevel.INFO,"Input detected as kebab-case.");
BaseLogger.log(BaseLogLevel.INFO, "Input detected as kebab-case.");
return convertUsingDelimiter(pInput, "-", false);
}

BaseLogger.log(BaseLogLevel.ERROR,"Input case is not recognized.");
BaseLogger.log(BaseLogLevel.ERROR, "Input case is not recognized.");
return pInput;
}

Expand All @@ -104,17 +108,15 @@ public static String toPascalCase(String pInput) {
*/
public static String toSnakeCase(String pInput) {
if (pInput == null || pInput.isEmpty()) {
BaseLogger.log(BaseLogLevel.WARNING,"Input for toSnakeCase is null or empty.");
BaseLogger.log(BaseLogLevel.WARNING, "Input for toSnakeCase is null or empty.");
return pInput;
}

String result = pInput.replaceAll("([a-z])([A-Z])", "$1_$2");

result = result.toLowerCase();

result = result.replace("-", "_");

BaseLogger.log(BaseLogLevel.SUCCESS,"Converted to snake_case: " + result);
BaseLogger.log(BaseLogLevel.SUCCESS, "Converted to snake_case: " + result);
return result;
}

Expand All @@ -130,54 +132,125 @@ public static String toSnakeCase(String pInput) {
*/
public static String toKebabCase(String pInput) {
if (pInput == null || pInput.isEmpty()) {
BaseLogger.log(BaseLogLevel.WARNING,"Input for toKebabCase is null or empty.");
BaseLogger.log(BaseLogLevel.WARNING, "Input for toKebabCase is null or empty.");
return pInput;
}

String result = pInput.replaceAll("([a-z])([A-Z])", "$1-$2");

result = result.toLowerCase();

result = result.replace("_", "-");

BaseLogger.log(BaseLogLevel.SUCCESS,"Converted to kebab-case: " + result);
BaseLogger.log(BaseLogLevel.SUCCESS, "Converted to kebab-case: " + result);
return result;
}

/**
* A {@link String} helper method to convert a string by splitting it using a given delimiter, then converting each part.
* A {@link String} that converts a given {@link String} to UPPER_SNAKE_CASE.
* <p>
* Converts camelCase, PascalCase, snake_case, and kebab-case to UPPER_SNAKE_CASE by adding underscores
* and converting all letters to uppercase.
*
* @param pInput {@link String} - The input string to be converted.
* @param pDelimiter {@link String} - The delimiter used for splitting the input string.
* @param pIsCamelCase {@code boolean} - Whether the result should be in camelCase (lowercase first letter).
* @return The converted string.
* @return The UPPER_SNAKE_CASE version of the input string.
* @author MeAlam
* @since 1.0.0
*/
private static String convertUsingDelimiter(String pInput, String pDelimiter, boolean pIsCamelCase) {
String[] parts = pInput.split(pDelimiter);
StringBuilder convertedString = new StringBuilder();
for (int i = 0; i < parts.length; i++) {
if (i == 0 && pIsCamelCase) {
convertedString.append(parts[i].toLowerCase());
} else {
convertedString.append(toProperCase(parts[i]));
}
public static String toUpperSnakeCase(String pInput) {
if (pInput == null || pInput.isEmpty()) {
BaseLogger.log(BaseLogLevel.WARNING, "Input for toUpperSnakeCase is null or empty.");
return pInput;
}
String result = convertedString.toString();
BaseLogger.log(BaseLogLevel.SUCCESS,"Converted: " + result);
return result;

String result = toSnakeCase(pInput);
return result.toUpperCase();
}

/**
* A {@link String} that converts a given {@link String} to Train-Case.
* <p>
* Converts camelCase, PascalCase, snake_case, and kebab-case to Train-Case by adding hyphens and
* capitalizing each word.
*
* @param pInput {@link String} - The input string to be converted.
* @return The Train-Case version of the input string.
* @author MeAlam
* @since 1.0.0
*/
public static String toTrainCase(String pInput) {
if (pInput == null || pInput.isEmpty()) {
BaseLogger.log(BaseLogLevel.WARNING, "Input for toTrainCase is null or empty.");
return pInput;
}

String result = toKebabCase(pInput).replace("-", " ");
return toCamelCase(result).replace(" ", "-");
}

/**
* A {@link String} that converts a given {@link String} to flatcase.
* <p>
* Converts all cases to flatcase by removing any delimiters and converting to lowercase.
*
* @param pInput {@link String} - The input string to be converted.
* @return The flatcase version of the input string.
* @author MeAlam
* @since 1.0.0
*/
public static String toFlatcase(String pInput) {
if (pInput == null || pInput.isEmpty()) {
BaseLogger.log(BaseLogLevel.WARNING, "Input for toFlatcase is null or empty.");
return pInput;
}

return pInput.replaceAll("[_-]", "").toLowerCase();
}

/**
* A {@link String} that converts a string to ProperCase, where the first letter is capitalized and the rest are lowercase.
* A {@link String} that converts a given {@link String} to COBOL-CASE.
* <p>
* Converts camelCase, PascalCase, snake_case, and kebab-case to COBOL-CASE by making all letters uppercase
* and replacing spaces with hyphens.
*
* @param pInput {@link String} - The input string to be converted.
* @return The ProperCase version of the input string.
* @return The COBOL-CASE version of the input string.
* @author MeAlam
* @since 1.0.0
*/
private static String toProperCase(String pInput) {
return pInput.substring(0, 1).toUpperCase() + pInput.substring(1).toLowerCase();
public static String toCobolCase(String pInput) {
if (pInput == null || pInput.isEmpty()) {
BaseLogger.log(BaseLogLevel.WARNING, "Input for toCobolCase is null or empty.");
return pInput;
}

String result = toKebabCase(pInput);
return result.toUpperCase();
}

/**
* A helper method that converts strings using a specified delimiter.
* <p>
* This method capitalizes the first letter of each word and joins them using the specified delimiter.
*
* @param pInput {@link String} - The input string to be converted.
* @param pDelim {@link String} - The delimiter used to split the input string.
* @param pCamel {@code boolean} - Indicates if the conversion is to camelCase.
* @return The converted string.
* @author MeAlam
* @since 1.0.0
*/
private static String convertUsingDelimiter(String pInput, String pDelim, boolean pCamel) {
String[] parts = pInput.split(pDelim);
StringBuilder sb = new StringBuilder();

for (String part : parts) {
if (pCamel && sb.isEmpty()) {
sb.append(part.substring(0, 1).toLowerCase());
} else {
sb.append(part.substring(0, 1).toUpperCase());
}
sb.append(part.substring(1).toLowerCase());
}

return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,36 @@
* @since 1.0.0
*/
public class BaseLogLevel {
/**
* Standard informational log level.
* @Co-author MeAlam
* @since 1.0.0
*/
public static final Level INFO = new Level("INFO", Level.INFO.intValue()) {};
/**
* Log level for error messages.
* @Co-author MeAlam
* @since 1.0.0
*/
public static final Level ERROR = new Level("ERROR", Level.SEVERE.intValue()) {};
/**
* Log level for warning messages.
* @Co-author MeAlam
* @since 1.0.0
*/
public static final Level WARNING = new Level("WARNING", Level.WARNING.intValue()) {};

/**
* Custom log level for indicating successful operations.
* @Co-author MeAlam
* @since 1.0.0
*/
public static final Level SUCCESS = new Level("SUCCESS", Level.INFO.intValue() + 50) {};

/**
* Custom log level specific to BlueLib.
* @Co-author MeAlam
* @since 1.0.0
*/
public static final Level BLUELIB = new Level("BlueLib Developer", Level.INFO.intValue() + 50) {};
}
Loading

0 comments on commit 9eff5e2

Please sign in to comment.