/*
 * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.mandarax.util;


import java.util.Collection;
import java.util.Iterator;

/**
 * Contains useful string utilities.
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.1 <9 August 2004>
 * @since 1.1
 */
public final class StringUtils {

    // this constant defines the separator used for printing lists
    public static String SEPARATOR = ",";

    /**
     * Get a print string for an array based on the
     * print strings of the objects.
     * @return the produced string
     * @param objs the objects
     */
    public static String toString(Object[] objs) {
        if(objs == null) {
            return "?";
        }

        boolean      first = true;
        StringBuffer buf   = new StringBuffer ();

        for(int i = 0; i < objs.length; i++) {
            if(first) {
                first = false;
            } else {
                buf.append (SEPARATOR);
            }

            buf.append ((objs[i] == null)
                        ? "?"
                        : objs[i].toString ());
        }

        return new String (buf);
    }

    /**
     * Get a print string for a collection based on the
     * print strings of the objects it contains.
     * @return the produced string
     * @param coll a collection
     */
    public static String toString(Collection coll) {
        return toString (coll.iterator ());
    }

    /**
     * Get a print string for an iterator based on the
     * print strings of the objects.
     * @return the produced string
     * @param it an iterator (should be new initialized in order to make sure that we include all objects)
     */
    public static String toString(Iterator it) {
        if(it == null) {
            return "?";
        }

        boolean      first = true;
        StringBuffer buf   = new StringBuffer ();

        for(Iterator ii = it; ii.hasNext (); ) {
            if(first) {
                first = false;
            } else {
                buf.append (SEPARATOR);
            }

            buf.append (ii.next ().toString ());
        }

        return new String (buf);
    }
    /**
     * Split a string. E.g., calling this method with "23"+"54","+" yields {"23","54"}
     * @param txt a string
     * @param sep a separator
     * @return an array of two strings or null if the separator has not be found 
     */
    public static String[] split(String txt,String sep) {
    	int position = txt.indexOf(sep);
    	if (position==-1) return null;
    	String[] parts = new String[2];
    	parts[0] = txt.substring(0,position);
    	parts[1] = txt.substring(position+sep.length());
    	return parts;
    }
}
