📄 numberformat.java
字号:
* the stream was written by JDK 1.1, * set the <code>int</code> fields such as <code>maximumIntegerDigits</code> * to be equal to the <code>byte</code> fields such as <code>maxIntegerDigits</code>, * since the <code>int</code> fields were not present in JDK 1.1. * Finally, set serialVersionOnStream back to the maximum allowed value so that * default serialization will work properly if this object is streamed out again. * * <p>If <code>minimumIntegerDigits</code> is greater than * <code>maximumIntegerDigits</code> or <code>minimumFractionDigits</code> * is greater than <code>maximumFractionDigits</code>, then the stream data * is invalid and this method throws an <code>InvalidObjectException</code>. * In addition, if any of these values is negative, then this method throws * an <code>InvalidObjectException</code>. * * @since 1.2 */ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); if (serialVersionOnStream < 1) { // Didn't have additional int fields, reassign to use them. maximumIntegerDigits = maxIntegerDigits; minimumIntegerDigits = minIntegerDigits; maximumFractionDigits = maxFractionDigits; minimumFractionDigits = minFractionDigits; } if (minimumIntegerDigits > maximumIntegerDigits || minimumFractionDigits > maximumFractionDigits || minimumIntegerDigits < 0 || minimumFractionDigits < 0) { throw new InvalidObjectException("Digit count range invalid"); } serialVersionOnStream = currentSerialVersion; } /** * Write out the default serializable data, after first setting * the <code>byte</code> fields such as <code>maxIntegerDigits</code> to be * equal to the <code>int</code> fields such as <code>maximumIntegerDigits</code> * (or to <code>Byte.MAX_VALUE</code>, whichever is smaller), for compatibility * with the JDK 1.1 version of the stream format. * * @since 1.2 */ private void writeObject(ObjectOutputStream stream) throws IOException { maxIntegerDigits = (maximumIntegerDigits > Byte.MAX_VALUE) ? Byte.MAX_VALUE : (byte)maximumIntegerDigits; minIntegerDigits = (minimumIntegerDigits > Byte.MAX_VALUE) ? Byte.MAX_VALUE : (byte)minimumIntegerDigits; maxFractionDigits = (maximumFractionDigits > Byte.MAX_VALUE) ? Byte.MAX_VALUE : (byte)maximumFractionDigits; minFractionDigits = (minimumFractionDigits > Byte.MAX_VALUE) ? Byte.MAX_VALUE : (byte)minimumFractionDigits; stream.defaultWriteObject(); } /** * Cache to hold the NumberPatterns of a Locale. */ private static final Hashtable cachedLocaleData = new Hashtable(3); // Constants used by factory methods to specify a style of format. private static final int NUMBERSTYLE = 0; private static final int CURRENCYSTYLE = 1; private static final int PERCENTSTYLE = 2; private static final int SCIENTIFICSTYLE = 3; private static final int INTEGERSTYLE = 4; /** * True if the the grouping (i.e. thousands) separator is used when * formatting and parsing numbers. * * @serial * @see #isGroupingUsed */ private boolean groupingUsed = true; /** * The maximum number of digits allowed in the integer portion of a * number. <code>maxIntegerDigits</code> must be greater than or equal to * <code>minIntegerDigits</code>. * <p> * <strong>Note:</strong> This field exists only for serialization * compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new * <code>int</code> field <code>maximumIntegerDigits</code> is used instead. * When writing to a stream, <code>maxIntegerDigits</code> is set to * <code>maximumIntegerDigits</code> or <code>Byte.MAX_VALUE</code>, * whichever is smaller. When reading from a stream, this field is used * only if <code>serialVersionOnStream</code> is less than 1. * * @serial * @see #getMaximumIntegerDigits */ private byte maxIntegerDigits = 40; /** * The minimum number of digits allowed in the integer portion of a * number. <code>minimumIntegerDigits</code> must be less than or equal to * <code>maximumIntegerDigits</code>. * <p> * <strong>Note:</strong> This field exists only for serialization * compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new * <code>int</code> field <code>minimumIntegerDigits</code> is used instead. * When writing to a stream, <code>minIntegerDigits</code> is set to * <code>minimumIntegerDigits</code> or <code>Byte.MAX_VALUE</code>, * whichever is smaller. When reading from a stream, this field is used * only if <code>serialVersionOnStream</code> is less than 1. * * @serial * @see #getMinimumIntegerDigits */ private byte minIntegerDigits = 1; /** * The maximum number of digits allowed in the fractional portion of a * number. <code>maximumFractionDigits</code> must be greater than or equal to * <code>minimumFractionDigits</code>. * <p> * <strong>Note:</strong> This field exists only for serialization * compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new * <code>int</code> field <code>maximumFractionDigits</code> is used instead. * When writing to a stream, <code>maxFractionDigits</code> is set to * <code>maximumFractionDigits</code> or <code>Byte.MAX_VALUE</code>, * whichever is smaller. When reading from a stream, this field is used * only if <code>serialVersionOnStream</code> is less than 1. * * @serial * @see #getMaximumFractionDigits */ private byte maxFractionDigits = 3; // invariant, >= minFractionDigits /** * The minimum number of digits allowed in the fractional portion of a * number. <code>minimumFractionDigits</code> must be less than or equal to * <code>maximumFractionDigits</code>. * <p> * <strong>Note:</strong> This field exists only for serialization * compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new * <code>int</code> field <code>minimumFractionDigits</code> is used instead. * When writing to a stream, <code>minFractionDigits</code> is set to * <code>minimumFractionDigits</code> or <code>Byte.MAX_VALUE</code>, * whichever is smaller. When reading from a stream, this field is used * only if <code>serialVersionOnStream</code> is less than 1. * * @serial * @see #getMinimumFractionDigits */ private byte minFractionDigits = 0; /** * True if this format will parse numbers as integers only. * * @serial * @see #isParseIntegerOnly */ private boolean parseIntegerOnly = false; // new fields for 1.2. byte is too small for integer digits. /** * The maximum number of digits allowed in the integer portion of a * number. <code>maximumIntegerDigits</code> must be greater than or equal to * <code>minimumIntegerDigits</code>. * * @serial * @since 1.2 * @see #getMaximumIntegerDigits */ private int maximumIntegerDigits = 40; /** * The minimum number of digits allowed in the integer portion of a * number. <code>minimumIntegerDigits</code> must be less than or equal to * <code>maximumIntegerDigits</code>. * * @serial * @since 1.2 * @see #getMinimumIntegerDigits */ private int minimumIntegerDigits = 1; /** * The maximum number of digits allowed in the fractional portion of a * number. <code>maximumFractionDigits</code> must be greater than or equal to * <code>minimumFractionDigits</code>. * * @serial * @since 1.2 * @see #getMaximumFractionDigits */ private int maximumFractionDigits = 3; // invariant, >= minFractionDigits /** * The minimum number of digits allowed in the fractional portion of a * number. <code>minimumFractionDigits</code> must be less than or equal to * <code>maximumFractionDigits</code>. * * @serial * @since 1.2 * @see #getMinimumFractionDigits */ private int minimumFractionDigits = 0; static final int currentSerialVersion = 1; /** * Describes the version of <code>NumberFormat</code> present on the stream. * Possible values are: * <ul> * <li><b>0</b> (or uninitialized): the JDK 1.1 version of the stream format. * In this version, the <code>int</code> fields such as * <code>maximumIntegerDigits</code> were not present, and the <code>byte</code> * fields such as <code>maxIntegerDigits</code> are used instead. * * <li><b>1</b>: the 1.2 version of the stream format. The values of the * <code>byte</code> fields such as <code>maxIntegerDigits</code> are ignored, * and the <code>int</code> fields such as <code>maximumIntegerDigits</code> * are used instead. * </ul> * When streaming out a <code>NumberFormat</code>, the most recent format * (corresponding to the highest allowable <code>serialVersionOnStream</code>) * is always written. * * @serial * @since 1.2 */ private int serialVersionOnStream = currentSerialVersion; // Removed "implements Cloneable" clause. Needs to update serialization // ID for backward compatibility. static final long serialVersionUID = -2308460125733713944L; // // class for AttributedCharacterIterator attributes // /** * Defines constants that are used as attribute keys in the * <code>AttributedCharacterIterator</code> returned * from <code>NumberFormat.formatToCharacterIterator</code> and as * field identifiers in <code>FieldPosition</code>. * * @since 1.4 */ public static class Field extends Format.Field { // table of all instances in this class, used by readResolve private static final Map instanceMap = new HashMap(11); /** * Creates a Field instance with the specified * name. * * @param name Name of the attribute */ protected Field(String name) { super(name); if (this.getClass() == NumberFormat.Field.class) { instanceMap.put(name, this); } } /** * Resolves instances being deserialized to the predefined constants. * * @throws InvalidObjectException if the constant could not be * resolved. * @return resolved NumberFormat.Field constant */ protected Object readResolve() throws InvalidObjectException { if (this.getClass() != NumberFormat.Field.class) { throw new InvalidObjectException("subclass didn't correctly implement readResolve"); } Object instance = instanceMap.get(getName()); if (instance != null) { return instance; } else { throw new InvalidObjectException("unknown attribute name"); } } /** * Constant identifying the integer field. */ public static final Field INTEGER = new Field("integer"); /** * Constant identifying the fraction field. */ public static final Field FRACTION = new Field("fraction"); /** * Constant identifying the exponent field. */ public static final Field EXPONENT = new Field("exponent"); /** * Constant identifying the decimal separator field. */ public static final Field DECIMAL_SEPARATOR = new Field("decimal separator"); /** * Constant identifying the sign field. */ public static final Field SIGN = new Field("sign"); /** * Constant identifying the grouping separator field. */ public static final Field GROUPING_SEPARATOR = new Field("grouping separator"); /** * Constant identifying the exponent symbol field. */ public static final Field EXPONENT_SYMBOL = new Field("exponent symbol"); /** * Constant identifying the percent field. */ public static final Field PERCENT = new Field("percent"); /** * Constant identifying the permille field. */ public static final Field PERMILLE = new Field("per mille"); /** * Constant identifying the currency field. */ public static final Field CURRENCY = new Field("currency"); /** * Constant identifying the exponent sign field. */ public static final Field EXPONENT_SIGN = new Field("exponent sign"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -