Java String Format format(String name, char separator, String prefix, boolean includePrefix, boolean includeLeadingSeparator)

Here you can find the source of format(String name, char separator, String prefix, boolean includePrefix, boolean includeLeadingSeparator)

Description

Formats a name by parsing it into words separated by underscores and/or mixed-casing and then recombining them using the specified separator.

License

Open Source License

Declaration

public static String format(String name, char separator, String prefix, boolean includePrefix,
        boolean includeLeadingSeparator) 

Method Source Code

//package com.java2s;
/**/*  w w  w . j  a  v  a2 s  .  c om*/
 * Copyright (c) 2002-2009 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   IBM - Initial API and implementation
 */

import java.util.ArrayList;

import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * Formats a name by parsing it into words separated by underscores and/or mixed-casing and then recombining them using the specified
     * separator. A prefix can also be given to be recognized as a separate word or to be trimmed. Leading underscores can be ignored or can
     * cause a leading separator to be prepended.
     * 
     * @since 2.2
     */
    public static String format(String name, char separator, String prefix, boolean includePrefix,
            boolean includeLeadingSeparator) {
        String leadingSeparators = includeLeadingSeparator ? getLeadingSeparators(name, '_') : null;
        if (leadingSeparators != null) {
            name = name.substring(leadingSeparators.length());
        }

        List<String> parsedName = new ArrayList<String>();
        if (prefix != null && name.startsWith(prefix) && name.length() > prefix.length()
                && Character.isUpperCase(name.charAt(prefix.length()))) {
            name = name.substring(prefix.length());
            if (includePrefix) {
                parsedName = parseName(prefix, '_');
            }
        }

        if (name.length() != 0)
            parsedName.addAll(parseName(name, '_'));

        StringBuilder result = new StringBuilder();

        for (Iterator<String> nameIter = parsedName.iterator(); nameIter.hasNext();) {
            String nameComponent = nameIter.next();
            result.append(nameComponent);

            if (nameIter.hasNext() && nameComponent.length() > 1) {
                result.append(separator);
            }
        }

        if (result.length() == 0 && prefix != null) {
            result.append(prefix);
        }
        return leadingSeparators != null ? "_" + result.toString() : result.toString();
    }

    private static String getLeadingSeparators(String name, char separator) {
        int i = 0;
        for (int len = name.length(); i < len && name.charAt(i) == separator; i++) {
            // the for loop's condition finds the separator 
        }
        return i != 0 ? name.substring(0, i) : null;
    }

    /**
     * This method breaks sourceName into words delimited by separator and/or mixed-case naming.
     */
    public static List<String> parseName(String sourceName, char separator) {
        List<String> result = new ArrayList<String>();
        if (sourceName != null) {
            StringBuilder currentWord = new StringBuilder();
            boolean lastIsLower = false;
            for (int index = 0, length = sourceName.length(); index < length; ++index) {
                char curChar = sourceName.charAt(index);
                if (Character.isUpperCase(curChar) || (!lastIsLower && Character.isDigit(curChar))
                        || curChar == separator) {
                    if (lastIsLower && currentWord.length() > 1
                            || curChar == separator && currentWord.length() > 0) {
                        result.add(currentWord.toString());
                        currentWord = new StringBuilder();
                    }
                    lastIsLower = false;
                } else {
                    if (!lastIsLower) {
                        int currentWordLength = currentWord.length();
                        if (currentWordLength > 1) {
                            char lastChar = currentWord.charAt(--currentWordLength);
                            currentWord.setLength(currentWordLength);
                            result.add(currentWord.toString());
                            currentWord = new StringBuilder();
                            currentWord.append(lastChar);
                        }
                    }
                    lastIsLower = true;
                }

                if (curChar != separator) {
                    currentWord.append(curChar);
                }
            }

            result.add(currentWord.toString());
        }
        return result;
    }
}

Related

  1. createFormatter()
  2. format(final String message, final Object... args)
  3. format(final String s, final int width, final int intend)
  4. format(String longMessage, int charPerLine, int paddingLeft, boolean padFirstLine)
  5. format(String name, char separator)
  6. format(String s)
  7. format(String source, String placeholder, Object... objs)
  8. format(String string)
  9. formatCommand(String cmd)