package cnp.ew.converter;

import java.util.*;
/**
 * Subclasses of this class are used to convert particular aspects of
 * a date to a string (e.g. the hours, the month, the year), given a
 * prespecified format string.
 *
 * @version        $Version$
 * @author         $Author: Ken $
 */
abstract class CpDateUnitToString implements CpToStringConverter
{
    int formatStyle;

    /**
     * Creates the CpDateUnitToString object, with the given formatString.
     */
    public CpDateUnitToString(String formatString) {
        setFormatString(formatString);
    }

    /**
     * Sets the format string for the date unit.  By default, this merely sets
     * a style to the length of the format string.
     */
    public void setFormatString(String formatString) {
        formatStyle = formatString.length();
    }

    /**
     * Converts a date into a string representing this particular date unit.
     */
    public String convert(Object o)
    {
        Date d = (Date)o;
        return convertDate((Date)o);
    }

    /**
     * Answer a string representing a two digit number. If the number has
     * less than two digits, precede it with a zero.
     */
    String prefixWithZero(int number)
    {
        if (number < 10) {
            return "0" + number;
        } else {
            return "" + number;
        }
    }

    /**
     * The date unit should implement this method.
     */
    abstract String convertDate(Date d);
}
