1. /*
  2. * @(#)Format.java 1.28 00/01/19
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. /*
  11. * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  12. * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
  13. *
  14. * The original version of this source code and documentation is copyrighted
  15. * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  16. * materials are provided under terms of a License Agreement between Taligent
  17. * and Sun. This technology is protected by multiple US and International
  18. * patents. This notice and attribution to Taligent may not be removed.
  19. * Taligent is a registered trademark of Taligent, Inc.
  20. *
  21. */
  22. package java.text;
  23. import java.io.Serializable;
  24. /**
  25. * <code>Format</code> is an abstract base class for formatting locale-sensitive
  26. * information such as dates, messages, and numbers.
  27. *
  28. * <p>
  29. * <code>Format</code> defines the programming interface for formatting
  30. * locale-sensitive objects into <code>String</code>s (the
  31. * <code>format</code> method) and for parsing <code>String</code>s back
  32. * into objects (the <code>parseObject</code> method). Any <code>String</code>
  33. * formatted by <code>format</code> is guaranteed to be parseable by
  34. * <code>parseObject</code>.
  35. *
  36. * <p>
  37. * If formatting is unsuccessful because the <code>Format</code> object
  38. * cannot format the type of object specified, <code>format</code> throws an
  39. * <code>IllegalArgumentException</code>. Otherwise, if there is something
  40. * illformed about the object, <code>format</code> returns the Unicode
  41. * replacement character <code>\\uFFFD</code>.
  42. *
  43. * <p>
  44. * If there is no match when parsing,
  45. * <code>parseObject(String)</code> throws a <code>ParseException</code>,
  46. * and <code>parseObject(String, ParsePosition)</code> leaves the
  47. * <code>ParsePosition</code> <code>index</code> member unchanged and
  48. * returns <code>null</code>.
  49. *
  50. * <p>
  51. * <STRONG>Subclassing:</STRONG>
  52. * The Java 2 platform provides three concrete subclasses of <code>Format</code>--
  53. * <code>DateFormat</code>, <code>MessageFormat</code>, and
  54. * <code>NumberFormat</code>--for formatting dates, messages, and numbers,
  55. * respectively.
  56. * <p>
  57. * Concrete subclasses <em>must</em> implement these two methods:
  58. * <ol>
  59. * <li> <code>format(Object obj, StringBuffer toAppendTo, FieldPosition pos)</code>
  60. * <li> <code>parseObject (String source, ParsePosition pos)</code>
  61. * </ol>
  62. *
  63. * <p>
  64. * Most subclasses will also implement the following two methods:
  65. * <ol>
  66. * <li>
  67. * <code>getInstance</code> for getting a useful format object appropriate
  68. * for the current locale
  69. * <li>
  70. * <code>getInstance(Locale)</code> for getting a useful format
  71. * object appropriate for the specified locale
  72. * </ol>
  73. * In addition, some subclasses may also choose to implement other
  74. * <code>getXxxxInstance</code> methods for more specialized control. For
  75. * example, the <code>NumberFormat</code> class provides
  76. * <code>getPercentInstance</code> and <code>getCurrencyInstance</code>
  77. * methods for getting specialized number formatters.
  78. *
  79. * <p>
  80. * Subclasses of <code>Format</code> that allow programmers to create objects
  81. * for locales (with <code>getInstance(Locale)</code> for example)
  82. * must also implement the following class method:
  83. * <blockquote>
  84. * <pre>
  85. * public static Locale[] getAvailableLocales()
  86. * </pre>
  87. * </blockquote>
  88. *
  89. * <p>
  90. * And finally subclasses may define a set of constants to identify the various
  91. * fields in the formatted output. These constants are used to create a FieldPosition
  92. * object which identifies what information is contained in the field and its
  93. * position in the formatted result. These constants should be named
  94. * <code><em>item</em>_FIELD</code> where <code><em>item</em></code> identifies
  95. * the field. For examples of these constants, see <code>ERA_FIELD</code> and its
  96. * friends in {@link DateFormat}.
  97. *
  98. * @see java.text.ParsePosition
  99. * @see java.text.FieldPosition
  100. * @see java.text.NumberFormat
  101. * @see java.text.DateFormat
  102. * @see java.text.MessageFormat
  103. * @version 1.28, 01/19/00
  104. * @author Mark Davis
  105. */
  106. public abstract class Format implements Serializable, Cloneable {
  107. /**
  108. * Formats an object to produce a string.
  109. * <p>Subclasses will override the StringBuffer version of format.
  110. * @param obj The object to format
  111. * @return Formatted string.
  112. * @exception IllegalArgumentException when the Format cannot format the
  113. * type of object.
  114. * @see MessageFormat
  115. * @see java.text.Format#format(Object, StringBuffer, FieldPosition)
  116. */
  117. public final String format (Object obj) {
  118. return format(obj, new StringBuffer(), new FieldPosition(0)).toString();
  119. }
  120. /**
  121. * Formats an object to produce a string.
  122. * Subclasses will implement for particular object, such as:
  123. * <pre>
  124. * StringBuffer format (Number obj, StringBuffer toAppendTo)
  125. * Number parse (String str)
  126. * </pre>
  127. * These general routines allow polymorphic parsing and
  128. * formatting for objects such as the MessageFormat.
  129. * @param obj The object to format
  130. * @param toAppendTo where the text is to be appended
  131. * @param pos On input: an alignment field, if desired.
  132. * On output: the offsets of the alignment field.
  133. * @return the value passed in as toAppendTo (this allows chaining,
  134. * as with StringBuffer.append())
  135. * @exception IllegalArgumentException when the Format cannot format the
  136. * given object.
  137. * @see MessageFormat
  138. * @see java.text.FieldPosition
  139. */
  140. public abstract StringBuffer format(Object obj,
  141. StringBuffer toAppendTo,
  142. FieldPosition pos);
  143. /**
  144. * Parses a string to produce an object.
  145. * Subclasses will typically implement for particular object, such as:
  146. * <pre>
  147. * String format (Number obj);
  148. * String format (long obj);
  149. * String format (double obj);
  150. * Number parse (String str);
  151. * </pre>
  152. * @param status Input-Output parameter.
  153. * <p>Before calling, set status.index to the offset you want to start
  154. * parsing at in the source.
  155. * After calling, status.index is the end of the text you parsed.
  156. * If error occurs, index is unchanged.
  157. * <p>When parsing, leading whitespace is discarded
  158. * (with successful parse),
  159. * while trailing whitespace is left as is.
  160. * <p>Example:
  161. * Parsing "_12_xy" (where _ represents a space) for a number,
  162. * with index == 0 will result in
  163. * the number 12, with status.index updated to 3
  164. * (just before the second space).
  165. * Parsing a second time will result in a ParseException
  166. * since "xy" is not a number, and leave index at 3.
  167. * <p>Subclasses will typically supply specific parse methods that
  168. * return different types of values. Since methods can't overload on
  169. * return types, these will typically be named "parse", while this
  170. * polymorphic method will always be called parseObject.
  171. * Any parse method that does not take a status should
  172. * throw ParseException when no text in the required format is at
  173. * the start position.
  174. * @return Object parsed from string. In case of error, returns null.
  175. * @see java.text.ParsePosition
  176. */
  177. public abstract Object parseObject (String source, ParsePosition status);
  178. /**
  179. * Parses a string to produce an object.
  180. *
  181. * @exception ParseException if the specified string is invalid.
  182. */
  183. public Object parseObject(String source) throws ParseException {
  184. ParsePosition status = new ParsePosition(0);
  185. Object result = parseObject(source, status);
  186. if (status.index == 0) {
  187. throw new ParseException("Format.parseObject(String) failed",
  188. status.errorIndex);
  189. }
  190. return result;
  191. }
  192. /**
  193. * Overrides Cloneable
  194. */
  195. public Object clone() {
  196. try {
  197. Format other = (Format) super.clone();
  198. return other;
  199. } catch (CloneNotSupportedException e) {
  200. // will never happen
  201. return null;
  202. }
  203. }
  204. }