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

📄 complexformat.java

📁 Apache的common math数学软件包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     */    public static ComplexFormat getInstance() {        return getInstance(Locale.getDefault());    }        /**     * Returns the default complex format for the given locale.     * @param locale the specific locale used by the format.     * @return the complex format specific to the given locale.     */    public static ComplexFormat getInstance(Locale locale) {        NumberFormat f = getDefaultNumberFormat(locale);        return new ComplexFormat(f);    }        /**     * Access the realFormat.     * @return the realFormat.     */    public NumberFormat getRealFormat() {        return realFormat;    }    /**     * Parses a string to produce a {@link Complex} object.     *     * @param source the string to parse     * @return the parsed {@link Complex} object.     * @exception ParseException if the beginning of the specified string     *            cannot be parsed.     */    public Complex parse(String source) throws ParseException {        ParsePosition parsePosition = new ParsePosition(0);        Complex result = parse(source, parsePosition);        if (parsePosition.getIndex() == 0) {            throw new ParseException("Unparseable complex number: \"" + source +                "\"", parsePosition.getErrorIndex());        }        return result;    }        /**     * Parses a string to produce a {@link Complex} object.     *     * @param source the string to parse     * @param pos input/ouput parsing parameter.     * @return the parsed {@link Complex} object.     */    public Complex parse(String source, ParsePosition pos) {        int initialIndex = pos.getIndex();        // parse whitespace        parseAndIgnoreWhitespace(source, pos);        // parse real        Number re = parseNumber(source, getRealFormat(), pos);        if (re == null) {            // invalid real number            // set index back to initial, error index should already be set            // character examined.            pos.setIndex(initialIndex);            return null;        }        // parse sign        int startIndex = pos.getIndex();        char c = parseNextCharacter(source, pos);        int sign = 0;        switch (c) {        case 0 :            // no sign            // return real only complex number            return new Complex(re.doubleValue(), 0.0);        case '-' :            sign = -1;            break;        case '+' :            sign = 1;            break;        default :            // invalid sign            // set index back to initial, error index should be the last            // character examined.            pos.setIndex(initialIndex);            pos.setErrorIndex(startIndex);            return null;        }        // parse whitespace        parseAndIgnoreWhitespace(source, pos);        // parse imaginary        Number im = parseNumber(source, getRealFormat(), pos);        if (im == null) {            // invalid imaginary number            // set index back to initial, error index should already be set            // character examined.            pos.setIndex(initialIndex);            return null;        }        // parse imaginary character        int n = getImaginaryCharacter().length();        startIndex = pos.getIndex();        int endIndex = startIndex + n;        if (source.substring(startIndex, endIndex).compareTo(            getImaginaryCharacter()) != 0) {            // set index back to initial, error index should be the start index            // character examined.            pos.setIndex(initialIndex);            pos.setErrorIndex(startIndex);            return null;        }        pos.setIndex(endIndex);        return new Complex(re.doubleValue(), im.doubleValue() * sign);    }         /**     * Parses <code>source</code> until a non-whitespace character is found.     *     * @param source the string to parse     * @param pos input/ouput parsing parameter.  On output, <code>pos</code>     *        holds the index of the next non-whitespace character.     */    private void parseAndIgnoreWhitespace(String source, ParsePosition pos) {        parseNextCharacter(source, pos);        pos.setIndex(pos.getIndex() - 1);    }    /**     * Parses <code>source</code> until a non-whitespace character is found.     *     * @param source the string to parse     * @param pos input/ouput parsing parameter.     * @return the first non-whitespace character.     */    private char parseNextCharacter(String source, ParsePosition pos) {         int index = pos.getIndex();         int n = source.length();         char ret = 0;         if (index < n) {             char c;             do {                 c = source.charAt(index++);             } while (Character.isWhitespace(c) && index < n);             pos.setIndex(index);                      if (index < n) {                 ret = c;             }         }                  return ret;    }        /**     * Parses <code>source</code> for a special double values.  These values     * include Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY.     *     * @param source the string to parse     * @param value the special value to parse.     * @param pos input/ouput parsing parameter.     * @return the special number.     */    private Number parseNumber(String source, double value, ParsePosition pos) {        Number ret = null;                StringBuffer sb = new StringBuffer();        sb.append('(');        sb.append(value);        sb.append(')');                int n = sb.length();        int startIndex = pos.getIndex();        int endIndex = startIndex + n;        if (endIndex < source.length()) {            if (source.substring(startIndex, endIndex).compareTo(sb.toString()) == 0) {                ret = new Double(value);                pos.setIndex(endIndex);            }        }                return ret;    }        /**     * Parses <code>source</code> for a number.  This method can parse normal,     * numeric values as well as special values.  These special values include     * Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY.     *     * @param source the string to parse     * @param format the number format used to parse normal, numeric values.     * @param pos input/ouput parsing parameter.     * @return the parsed number.     */    private Number parseNumber(String source, NumberFormat format, ParsePosition pos) {        int startIndex = pos.getIndex();        Number number = format.parse(source, pos);        int endIndex = pos.getIndex();                // check for error parsing number        if (startIndex == endIndex) {            // try parsing special numbers            double[] special = {Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY};            for (int i = 0; i < special.length; ++i) {                number = parseNumber(source, special[i], pos);                if (number != null) {                    break;                }            }        }                return number;    }    /**     * Parses a string to produce a object.     *     * @param source the string to parse     * @param pos input/ouput parsing parameter.     * @return the parsed object.     * @see java.text.Format#parseObject(java.lang.String, java.text.ParsePosition)     */    public Object parseObject(String source, ParsePosition pos) {        return parse(source, pos);    }    /**     * Modify the imaginaryCharacter.     * @param imaginaryCharacter The new imaginaryCharacter value.     * @throws IllegalArgumentException if <code>imaginaryCharacter</code> is     *         <code>null</code> or an empty string.     */    public void setImaginaryCharacter(String imaginaryCharacter) {        if (imaginaryCharacter == null || imaginaryCharacter.length() == 0) {            throw new IllegalArgumentException(                "imaginaryCharacter must be a non-empty string.");        }        this.imaginaryCharacter = imaginaryCharacter;    }        /**     * Modify the imaginaryFormat.     * @param imaginaryFormat The new imaginaryFormat value.     * @throws IllegalArgumentException if <code>imaginaryFormat</code> is     *         <code>null</code>.     */    public void setImaginaryFormat(NumberFormat imaginaryFormat) {        if (imaginaryFormat == null) {            throw new IllegalArgumentException(                "imaginaryFormat can not be null.");        }        this.imaginaryFormat = imaginaryFormat;    }        /**     * Modify the realFormat.     * @param realFormat The new realFormat value.     * @throws IllegalArgumentException if <code>realFormat</code> is     *         <code>null</code>.     */    public void setRealFormat(NumberFormat realFormat) {        if (realFormat == null) {            throw new IllegalArgumentException(                "realFormat can not be null.");        }        this.realFormat = realFormat;    }}

⌨️ 快捷键说明

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