Java String Camel Case To camelToLisp(final String pString)

Here you can find the source of camelToLisp(final String pString)

Description

Converts the input string from camel-style (Java in-fix) naming convention to Lisp-style naming convention (hyphen delimitted, all lower case).

License

Open Source License

Parameter

Parameter Description
pString the camel-style input string

Exception

Parameter Description
IllegalArgumentException if pString == null

Return

the string converted to lisp-style naming convention

Declaration


public static String camelToLisp(final String pString) 

Method Source Code

//package com.java2s;

public class Main {
    /**/* w  w w  .ja  v  a2  s  .  co m*/
     * Converts the input string
     * from camel-style (Java in-fix) naming convention
     * to Lisp-style naming convention (hyphen delimitted, all lower case).
     * Other characters in the string are left untouched.
     * <p/>
     * Eg.
     * {@code "foo" => "foo"},
     * {@code "fooBar" => "foo-bar"},
     * {@code "myURL" => "my-url"},
     * {@code "HttpRequestWrapper" => "http-request-wrapper"}
     * {@code "HttpURLConnection" => "http-url-connection"}
     * {@code "my45Caliber" => "my-45-caliber"}
     * {@code "allready-lisp" => "allready-lisp"}
     *
     * @param pString the camel-style input string
     * @return the string converted to lisp-style naming convention
     * @throws IllegalArgumentException if {@code pString == null}
     * @see #lispToCamel(String)
     */
    // TODO: RefactorMe!
    public static String camelToLisp(final String pString) {
        if (pString == null) {
            throw new IllegalArgumentException("string == null");
        }
        if (pString.length() == 0) {
            return pString;
        }

        StringBuilder buf = null;
        int lastPos = 0;
        boolean inCharSequence = false;
        boolean inNumberSequence = false;

        // NOTE: Start at index 1, as first letter should never be hyphen
        for (int i = 1; i < pString.length(); i++) {
            char current = pString.charAt(i);
            if (Character.isUpperCase(current)) {
                // Init buffer if necessary
                if (buf == null) {
                    buf = new StringBuilder(pString.length() + 3);// Allow for some growth
                }

                if (inNumberSequence) {
                    // Sequence end
                    inNumberSequence = false;

                    buf.append(pString.substring(lastPos, i));
                    if (current != '-') {
                        buf.append('-');
                    }
                    lastPos = i;
                    continue;
                }

                // Treat multiple uppercase chars as single word
                char previous = pString.charAt(i - 1);
                if (i == lastPos || Character.isUpperCase(previous)) {
                    inCharSequence = true;
                    continue;
                }

                // Append word
                buf.append(pString.substring(lastPos, i).toLowerCase());
                if (previous != '-') {
                    buf.append('-');
                }
                buf.append(Character.toLowerCase(current));

                lastPos = i + 1;
            } else if (Character.isDigit(current)) {
                // Init buffer if necessary
                if (buf == null) {
                    buf = new StringBuilder(pString.length() + 3);// Allow for some growth
                }

                if (inCharSequence) {
                    // Sequence end
                    inCharSequence = false;

                    buf.append(pString.substring(lastPos, i).toLowerCase());
                    if (current != '-') {
                        buf.append('-');
                    }
                    lastPos = i;
                    continue;
                }

                // Treat multiple digits as single word
                char previous = pString.charAt(i - 1);
                if (i == lastPos || Character.isDigit(previous)) {
                    inNumberSequence = true;
                    continue;
                }

                // Append word
                buf.append(pString.substring(lastPos, i).toLowerCase());
                if (previous != '-') {
                    buf.append('-');
                }
                buf.append(Character.toLowerCase(current));

                lastPos = i + 1;
            } else if (inNumberSequence) {
                // Sequence end
                inNumberSequence = false;

                buf.append(pString.substring(lastPos, i));
                if (current != '-') {
                    buf.append('-');
                }
                lastPos = i;
            } else if (inCharSequence) {
                // Sequence end
                inCharSequence = false;

                // NOTE: Special treatment! Last upper case, is first char in
                // next word, not last char in this word
                buf.append(pString.substring(lastPos, i - 1).toLowerCase());
                if (current != '-') {
                    buf.append('-');
                }
                lastPos = i - 1;
            }
        }

        if (buf != null) {
            // Append the rest
            buf.append(pString.substring(lastPos).toLowerCase());
            return buf.toString();
        } else {
            return Character.isUpperCase(pString.charAt(0)) ? pString.toLowerCase() : pString;
        }
    }

    /**
     * Gets the first substring between the given string boundaries.
     * <p/>
     *
     * @param pSource              The source string.
     * @param pBeginBoundaryString The string that marks the beginning.
     * @param pEndBoundaryString   The string that marks the end.
     * @param pOffset              The index to start searching in the source
     *                             string. If it is less than 0, the index will be set to 0.
     * @return the substring demarcated by the given string boundaries or null
     *         if not both string boundaries are found.
     */
    public static String substring(final String pSource, final String pBeginBoundaryString,
            final String pEndBoundaryString, final int pOffset) {
        // Check offset
        int offset = (pOffset < 0) ? 0 : pOffset;

        // Find the start index
        int startIndex = pSource.indexOf(pBeginBoundaryString, offset) + pBeginBoundaryString.length();

        if (startIndex < 0) {
            return null;
        }

        // Find the end index
        int endIndex = pSource.indexOf(pEndBoundaryString, startIndex);

        if (endIndex < 0) {
            return null;
        }
        return pSource.substring(startIndex, endIndex);
    }

    /**
     * Converts a string to lowercase.
     *
     * @param pString the string to convert
     * @return the string converted to lowercase, or null if the argument was
     *         null.
     */
    public static String toLowerCase(String pString) {
        if (pString != null) {
            return pString.toLowerCase();
        }
        return null;
    }
}

Related

  1. camelhumpToUnderline(String str)
  2. camelizeOneWord(String word, boolean firstLetterInLowerCase)
  3. camelPrefix(String str, int prefixSize)
  4. camelToComposite(String camel)
  5. camelToFixedString(String str, String fixed)
  6. camelToLowerSnake(String camel)
  7. camelToPeriod(String value)
  8. camelToSplitName(String camelName, String split)
  9. camelToSql(String name)