1 | /* Format.java -- Abstract superclass for formatting/parsing strings.
|
---|
2 | Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
6 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
21 | Linking this library statically or dynamically with other modules is
|
---|
22 | making a combined work based on this library. Thus, the terms and
|
---|
23 | conditions of the GNU General Public License cover the whole
|
---|
24 | combination.
|
---|
25 |
|
---|
26 | As a special exception, the copyright holders of this library give you
|
---|
27 | permission to link this library with independent modules to produce an
|
---|
28 | executable, regardless of the license terms of these independent
|
---|
29 | modules, and to copy and distribute the resulting executable under
|
---|
30 | terms of your choice, provided that you also meet, for each linked
|
---|
31 | independent module, the terms and conditions of the license of that
|
---|
32 | module. An independent module is a module which is not derived from
|
---|
33 | or based on this library. If you modify this library, you may extend
|
---|
34 | this exception to your version of the library, but you are not
|
---|
35 | obligated to do so. If you do not wish to do so, delete this
|
---|
36 | exception statement from your version. */
|
---|
37 |
|
---|
38 |
|
---|
39 | package java.text;
|
---|
40 |
|
---|
41 | import java.io.Serializable;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * This class is the abstract superclass of classes that format and parse
|
---|
45 | * data to/from <code>Strings</code>. It is guaranteed that any
|
---|
46 | * <code>String</code> produced by a concrete subclass of <code>Format</code>
|
---|
47 | * will be parseable by that same subclass.
|
---|
48 | * <p>
|
---|
49 | * In addition to implementing the abstract methods in this class, subclasses
|
---|
50 | * should provide static factory methods of the form
|
---|
51 | * <code>getInstance()</code> and <code>getInstance(Locale)</code> if the
|
---|
52 | * subclass loads different formatting/parsing schemes based on locale.
|
---|
53 | * These subclasses should also implement a static method called
|
---|
54 | * <code>getAvailableLocales()</code> which returns an array of
|
---|
55 | * available locales in the current runtime environment.
|
---|
56 | *
|
---|
57 | * @author Aaron M. Renn (arenn@urbanophile.com)
|
---|
58 | * @author Per Bothner <bothner@cygnus.com>
|
---|
59 | */
|
---|
60 | public abstract class Format implements Serializable, Cloneable
|
---|
61 | {
|
---|
62 | static final long serialVersionUID = -299282585814624189L;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * This method initializes a new instance of <code>Format</code>.
|
---|
66 | * It performs no actions, but acts as a default constructor for
|
---|
67 | * subclasses.
|
---|
68 | */
|
---|
69 | public Format ()
|
---|
70 | {
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * This method formats an <code>Object</code> into a <code>String</code>.
|
---|
75 | *
|
---|
76 | * @param obj The <code>Object</code> to format.
|
---|
77 | *
|
---|
78 | * @return The formatted <code>String</code>.
|
---|
79 | *
|
---|
80 | * @exception IllegalArgumentException If the <code>Object</code>
|
---|
81 | * cannot be formatted.
|
---|
82 | */
|
---|
83 | public final String format(Object obj) throws IllegalArgumentException
|
---|
84 | {
|
---|
85 | StringBuffer sb = new StringBuffer ();
|
---|
86 | format (obj, sb, new FieldPosition (0));
|
---|
87 | return sb.toString ();
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * This method formats an <code>Object</code> into a <code>String</code> and
|
---|
92 | * appends the <code>String</code> to a <code>StringBuffer</code>.
|
---|
93 | *
|
---|
94 | * @param obj The <code>Object</code> to format.
|
---|
95 | * @param sb The <code>StringBuffer</code> to append to.
|
---|
96 | * @param pos The desired <code>FieldPosition</code>, which is also
|
---|
97 | * updated by this call.
|
---|
98 | *
|
---|
99 | * @return The updated <code>StringBuffer</code>.
|
---|
100 | *
|
---|
101 | * @exception IllegalArgumentException If the <code>Object</code>
|
---|
102 | * cannot be formatted.
|
---|
103 | */
|
---|
104 | public abstract StringBuffer format (Object obj, StringBuffer sb,
|
---|
105 | FieldPosition pos)
|
---|
106 | throws IllegalArgumentException;
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * This method parses a <code>String</code> and converts the parsed
|
---|
110 | * contents into an <code>Object</code>.
|
---|
111 | *
|
---|
112 | * @param str The <code>String to parse.
|
---|
113 | *
|
---|
114 | * @return The resulting <code>Object</code>.
|
---|
115 | *
|
---|
116 | * @exception ParseException If the <code>String</code> cannot be parsed.
|
---|
117 | */
|
---|
118 | public Object parseObject (String str) throws ParseException
|
---|
119 | {
|
---|
120 | ParsePosition pos = new ParsePosition(0);
|
---|
121 | Object result = parseObject (str, pos);
|
---|
122 | if (result == null)
|
---|
123 | {
|
---|
124 | int index = pos.getErrorIndex();
|
---|
125 | if (index < 0)
|
---|
126 | index = pos.getIndex();
|
---|
127 | throw new ParseException("parseObject failed", index);
|
---|
128 | }
|
---|
129 | return result;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * This method parses a <code>String</code> and converts the parsed
|
---|
134 | * contents into an <code>Object</code>.
|
---|
135 | *
|
---|
136 | * @param str The <code>String</code> to parse.
|
---|
137 | * @param pos The starting parse index on input, the ending parse
|
---|
138 | * index on output.
|
---|
139 | *
|
---|
140 | * @return The parsed <code>Object</code>, or <code>null</code> in
|
---|
141 | * case of error.
|
---|
142 | */
|
---|
143 | public abstract Object parseObject (String str, ParsePosition pos);
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Creates a copy of this object.
|
---|
147 | *
|
---|
148 | * @return The copied <code>Object</code>.
|
---|
149 | */
|
---|
150 | public Object clone ()
|
---|
151 | {
|
---|
152 | try
|
---|
153 | {
|
---|
154 | return super.clone ();
|
---|
155 | }
|
---|
156 | catch (CloneNotSupportedException e)
|
---|
157 | {
|
---|
158 | return null;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|