< prev index next >

src/java.base/share/classes/java/text/NumberFormat.java

Print this page




 996     /**
 997      * First, read in the default serializable data.
 998      *
 999      * Then, if <code>serialVersionOnStream</code> is less than 1, indicating that
1000      * the stream was written by JDK 1.1,
1001      * set the <code>int</code> fields such as <code>maximumIntegerDigits</code>
1002      * to be equal to the <code>byte</code> fields such as <code>maxIntegerDigits</code>,
1003      * since the <code>int</code> fields were not present in JDK 1.1.
1004      * Finally, set serialVersionOnStream back to the maximum allowed value so that
1005      * default serialization will work properly if this object is streamed out again.
1006      *
1007      * <p>If <code>minimumIntegerDigits</code> is greater than
1008      * <code>maximumIntegerDigits</code> or <code>minimumFractionDigits</code>
1009      * is greater than <code>maximumFractionDigits</code>, then the stream data
1010      * is invalid and this method throws an <code>InvalidObjectException</code>.
1011      * In addition, if any of these values is negative, then this method throws
1012      * an <code>InvalidObjectException</code>.
1013      *
1014      * @since 1.2
1015      */

1016     private void readObject(ObjectInputStream stream)
1017          throws IOException, ClassNotFoundException
1018     {
1019         stream.defaultReadObject();
1020         if (serialVersionOnStream < 1) {
1021             // Didn't have additional int fields, reassign to use them.
1022             maximumIntegerDigits = maxIntegerDigits;
1023             minimumIntegerDigits = minIntegerDigits;
1024             maximumFractionDigits = maxFractionDigits;
1025             minimumFractionDigits = minFractionDigits;
1026         }
1027         if (minimumIntegerDigits > maximumIntegerDigits ||
1028             minimumFractionDigits > maximumFractionDigits ||
1029             minimumIntegerDigits < 0 || minimumFractionDigits < 0) {
1030             throw new InvalidObjectException("Digit count range invalid");
1031         }
1032         serialVersionOnStream = currentSerialVersion;
1033     }
1034 
1035     /**
1036      * Write out the default serializable data, after first setting
1037      * the <code>byte</code> fields such as <code>maxIntegerDigits</code> to be
1038      * equal to the <code>int</code> fields such as <code>maximumIntegerDigits</code>
1039      * (or to <code>Byte.MAX_VALUE</code>, whichever is smaller), for compatibility
1040      * with the JDK 1.1 version of the stream format.
1041      *
1042      * @since 1.2
1043      */

1044     private void writeObject(ObjectOutputStream stream)
1045          throws IOException
1046     {
1047         maxIntegerDigits = (maximumIntegerDigits > Byte.MAX_VALUE) ?
1048                            Byte.MAX_VALUE : (byte)maximumIntegerDigits;
1049         minIntegerDigits = (minimumIntegerDigits > Byte.MAX_VALUE) ?
1050                            Byte.MAX_VALUE : (byte)minimumIntegerDigits;
1051         maxFractionDigits = (maximumFractionDigits > Byte.MAX_VALUE) ?
1052                             Byte.MAX_VALUE : (byte)maximumFractionDigits;
1053         minFractionDigits = (minimumFractionDigits > Byte.MAX_VALUE) ?
1054                             Byte.MAX_VALUE : (byte)minimumFractionDigits;
1055         stream.defaultWriteObject();
1056     }
1057 
1058     // Constants used by factory methods to specify a style of format.
1059     private static final int NUMBERSTYLE = 0;
1060     private static final int CURRENCYSTYLE = 1;
1061     private static final int PERCENTSTYLE = 2;
1062     private static final int SCIENTIFICSTYLE = 3;
1063     private static final int INTEGERSTYLE = 4;


1208      *     In this version, the <code>int</code> fields such as
1209      *     <code>maximumIntegerDigits</code> were not present, and the <code>byte</code>
1210      *     fields such as <code>maxIntegerDigits</code> are used instead.
1211      *
1212      * <li><b>1</b>: the 1.2 version of the stream format.  The values of the
1213      *     <code>byte</code> fields such as <code>maxIntegerDigits</code> are ignored,
1214      *     and the <code>int</code> fields such as <code>maximumIntegerDigits</code>
1215      *     are used instead.
1216      * </ul>
1217      * When streaming out a <code>NumberFormat</code>, the most recent format
1218      * (corresponding to the highest allowable <code>serialVersionOnStream</code>)
1219      * is always written.
1220      *
1221      * @serial
1222      * @since 1.2
1223      */
1224     private int serialVersionOnStream = currentSerialVersion;
1225 
1226     // Removed "implements Cloneable" clause.  Needs to update serialization
1227     // ID for backward compatibility.

1228     static final long serialVersionUID = -2308460125733713944L;
1229 
1230 
1231     //
1232     // class for AttributedCharacterIterator attributes
1233     //
1234     /**
1235      * Defines constants that are used as attribute keys in the
1236      * <code>AttributedCharacterIterator</code> returned
1237      * from <code>NumberFormat.formatToCharacterIterator</code> and as
1238      * field identifiers in <code>FieldPosition</code>.
1239      *
1240      * @since 1.4
1241      */
1242     public static class Field extends Format.Field {
1243 
1244         // Proclaim serial compatibility with 1.4 FCS

1245         private static final long serialVersionUID = 7494728892700160890L;
1246 
1247         // table of all instances in this class, used by readResolve
1248         private static final Map<String, Field> instanceMap = new HashMap<>(11);
1249 
1250         /**
1251          * Creates a Field instance with the specified
1252          * name.
1253          *
1254          * @param name Name of the attribute
1255          */
1256         protected Field(String name) {
1257             super(name);
1258             if (this.getClass() == NumberFormat.Field.class) {
1259                 instanceMap.put(name, this);
1260             }
1261         }
1262 
1263         /**
1264          * Resolves instances being deserialized to the predefined constants.
1265          *
1266          * @throws InvalidObjectException if the constant could not be resolved.
1267          * @return resolved NumberFormat.Field constant
1268          */
1269         @Override

1270         protected Object readResolve() throws InvalidObjectException {
1271             if (this.getClass() != NumberFormat.Field.class) {
1272                 throw new InvalidObjectException("subclass didn't correctly implement readResolve");
1273             }
1274 
1275             Object instance = instanceMap.get(getName());
1276             if (instance != null) {
1277                 return instance;
1278             } else {
1279                 throw new InvalidObjectException("unknown attribute name");
1280             }
1281         }
1282 
1283         /**
1284          * Constant identifying the integer field.
1285          */
1286         public static final Field INTEGER = new Field("integer");
1287 
1288         /**
1289          * Constant identifying the fraction field.




 996     /**
 997      * First, read in the default serializable data.
 998      *
 999      * Then, if <code>serialVersionOnStream</code> is less than 1, indicating that
1000      * the stream was written by JDK 1.1,
1001      * set the <code>int</code> fields such as <code>maximumIntegerDigits</code>
1002      * to be equal to the <code>byte</code> fields such as <code>maxIntegerDigits</code>,
1003      * since the <code>int</code> fields were not present in JDK 1.1.
1004      * Finally, set serialVersionOnStream back to the maximum allowed value so that
1005      * default serialization will work properly if this object is streamed out again.
1006      *
1007      * <p>If <code>minimumIntegerDigits</code> is greater than
1008      * <code>maximumIntegerDigits</code> or <code>minimumFractionDigits</code>
1009      * is greater than <code>maximumFractionDigits</code>, then the stream data
1010      * is invalid and this method throws an <code>InvalidObjectException</code>.
1011      * In addition, if any of these values is negative, then this method throws
1012      * an <code>InvalidObjectException</code>.
1013      *
1014      * @since 1.2
1015      */
1016     @java.io.Serial
1017     private void readObject(ObjectInputStream stream)
1018          throws IOException, ClassNotFoundException
1019     {
1020         stream.defaultReadObject();
1021         if (serialVersionOnStream < 1) {
1022             // Didn't have additional int fields, reassign to use them.
1023             maximumIntegerDigits = maxIntegerDigits;
1024             minimumIntegerDigits = minIntegerDigits;
1025             maximumFractionDigits = maxFractionDigits;
1026             minimumFractionDigits = minFractionDigits;
1027         }
1028         if (minimumIntegerDigits > maximumIntegerDigits ||
1029             minimumFractionDigits > maximumFractionDigits ||
1030             minimumIntegerDigits < 0 || minimumFractionDigits < 0) {
1031             throw new InvalidObjectException("Digit count range invalid");
1032         }
1033         serialVersionOnStream = currentSerialVersion;
1034     }
1035 
1036     /**
1037      * Write out the default serializable data, after first setting
1038      * the <code>byte</code> fields such as <code>maxIntegerDigits</code> to be
1039      * equal to the <code>int</code> fields such as <code>maximumIntegerDigits</code>
1040      * (or to <code>Byte.MAX_VALUE</code>, whichever is smaller), for compatibility
1041      * with the JDK 1.1 version of the stream format.
1042      *
1043      * @since 1.2
1044      */
1045     @java.io.Serial
1046     private void writeObject(ObjectOutputStream stream)
1047          throws IOException
1048     {
1049         maxIntegerDigits = (maximumIntegerDigits > Byte.MAX_VALUE) ?
1050                            Byte.MAX_VALUE : (byte)maximumIntegerDigits;
1051         minIntegerDigits = (minimumIntegerDigits > Byte.MAX_VALUE) ?
1052                            Byte.MAX_VALUE : (byte)minimumIntegerDigits;
1053         maxFractionDigits = (maximumFractionDigits > Byte.MAX_VALUE) ?
1054                             Byte.MAX_VALUE : (byte)maximumFractionDigits;
1055         minFractionDigits = (minimumFractionDigits > Byte.MAX_VALUE) ?
1056                             Byte.MAX_VALUE : (byte)minimumFractionDigits;
1057         stream.defaultWriteObject();
1058     }
1059 
1060     // Constants used by factory methods to specify a style of format.
1061     private static final int NUMBERSTYLE = 0;
1062     private static final int CURRENCYSTYLE = 1;
1063     private static final int PERCENTSTYLE = 2;
1064     private static final int SCIENTIFICSTYLE = 3;
1065     private static final int INTEGERSTYLE = 4;


1210      *     In this version, the <code>int</code> fields such as
1211      *     <code>maximumIntegerDigits</code> were not present, and the <code>byte</code>
1212      *     fields such as <code>maxIntegerDigits</code> are used instead.
1213      *
1214      * <li><b>1</b>: the 1.2 version of the stream format.  The values of the
1215      *     <code>byte</code> fields such as <code>maxIntegerDigits</code> are ignored,
1216      *     and the <code>int</code> fields such as <code>maximumIntegerDigits</code>
1217      *     are used instead.
1218      * </ul>
1219      * When streaming out a <code>NumberFormat</code>, the most recent format
1220      * (corresponding to the highest allowable <code>serialVersionOnStream</code>)
1221      * is always written.
1222      *
1223      * @serial
1224      * @since 1.2
1225      */
1226     private int serialVersionOnStream = currentSerialVersion;
1227 
1228     // Removed "implements Cloneable" clause.  Needs to update serialization
1229     // ID for backward compatibility.
1230     @java.io.Serial
1231     static final long serialVersionUID = -2308460125733713944L;
1232 
1233 
1234     //
1235     // class for AttributedCharacterIterator attributes
1236     //
1237     /**
1238      * Defines constants that are used as attribute keys in the
1239      * <code>AttributedCharacterIterator</code> returned
1240      * from <code>NumberFormat.formatToCharacterIterator</code> and as
1241      * field identifiers in <code>FieldPosition</code>.
1242      *
1243      * @since 1.4
1244      */
1245     public static class Field extends Format.Field {
1246 
1247         // Proclaim serial compatibility with 1.4 FCS
1248         @java.io.Serial
1249         private static final long serialVersionUID = 7494728892700160890L;
1250 
1251         // table of all instances in this class, used by readResolve
1252         private static final Map<String, Field> instanceMap = new HashMap<>(11);
1253 
1254         /**
1255          * Creates a Field instance with the specified
1256          * name.
1257          *
1258          * @param name Name of the attribute
1259          */
1260         protected Field(String name) {
1261             super(name);
1262             if (this.getClass() == NumberFormat.Field.class) {
1263                 instanceMap.put(name, this);
1264             }
1265         }
1266 
1267         /**
1268          * Resolves instances being deserialized to the predefined constants.
1269          *
1270          * @throws InvalidObjectException if the constant could not be resolved.
1271          * @return resolved NumberFormat.Field constant
1272          */
1273         @Override
1274         @java.io.Serial
1275         protected Object readResolve() throws InvalidObjectException {
1276             if (this.getClass() != NumberFormat.Field.class) {
1277                 throw new InvalidObjectException("subclass didn't correctly implement readResolve");
1278             }
1279 
1280             Object instance = instanceMap.get(getName());
1281             if (instance != null) {
1282                 return instance;
1283             } else {
1284                 throw new InvalidObjectException("unknown attribute name");
1285             }
1286         }
1287 
1288         /**
1289          * Constant identifying the integer field.
1290          */
1291         public static final Field INTEGER = new Field("integer");
1292 
1293         /**
1294          * Constant identifying the fraction field.


< prev index next >