package baseCode.util;

import com.Ostermiller.util.CSVParser;

/**
 * <hr>
 * <p>
 * Copyright (c) 2004-2005 Columbia University
 * 
 * @author pavlidis
 * @version $Id$
 */
public class StringUtil {

    /**
     * @param numFields
     * @param line
     * @return
     */
    public static String[] csvSplit( int numFields, String line ) {
        String[][] parsedFields = CSVParser.parse( line );
        return parsedFields[0];
    }

    /**
     * Method to wrap lines. It attempts to wrap on whitespace - it will not break within a word.
     * 
     * @param string
     * @param charPerLine
     * @param delim The string to put at the end of each new line. For example, "\n" or "&lt;br /&gt;";
     * @return
     */
    public static String wrap( String string, int charPerLine, String delim ) {
        if ( string == null || string.length() <= charPerLine ) return string;
        StringBuffer buf = new StringBuffer();
        char[] chars = string.toCharArray();
        int lastLineCount = 0;
        boolean needDelim = false;
        for ( int i = 0; i < chars.length; i++ ) {
            char c = chars[i];
            buf.append( c );
            lastLineCount++;

            if ( needDelim ) {
                if ( ( c == ' ' || c == '\t' || c == '\n' ) ) {
                    buf.append( delim );
                    lastLineCount = 0;
                    needDelim = false;
                    continue;
                }
            }

            if ( i > 0 && lastLineCount >= charPerLine ) {
                if ( ( c == ' ' || c == '\t' || c == '\n' ) ) {
                    buf.append( delim );
                    lastLineCount = 0;
                    needDelim = false;
                } else {
                    needDelim = true;
                }
            }
        }
        return buf.toString();
    }

    /**
     * Convert "Property" to "property"
     * 
     * @param input
     * @return the input with the first character in lowercase.
     */
    public static String lowerCaseFirstLetter( String input ) {
        return ( new String( new char[] { input.charAt( 0 ) } ) ).toLowerCase() + input.substring( 1 );
    }

    /**
     * Convert "property" to "Property"
     * 
     * @param input
     * @return the input with the first character in upper case..
     */
    public static String upperCaseFirstLetter( String input ) {
        return ( new String( new char[] { input.charAt( 0 ) } ) ).toUpperCase() + input.substring( 1 );
    }

    /**
     * @param stringi
     * @param stringj
     * @return
     */
    public static Object twoStringHashKey( String stringi, String stringj ) {
        // use arbitrary but consistent method for ordering.
        if ( stringi.hashCode() < stringj.hashCode() ) {
            return new Long( stringi.hashCode() | ( long ) stringj.hashCode() << 32 );
        }
        return new Long( stringj.hashCode() | ( long ) stringi.hashCode() << 32 );

    }

}

@@ -17,16 +17,112 @@
 .util;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport org.apache.commons.lang.ArrayUtils;%0A%0A
 import c
@@ -3046,10 +3046,1687 @@
 %0A    %7D%0A%0A
+    /**%0A     * Borrowed from apache commons-lang-2.1, which clashes with something in our build environment (this is%0A     * %22splitworker%22)%0A     * %0A     * @param str the String to parse, may be %3Ccode%3Enull%3C/code%3E%0A     * @param separatorChar the separate character%0A     * @param preserveAllTokens if %3Ccode%3Etrue%3C/code%3E, adjacent separators are treated as empty token separators; if%0A     *        %3Ccode%3Efalse%3C/code%3E, adjacent separators are treated as one separator.%0A     * @return an array of parsed Strings, %3Ccode%3Enull%3C/code%3E if null String input%0A     */%0A    public static String%5B%5D splitPreserveAllTokens( String str, char separatorChar ) %7B%0A        boolean preserveAllTokens = true;%0A        // Performance tuned for 2.0 (JDK1.4)%0A%0A        if ( str == null ) %7B%0A            return null;%0A        %7D%0A        int len = str.length();%0A        if ( len == 0 ) %7B%0A            return ArrayUtils.EMPTY_STRING_ARRAY;%0A        %7D%0A        List list = new ArrayList();%0A        int i = 0, start = 0;%0A        boolean match = false;%0A        boolean lastMatch = false;%0A        while ( i %3C len ) %7B%0A            if ( str.charAt( i ) == separatorChar ) %7B%0A                if ( match %7C%7C preserveAllTokens ) %7B%0A                    list.add( str.substring( start, i ) );%0A                    match = false;%0A                    lastMatch = true;%0A                %7D%0A                start = ++i;%0A                continue;%0A            %7D%0A            lastMatch = false;%0A%0A            match = true;%0A            i++;%0A        %7D%0A        if ( match %7C%7C ( preserveAllTokens && lastMatch ) ) %7B%0A            list.add( str.substring( start, i ) );%0A        %7D%0A        return ( String%5B%5D ) list.toArray( new String%5Blist.size()%5D );%0A    %7D%0A
 %7D%0A

add splitPreserveAllTokens due to problem with commons lang.