1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | package Torello.CSS; import java.util.Vector; /** * Reprents a Number in CSS. This class is the parent-class of both {@link Percentage} and * {@ilnk Dimension} */ @Torello.JavaDoc.JDHeaderBackgroundImg(EmbedTagFileID="CSS_TOK") public class Percentage extends Num implements CharSequence, java.io.Serializable, Comparable<CharSequence> { /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUID> */ protected static final long serialVersionUID = 1; // ******************************************************************************************** // ******************************************************************************************** // Private Constructor, API "is" and "if" Methods // ******************************************************************************************** // ******************************************************************************************** Percentage( final int[] css, final int sPos, final int ePos, final Num n ) { super(css, sPos, ePos, n); } @Override public final boolean isPercentage() { return true; } @Override public final Percentage ifPercentage() { return this; } // ******************************************************************************************** // ******************************************************************************************** // User's Constructor: a static "build" method // ******************************************************************************************** // ******************************************************************************************** /** * <EMBED CLASS=defs DATA-TOK=Percentage DATA-P=percentageStr> * <EMBED CLASS='external-html' DATA-FILE-ID=BUILD_DESC> * @param percentageStr <EMBED CLASS='external-html' DATA-FILE-ID=BUILD_PARAM> * @return <EMBED CLASS='external-html' DATA-FILE-ID=BUILD_RET> * @throws TokenizeException <EMBED CLASS='external-html' DATA-FILE-ID=BUILD_TOK_EX> */ @SuppressWarnings("unchecked") public static Percentage build(final String percentageStr) { return (Percentage) CSSToken.build(percentageStr, INPUT_CHECKER, Num::consume); } private static final CSSToken.InputChecker INPUT_CHECKER = (int[] css) -> { if (css.length < 2) throw new TokenizeException(Percentage.class); // IMPORTANT: The above "build" method DOES NOT NEED TO CHECK the Return-Type of number // that Num::consume produced in order to ensure/guarantee that it is actually // an instance of Percentage. As long as this Error-Check ensures that the // String ends with a Percent-Symbol, then THERE IS NO WAY THAT THIS METHOD // WOULD FAIL with a CLASS-CAST-EXCPTION, because if Num::consume returned // something other that Class Percentage (specifically: Class Num or Class // Dimension), then that class CSSToken.str field WOULD NOT EQUAL the original // User-Input-String. // // If there is an error, we must throw "TokenizeException" (not ClassCastException), which // could happen beause of the "return (Precentage) ..." method above! if (css[css.length-1] != '%') throw new TokenizeException ("Input String does not end with a Percent-Symbol '%'"); if (! Num.is(css, 0)) throw new TokenizeException ("String-text beginning does not constitute a valid CSS Number-Token"); }; } |