package cnp.ew.converter;
import java.util.*;
import cnp.ew.util.*;

/**
 * Used to display the quarter portion of a date, using a prespecified
 * format. It is typically used as a component in converting a quarter
 * to a full string with a prespecified format, where the quarter output
 * is just one portion of the string.  Example usage:
 * <pre>
 *    CpQuarterToString converter = new CpQuarterToString("q");
 *    convertedString = converter.convertDate(new Date("3/1/95 11:30"));
 * </pre>
 *
 * @see            cnp.ew.util.CpDateToStringConverter
 * @version        $Version$
 * @author         $Author: Ken $
 */
class CpQuarterToString extends CpDateUnitToString
{

    static final String[] shortQuarterNames = { "1st", "2nd", "3rd", "4th" };
    static final String[] longQuarterNames = { "first", "second", "third", "fourth" };
    static final String[] longCapQuarterNames = { "First", "Second", "Third", "Fourth" };

    /**
     * Creates a new converter, with the given format for conversion.
     * @param formatString  Possible formats are:
     *      "q"    numeric quarter of the year (1 to 4)
     *      "qq"   shortened quarter names (1st, 2nd, 3rd, 4th)
     *      "qqq"  long quarter names, lower case (first, second, third, fourth)
     *      "qqqq" long quarter names, upper case (First, Second, Third, Fourth)
     */
    public CpQuarterToString(String formatString)
    {
        super(formatString);
    }

    /**
     * Converts a given date to the appropriate string for the
     * predefined format.
     */
    public String convertDate(Date d)
    {
        int quarter = (new CpDate(d)).getQuarter();
        switch (formatStyle) {
        case 1:
            // 'q' - single digit quarter
            return "" + quarter;
        case 2:
            return shortQuarterNames[quarter - 1];
        case 3:
            return longQuarterNames[quarter - 1];
        default:
            return longCapQuarterNames[quarter - 1];
        }

    }
}
