⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 format.java

📁 数据仓库展示程序
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

        LiteralFormat(String s)
        {
            this(FORMAT_LITERAL, s);
        }

        LiteralFormat(int code, String s)
        {
            super(code);
            this.s = s;
        }

        // override Format
        void format(Object o, PrintWriter pw) {
            pw.print(s);
        }
        void format(double d, PrintWriter pw) {
            pw.print(s);
        }
        void format(long n, PrintWriter pw) {
            pw.print(s);
        }
        void format(String s, PrintWriter pw) {
            pw.print(s);
        }
        void format(Date date, PrintWriter pw) {
            pw.print(s);
        }
        void format(Calendar calendar, PrintWriter pw) {
            pw.print(s);
        }
    };

    /**
     * CompoundFormat is an implementation of {@link BasicFormat} where each
     * value is formatted by applying a sequence of format elements.  Each
     * format element is itself a format.
     *
     * @see AlternateFormat
     */
    static class CompoundFormat extends BasicFormat
    {
        BasicFormat[] formats;
        CompoundFormat(BasicFormat[] formats)
        {
            this.formats = formats;
        }

        public void format(Object v, PrintWriter pw)
        {
            for (int i = 0; i < formats.length; i++) {
                formats[i].format(v, pw);
            }
        }
        void format(double v, PrintWriter pw) {
            for (int i = 0; i < formats.length; i++) {
                formats[i].format(v, pw);
            }
        }
        void format(long v, PrintWriter pw) {
            for (int i = 0; i < formats.length; i++) {
                formats[i].format(v, pw);
            }
        }
        void format(String v, PrintWriter pw) {
            for (int i = 0; i < formats.length; i++) {
                formats[i].format(v, pw);
            }
        }
        void format(Date v, PrintWriter pw) {
            for (int i = 0; i < formats.length; i++) {
                formats[i].format(v, pw);
            }
        }
        void format(Calendar v, PrintWriter pw) {
            for (int i = 0; i < formats.length; i++) {
                formats[i].format(v, pw);
            }
        }
    };

    /**
     * JavaFormat is an implementation of {@link BasicFormat} which prints
     * values using Java's default formatting for their type.
     * <code>null</code> values appear as an empty string.
     */
    static class JavaFormat extends BasicFormat
    {
        JavaFormat()
        {
        }
        // No need to override format(Object,PrintWriter) or
        // format(Date,PrintWriter).
        void format(double d, PrintWriter pw) {
            pw.print(d);
        }
        void format(long n, PrintWriter pw) {
            pw.print(n);
        }
        void format(String s, PrintWriter pw) {
            pw.print(s);
        }
        void format(Calendar calendar, PrintWriter pw) {
            pw.print(calendar.getTime());
        }
    };

    /**
     * FallbackFormat catches un-handled datatypes and prints the original
     * format string.  Better than giving an error.  Abstract base class for
     * NumericFormat and DateFormat.
     */
    static abstract class FallbackFormat extends BasicFormat
    {
        String token;

        FallbackFormat(int code, String token)
        {
            super(code);
            this.token = token;
        }

        private void printToken(PrintWriter pw) {
            pw.print(token);
        }

        void format(double d, PrintWriter pw) {
            printToken(pw);
        }
        void format(long n, PrintWriter pw) {
            printToken(pw);
        }
        void format(String s, PrintWriter pw) {
            printToken(pw);
        }
        void format(Calendar calendar, PrintWriter pw) {
            printToken(pw);
        }
    };

    /**
     * NumericFormat is an implementation of {@link BasicFormat} which prints
     * numbers with a given number of decimal places, leading zeroes, in
     * exponential notation, etc.
     *
     * <p>It is implemented using {@link FloatingDecimal}, which is a
     * barely-modified version of <code>java.lang.FloatingDecimal</code>.
     */
    static class NumericFormat extends FallbackFormat
    {
        FormatLocale locale;
        int digitsLeftOfPoint;
        int zeroesLeftOfPoint;
        int digitsRightOfPoint;
        int zeroesRightOfPoint;
        int digitsRightOfExp;
        int zeroesRightOfExp;

        /** Number of decimal places to shift the number left before formatting
         * it.  2 means multiply by 100; -3 means divide by 1000. */
        int decimalShift;
        char expChar;
        boolean expSign;
        boolean useDecimal; // not used
        boolean useThouSep;

        NumericFormat(
            String token, FormatLocale locale,
            int expFormat,
            int digitsLeftOfPoint, int zeroesLeftOfPoint,
            int digitsRightOfPoint, int zeroesRightOfPoint,
            int digitsRightOfExp, int zeroesRightOfExp,
            boolean useDecimal, boolean useThouSep)
        {
            super(FORMAT_NULL, token);
            this.locale = locale;
            switch (expFormat) {
            case FORMAT_E_MINUS_UPPER:
                this.expChar = 'E';
                this.expSign = false;
                break;
            case FORMAT_E_PLUS_UPPER:
                this.expChar = 'E';
                this.expSign = true;
                break;
            case FORMAT_E_MINUS_LOWER:
                this.expChar = 'e';
                this.expSign = false;
                break;
            case FORMAT_E_PLUS_LOWER:
                this.expChar = 'e';
                this.expSign = true;
                break;
            default:
                this.expChar = 0;
                this.expSign = false;
            }
            this.digitsLeftOfPoint = digitsLeftOfPoint;
            this.zeroesLeftOfPoint = zeroesLeftOfPoint;
            this.digitsRightOfPoint = digitsRightOfPoint;
            this.zeroesRightOfPoint = zeroesRightOfPoint;
            this.digitsRightOfExp = digitsRightOfExp;
            this.zeroesRightOfExp = zeroesRightOfExp;
            this.useDecimal = useDecimal;
            this.useThouSep = useThouSep;
            this.decimalShift = 0; // set later
        }

        void format(Double n, PrintWriter pw)
        {
            format(n.doubleValue(), pw);
        }

        void format(double n, PrintWriter pw)
        {
            mondrian.util.Format.FloatingDecimal fd
                = new mondrian.util.Format.FloatingDecimal(n);
            fd.shift(decimalShift);
            String s = fd.toJavaFormatString(
                zeroesLeftOfPoint,
                locale.decimalPlaceholder,
                zeroesRightOfPoint,
                zeroesRightOfPoint + digitsRightOfPoint,
                expChar,
                expSign,
                zeroesRightOfExp,
                useThouSep ? locale.thousandSeparator : '\0');
            pw.print(s);
        }

        void format(Long n, PrintWriter pw)
        {
            format(n.longValue(), pw);
        }

        void format(long n, PrintWriter pw)
        {
            mondrian.util.Format.FloatingDecimal fd
                = new mondrian.util.Format.FloatingDecimal(n);
            fd.shift(decimalShift);
            String s = fd.toJavaFormatString(
                zeroesLeftOfPoint,
                locale.decimalPlaceholder,
                zeroesRightOfPoint,
                zeroesRightOfPoint + digitsRightOfPoint,
                expChar,
                expSign,
                zeroesRightOfExp,
                useThouSep ? locale.thousandSeparator : '\0');
            pw.print(s);
        }
    };

    /**
     * DateFormat is an element of a {@link CompoundFormat} which has a value
     * when applied to a {@link Calendar} object.  (Values of type {@link Date}
     * are automatically converted into {@link Calendar}s when you call {@link
     * BasicFormat#format(Date, PrintWriter)} calls to format other kinds of
     * values give a runtime error.)
     *
     * In a typical use of this class, a format string such as "m/d/yy" is
     * parsed into DateFormat objects for "m", "d", and "yy", and {@link
     * LiteralFormat} objects for "/".  A {@link CompoundFormat} object is
     * created to bind them together.
     */
    static class DateFormat extends FallbackFormat
    {
        FormatLocale locale;
        boolean twelveHourClock;

        DateFormat(int code, String s, FormatLocale locale, boolean twelveHourClock)
        {
            super(code, s);
            this.locale = locale;
            this.twelveHourClock = twelveHourClock;
        }
        void setTwelveHourClock(boolean twelveHourClock)
        {
            this.twelveHourClock = twelveHourClock;
        }
        void format(Calendar calendar, PrintWriter pw)
        {
            format(code, calendar, pw);
        }
        private void format(int code, Calendar calendar, PrintWriter pw)
        {
            switch (code) {
            case FORMAT_C:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -