aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/text/NumberFormat.java
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/java/text/NumberFormat.java')
-rw-r--r--libjava/java/text/NumberFormat.java208
1 files changed, 203 insertions, 5 deletions
diff --git a/libjava/java/text/NumberFormat.java b/libjava/java/text/NumberFormat.java
index 272cdbddac8..568018ffdf4 100644
--- a/libjava/java/text/NumberFormat.java
+++ b/libjava/java/text/NumberFormat.java
@@ -1,5 +1,5 @@
/* NumberFormat.java -- Formats and parses numbers
- Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+ Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -38,12 +38,14 @@ exception statement from your version. */
package java.text;
-import java.util.Locale;
-import java.util.ResourceBundle;
-import java.util.MissingResourceException;
+import java.io.InvalidObjectException;
+import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
-import java.io.IOException;
+import java.util.Currency;
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
/**
* This is the abstract superclass of all classes which format and
@@ -79,6 +81,132 @@ public abstract class NumberFormat extends Format implements Cloneable
*/
public static final int FRACTION_FIELD = 1;
+ public static class Field extends Format.Field
+ {
+ static final long serialVersionUID = 7494728892700160890L;
+
+ /**
+ * Attribute set to all characters containing digits of the integer
+ * part.
+ */
+ public static final NumberFormat.Field INTEGER
+ = new Field("integer");
+
+ /**
+ * Attribute set to all characters containing digits of the fractional
+ * part.
+ */
+ public static final NumberFormat.Field FRACTION
+ = new Field("fraction");
+
+ /**
+ * Attribute set to all characters containing digits of the exponential
+ * part.
+ */
+ public static final NumberFormat.Field EXPONENT
+ = new Field("exponent");
+
+ /**
+ * Attribute set to all characters containing a decimal separator.
+ */
+ public static final NumberFormat.Field DECIMAL_SEPARATOR
+ = new Field("decimal separator");
+
+ /**
+ * Attribute set to all characters containing a sign (plus or minus).
+ */
+ public static final NumberFormat.Field SIGN
+ = new Field("sign");
+
+ /**
+ * Attribute set to all characters containing a grouping separator (e.g.
+ * a comma, a white space,...).
+ */
+ public static final NumberFormat.Field GROUPING_SEPARATOR
+ = new Field("grouping separator");
+
+ /**
+ * Attribute set to all characters containing an exponential symbol (e.g.
+ * 'E')
+ */
+ public static final NumberFormat.Field EXPONENT_SYMBOL
+ = new Field("exponent symbol");
+
+ /**
+ * Attribute set to all characters containing a percent symbol (e.g. '%')
+ */
+ public static final NumberFormat.Field PERCENT
+ = new Field("percent");
+
+ /**
+ * Attribute set to all characters containing a permille symbol.
+ */
+ public static final NumberFormat.Field PERMILLE
+ = new Field("permille");
+
+ /**
+ * Attribute set to all characters containing the currency unit.
+ */
+ public static final NumberFormat.Field CURRENCY
+ = new Field("currency");
+
+ /**
+ * Attribute set to all characters containing the exponent sign.
+ */
+ public static final NumberFormat.Field EXPONENT_SIGN
+ = new Field("exponent sign");
+
+ /**
+ * Private fields to register all fields contained in this descriptor.
+ */
+ private static final NumberFormat.Field[] allFields =
+ {
+ INTEGER, FRACTION, EXPONENT, DECIMAL_SEPARATOR, SIGN,
+ GROUPING_SEPARATOR, EXPONENT_SYMBOL, PERCENT,
+ PERMILLE, CURRENCY, EXPONENT_SIGN
+ };
+
+ /**
+ * This constructor is only used by the deserializer. Without it,
+ * it would fail to construct a valid object.
+ */
+ private Field()
+ {
+ super("");
+ }
+
+ /**
+ * Create a Field instance with the specified field name.
+ *
+ * @param field_name Field name for the new Field instance.
+ */
+ protected Field(String field_name)
+ {
+ super (field_name);
+ }
+
+ /**
+ * This function is used by the deserializer to know which object
+ * to use when it encounters an encoded NumberFormat.Field in a
+ * serialization stream. If the stream is valid it should return
+ * one of the above field. In the other case we throw an exception.
+ *
+ * @return a valid official NumberFormat.Field instance.
+ *
+ * @throws InvalidObjectException if the field name is invalid.
+ */
+ protected Object readResolve() throws InvalidObjectException
+ {
+ String s = getName();
+ for (int i = 0; i < allFields.length; i++)
+ if (s.equals(allFields[i].getName()))
+ return allFields[i];
+
+ throw new InvalidObjectException("no such NumberFormat field called "
+ + s);
+ }
+ }
+
/**
* This method is a specialization of the format method that performs
* a simple formatting of the specified <code>long</code> number.
@@ -326,6 +454,36 @@ public abstract class NumberFormat extends Format implements Cloneable
}
/**
+ * This method returns an integer formatting and parsing class for the
+ * default locale. This will be a concrete subclass of <code>NumberFormat</code>,
+ * but the actual class returned is dependent on the locale.
+ *
+ * @return An instance of an integer number formatter for the default locale.
+ * @since 1.4
+ */
+ public static final NumberFormat getIntegerInstance()
+ {
+ return getIntegerInstance (Locale.getDefault());
+ }
+
+ /**
+ * This method returns an integer formatting and parsing class for the
+ * default locale. This will be a concrete subclass of <code>NumberFormat</code>,
+ * but the actual class returned is dependent on the locale.
+ *
+ * @param locale the desired locale.
+ *
+ * @return An instance of an integer number formatter for the desired locale.
+ * @since 1.4
+ */
+ public static NumberFormat getIntegerInstance(Locale locale)
+ {
+ NumberFormat format = computeInstance (locale, "numberFormat", "#,##0");
+ format.setParseIntegerOnly (true);
+ return format;
+ }
+
+ /**
* This method returns an instance of <code>NumberFormat</code> suitable
* for formatting and parsing percentage values in the default locale.
*
@@ -603,4 +761,44 @@ public abstract class NumberFormat extends Format implements Cloneable
serialVersionOnStream = 1;
stream.defaultWriteObject();
}
+
+ /**
+ * Returns the currency used by this number format when formatting currency
+ * values.
+ *
+ * The default implementation throws UnsupportedOperationException.
+ *
+ * @return The used currency object, or null.
+ *
+ * @throws UnsupportedOperationException If the number format class doesn't
+ * implement currency formatting.
+ *
+ * @since 1.4
+ */
+ public Currency getCurrency()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * Sets the currency used by this number format when formatting currency
+ * values.
+ *
+ * The default implementation throws UnsupportedOperationException.
+ *
+ * @param currency The new currency to be used by this number format.
+ *
+ * @throws NullPointerException If currenc is null.
+ * @throws UnsupportedOperationException If the number format class doesn't
+ * implement currency formatting.
+ *
+ * @since 1.4
+ */
+ public void setCurreny(Currency currency)
+ {
+ if (currency == null)
+ throw new NullPointerException("currency may not be null");
+
+ throw new UnsupportedOperationException();
+ }
}