package cnp.ew.converter;
import java.awt.*;
import java.util.Date;

/**
 * Converts an arbitrary object to a string, using the toString() protocol
 * in Object. In addition, this class has the responsibility of maintaining a
 * mapping mechanism between objects and the converters they use to convert
 * themselves to strings, since there is no way to add a protocol to preexisting
 * classes.
 *
 * @version        $Version$
 * @author         $Author: Ken $
 */
public class CpDefaultToStringConverter implements CpToStringConverter
{

    /**
     * Answer an object that should be used to convert the given object o
     * to a string.  This is required, since we cannot add methods to
     * the Object class, and not all classes will implement the CpConvertibleToString
     * interface.  For those classes that do, we can simply ask them directly.
     *
     * @see cnp.ew.util.CpConvertibleToString#converterToString
     */
    public static CpToStringConverter converterFor(Object o)
    {
        if (o instanceof Date) {
            return new CpDateToStringConverter();
        }

        if (o instanceof Number) {
            return new CpNumberToStringConverter();
        }

        if (o instanceof Boolean) {
            return new CpBooleanToStringConverter();
        }

        if (o instanceof CpConvertibleToString) {
            return ((CpConvertibleToString)o).converterToString();
        }

        return new CpDefaultToStringConverter();
    }


    /**
     * Convert an object to a string.
     */
    public String convert(Object o)
    {
        return o.toString();
    }

    /**
     * Set the format string for this converter. The default converter
     * doesn't make use of it.
     */
    public void setFormatString(String ignore)
    {
    }
}
