001package Torello.CSS;
002
003import java.util.Vector;
004
005/**
006 * Reprents a Number in CSS.  This class is the parent-class of both {@link Percentage} and
007 * {@ilnk Dimension}
008 */
009@Torello.JavaDoc.JDHeaderBackgroundImg(EmbedTagFileID="CSS_TOK")
010public class Percentage extends Num
011    implements CharSequence, java.io.Serializable, Comparable<CharSequence>
012{
013    /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUID> */
014    protected static final long serialVersionUID = 1;
015
016
017    // ********************************************************************************************
018    // ********************************************************************************************
019    // Private Constructor, API "is" and "if" Methods
020    // ********************************************************************************************
021    // ********************************************************************************************
022
023
024    Percentage(
025            final int[] css,
026            final int   sPos,
027            final int   ePos,
028            final Num   n
029        )
030    { super(css, sPos, ePos, n); }
031
032    @Override 
033    public final boolean isPercentage() { return true; }
034
035    @Override
036    public final Percentage ifPercentage() { return this; }
037
038
039    // ********************************************************************************************
040    // ********************************************************************************************
041    // User's Constructor: a static "build" method
042    // ********************************************************************************************
043    // ********************************************************************************************
044
045
046    /**
047     * <EMBED CLASS=defs DATA-TOK=Percentage DATA-P=percentageStr>
048     * <EMBED CLASS='external-html' DATA-FILE-ID=BUILD_DESC>
049     * @param percentageStr <EMBED CLASS='external-html' DATA-FILE-ID=BUILD_PARAM>
050     * @return <EMBED CLASS='external-html' DATA-FILE-ID=BUILD_RET>
051     * @throws TokenizeException <EMBED CLASS='external-html' DATA-FILE-ID=BUILD_TOK_EX>
052     */
053    @SuppressWarnings("unchecked")
054    public static Percentage build(final String percentageStr)
055    { return (Percentage) CSSToken.build(percentageStr, INPUT_CHECKER, Num::consume); }
056
057    private static final CSSToken.InputChecker INPUT_CHECKER = (int[] css) ->
058    {
059        if (css.length < 2) throw new TokenizeException(Percentage.class);
060
061        // IMPORTANT: The above "build" method DOES NOT NEED TO CHECK the Return-Type of number
062        //            that Num::consume produced in order to ensure/guarantee that it is actually
063        //            an instance of Percentage.  As long as this Error-Check ensures that the
064        //            String ends with a Percent-Symbol, then THERE IS NO WAY THAT THIS METHOD
065        //            WOULD FAIL with a CLASS-CAST-EXCPTION, because if Num::consume returned
066        //            something other that Class Percentage (specifically: Class Num or Class 
067        //            Dimension), then that class CSSToken.str field WOULD NOT EQUAL the original
068        //            User-Input-String.
069        // 
070        // If there is an error, we must throw "TokenizeException" (not ClassCastException), which
071        // could happen beause of the "return (Precentage) ..." method above!
072
073        if (css[css.length-1] != '%') throw new TokenizeException
074            ("Input String does not end with a Percent-Symbol '%'");
075
076        if (! Num.is(css, 0)) throw new TokenizeException
077            ("String-text beginning does not constitute a valid CSS Number-Token");
078    };
079}