Java String Format format(String name, char separator)

Here you can find the source of format(String name, char separator)

Description

format

License

Open Source License

Declaration

protected static String format(String name, char separator) 

Method Source Code

//package com.java2s;
/**/*from  w w w . j a v  a2  s.  co m*/
 * Copyright (c) 2010 Kenn Hussey and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors: 
 *   Kenn Hussey - Initial API and implementation
 */

import java.util.ArrayList;

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

public class Main {
    protected static String format(String name, char separator) {
        StringBuffer result = new StringBuffer();

        for (Iterator<String> i = parseName(name, '_').iterator(); i.hasNext();) {
            String component = i.next();
            result.append(component);
            if (i.hasNext() && component.length() > 1) {
                result.append(separator);
            }
        }
        return result.toString();
    }

    protected static List<String> parseName(String sourceName, char sourceSeparator) {
        List<String> result = new ArrayList<String>();
        StringBuffer currentWord = new StringBuffer();

        int length = sourceName.length();
        boolean lastIsLower = false;

        for (int index = 0; index < length; index++) {
            char curChar = sourceName.charAt(index);
            if (Character.isUpperCase(curChar) || (!lastIsLower && Character.isDigit(curChar))
                    || curChar == sourceSeparator) {
                if (lastIsLower || curChar == sourceSeparator) {
                    result.add(currentWord.toString());
                    currentWord = new StringBuffer();
                }
                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 StringBuffer();
                        currentWord.append(lastChar);
                    }
                }
                lastIsLower = true;
            }
            if (curChar != sourceSeparator) {
                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, String prefix, boolean includePrefix, boolean includeLeadingSeparator)
  6. format(String s)
  7. format(String source, String placeholder, Object... objs)
  8. format(String string)