author | jboes |
Tue, 24 Sep 2019 09:43:43 +0100 | |
changeset 58288 | 48e480e56aad |
parent 58242 | 94bb65cb37d3 |
child 58679 | 9c3209ff7550 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54206 | 2 |
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved |
|
28 |
* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved |
|
29 |
* |
|
30 |
* The original version of this source code and documentation is copyrighted |
|
31 |
* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These |
|
32 |
* materials are provided under terms of a License Agreement between Taligent |
|
33 |
* and Sun. This technology is protected by multiple US and International |
|
34 |
* patents. This notice and attribution to Taligent may not be removed. |
|
35 |
* Taligent is a registered trademark of Taligent, Inc. |
|
36 |
* |
|
37 |
*/ |
|
38 |
||
39 |
package java.text; |
|
40 |
||
41 |
import java.io.Serializable; |
|
42 |
||
43 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
44 |
* {@code Format} is an abstract base class for formatting locale-sensitive |
2 | 45 |
* information such as dates, messages, and numbers. |
46 |
* |
|
47 |
* <p> |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
48 |
* {@code Format} defines the programming interface for formatting |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
49 |
* locale-sensitive objects into {@code String}s (the |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
50 |
* {@code format} method) and for parsing {@code String}s back |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
51 |
* into objects (the {@code parseObject} method). |
2 | 52 |
* |
53 |
* <p> |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
54 |
* Generally, a format's {@code parseObject} method must be able to parse |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
55 |
* any string formatted by its {@code format} method. However, there may |
2 | 56 |
* be exceptional cases where this is not possible. For example, a |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
57 |
* {@code format} method might create two adjacent integer numbers with |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
58 |
* no separator in between, and in this case the {@code parseObject} could |
2 | 59 |
* not tell which digits belong to which number. |
60 |
* |
|
54206 | 61 |
* <h2>Subclassing</h2> |
2 | 62 |
* |
63 |
* <p> |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
64 |
* The Java Platform provides three specialized subclasses of {@code Format}-- |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
65 |
* {@code DateFormat}, {@code MessageFormat}, and |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
66 |
* {@code NumberFormat}--for formatting dates, messages, and numbers, |
2 | 67 |
* respectively. |
68 |
* <p> |
|
69 |
* Concrete subclasses must implement three methods: |
|
70 |
* <ol> |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
71 |
* <li> {@code format(Object obj, StringBuffer toAppendTo, FieldPosition pos)} |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
72 |
* <li> {@code formatToCharacterIterator(Object obj)} |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
73 |
* <li> {@code parseObject(String source, ParsePosition pos)} |
2 | 74 |
* </ol> |
75 |
* These general methods allow polymorphic parsing and formatting of objects |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
76 |
* and are used, for example, by {@code MessageFormat}. |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
77 |
* Subclasses often also provide additional {@code format} methods for |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
78 |
* specific input types as well as {@code parse} methods for specific |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
79 |
* result types. Any {@code parse} method that does not take a |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
80 |
* {@code ParsePosition} argument should throw {@code ParseException} |
2 | 81 |
* when no text in the required format is at the beginning of the input text. |
82 |
* |
|
83 |
* <p> |
|
84 |
* Most subclasses will also implement the following factory methods: |
|
85 |
* <ol> |
|
86 |
* <li> |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
87 |
* {@code getInstance} for getting a useful format object appropriate |
2 | 88 |
* for the current locale |
89 |
* <li> |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
90 |
* {@code getInstance(Locale)} for getting a useful format |
2 | 91 |
* object appropriate for the specified locale |
92 |
* </ol> |
|
93 |
* In addition, some subclasses may also implement other |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
94 |
* {@code getXxxxInstance} methods for more specialized control. For |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
95 |
* example, the {@code NumberFormat} class provides |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
96 |
* {@code getPercentInstance} and {@code getCurrencyInstance} |
2 | 97 |
* methods for getting specialized number formatters. |
98 |
* |
|
99 |
* <p> |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
100 |
* Subclasses of {@code Format} that allow programmers to create objects |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
101 |
* for locales (with {@code getInstance(Locale)} for example) |
2 | 102 |
* must also implement the following class method: |
103 |
* <blockquote> |
|
104 |
* <pre> |
|
105 |
* public static Locale[] getAvailableLocales() |
|
106 |
* </pre> |
|
107 |
* </blockquote> |
|
108 |
* |
|
109 |
* <p> |
|
110 |
* And finally subclasses may define a set of constants to identify the various |
|
111 |
* fields in the formatted output. These constants are used to create a FieldPosition |
|
112 |
* object which identifies what information is contained in the field and its |
|
113 |
* position in the formatted result. These constants should be named |
|
114 |
* <code><em>item</em>_FIELD</code> where <code><em>item</em></code> identifies |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
115 |
* the field. For examples of these constants, see {@code ERA_FIELD} and its |
2 | 116 |
* friends in {@link DateFormat}. |
117 |
* |
|
54206 | 118 |
* <h3><a id="synchronization">Synchronization</a></h3> |
2 | 119 |
* |
120 |
* <p> |
|
121 |
* Formats are generally not synchronized. |
|
122 |
* It is recommended to create separate format instances for each thread. |
|
123 |
* If multiple threads access a format concurrently, it must be synchronized |
|
124 |
* externally. |
|
125 |
* |
|
126 |
* @see java.text.ParsePosition |
|
127 |
* @see java.text.FieldPosition |
|
128 |
* @see java.text.NumberFormat |
|
129 |
* @see java.text.DateFormat |
|
130 |
* @see java.text.MessageFormat |
|
131 |
* @author Mark Davis |
|
45434
4582657c7260
8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents:
44844
diff
changeset
|
132 |
* @since 1.1 |
2 | 133 |
*/ |
134 |
public abstract class Format implements Serializable, Cloneable { |
|
135 |
||
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
54206
diff
changeset
|
136 |
@java.io.Serial |
2 | 137 |
private static final long serialVersionUID = -299282585814624189L; |
138 |
||
139 |
/** |
|
140 |
* Sole constructor. (For invocation by subclass constructors, typically |
|
141 |
* implicit.) |
|
142 |
*/ |
|
143 |
protected Format() { |
|
144 |
} |
|
145 |
||
146 |
/** |
|
147 |
* Formats an object to produce a string. This is equivalent to |
|
148 |
* <blockquote> |
|
149 |
* {@link #format(Object, StringBuffer, FieldPosition) format}<code>(obj, |
|
150 |
* new StringBuffer(), new FieldPosition(0)).toString();</code> |
|
151 |
* </blockquote> |
|
152 |
* |
|
153 |
* @param obj The object to format |
|
154 |
* @return Formatted string. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57956
diff
changeset
|
155 |
* @throws IllegalArgumentException if the Format cannot format the given |
2 | 156 |
* object |
157 |
*/ |
|
158 |
public final String format (Object obj) { |
|
159 |
return format(obj, new StringBuffer(), new FieldPosition(0)).toString(); |
|
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* Formats an object and appends the resulting text to a given string |
|
164 |
* buffer. |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
165 |
* If the {@code pos} argument identifies a field used by the format, |
2 | 166 |
* then its indices are set to the beginning and end of the first such |
167 |
* field encountered. |
|
168 |
* |
|
169 |
* @param obj The object to format |
|
170 |
* @param toAppendTo where the text is to be appended |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
171 |
* @param pos A {@code FieldPosition} identifying a field |
2 | 172 |
* in the formatted text |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
173 |
* @return the string buffer passed in as {@code toAppendTo}, |
2 | 174 |
* with formatted text appended |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
175 |
* @throws NullPointerException if {@code toAppendTo} or |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
176 |
* {@code pos} is null |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57956
diff
changeset
|
177 |
* @throws IllegalArgumentException if the Format cannot format the given |
2 | 178 |
* object |
179 |
*/ |
|
180 |
public abstract StringBuffer format(Object obj, |
|
181 |
StringBuffer toAppendTo, |
|
182 |
FieldPosition pos); |
|
183 |
||
184 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
185 |
* Formats an Object producing an {@code AttributedCharacterIterator}. |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
186 |
* You can use the returned {@code AttributedCharacterIterator} |
2 | 187 |
* to build the resulting String, as well as to determine information |
188 |
* about the resulting String. |
|
189 |
* <p> |
|
190 |
* Each attribute key of the AttributedCharacterIterator will be of type |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
191 |
* {@code Field}. It is up to each {@code Format} implementation |
2 | 192 |
* to define what the legal values are for each attribute in the |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
193 |
* {@code AttributedCharacterIterator}, but typically the attribute |
2 | 194 |
* key is also used as the attribute value. |
195 |
* <p>The default implementation creates an |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
196 |
* {@code AttributedCharacterIterator} with no attributes. Subclasses |
2 | 197 |
* that support fields should override this and create an |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
198 |
* {@code AttributedCharacterIterator} with meaningful attributes. |
2 | 199 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57956
diff
changeset
|
200 |
* @throws NullPointerException if obj is null. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57956
diff
changeset
|
201 |
* @throws IllegalArgumentException when the Format cannot format the |
2 | 202 |
* given object. |
203 |
* @param obj The object to format |
|
204 |
* @return AttributedCharacterIterator describing the formatted value. |
|
205 |
* @since 1.4 |
|
206 |
*/ |
|
207 |
public AttributedCharacterIterator formatToCharacterIterator(Object obj) { |
|
208 |
return createAttributedCharacterIterator(format(obj)); |
|
209 |
} |
|
210 |
||
211 |
/** |
|
212 |
* Parses text from a string to produce an object. |
|
213 |
* <p> |
|
214 |
* The method attempts to parse text starting at the index given by |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
215 |
* {@code pos}. |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
216 |
* If parsing succeeds, then the index of {@code pos} is updated |
2 | 217 |
* to the index after the last character used (parsing does not necessarily |
218 |
* use all characters up to the end of the string), and the parsed |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
219 |
* object is returned. The updated {@code pos} can be used to |
2 | 220 |
* indicate the starting point for the next call to this method. |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
221 |
* If an error occurs, then the index of {@code pos} is not |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
222 |
* changed, the error index of {@code pos} is set to the index of |
2 | 223 |
* the character where the error occurred, and null is returned. |
224 |
* |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
225 |
* @param source A {@code String}, part of which should be parsed. |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
226 |
* @param pos A {@code ParsePosition} object with index and error |
2 | 227 |
* index information as described above. |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
228 |
* @return An {@code Object} parsed from the string. In case of |
2 | 229 |
* error, returns null. |
34328
0534cb944d0e
8073211: javadoc of Format parseObject methods should specify NullPointerExceptions
okutsu
parents:
25859
diff
changeset
|
230 |
* @throws NullPointerException if {@code source} or {@code pos} is null. |
2 | 231 |
*/ |
232 |
public abstract Object parseObject (String source, ParsePosition pos); |
|
233 |
||
234 |
/** |
|
235 |
* Parses text from the beginning of the given string to produce an object. |
|
236 |
* The method may not use the entire text of the given string. |
|
237 |
* |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
238 |
* @param source A {@code String} whose beginning should be parsed. |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
239 |
* @return An {@code Object} parsed from the string. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57956
diff
changeset
|
240 |
* @throws ParseException if the beginning of the specified string |
2 | 241 |
* cannot be parsed. |
34328
0534cb944d0e
8073211: javadoc of Format parseObject methods should specify NullPointerExceptions
okutsu
parents:
25859
diff
changeset
|
242 |
* @throws NullPointerException if {@code source} is null. |
2 | 243 |
*/ |
244 |
public Object parseObject(String source) throws ParseException { |
|
245 |
ParsePosition pos = new ParsePosition(0); |
|
246 |
Object result = parseObject(source, pos); |
|
247 |
if (pos.index == 0) { |
|
248 |
throw new ParseException("Format.parseObject(String) failed", |
|
249 |
pos.errorIndex); |
|
250 |
} |
|
251 |
return result; |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* Creates and returns a copy of this object. |
|
256 |
* |
|
257 |
* @return a clone of this instance. |
|
258 |
*/ |
|
259 |
public Object clone() { |
|
260 |
try { |
|
261 |
return super.clone(); |
|
262 |
} catch (CloneNotSupportedException e) { |
|
263 |
// will never happen |
|
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
5506
diff
changeset
|
264 |
throw new InternalError(e); |
2 | 265 |
} |
266 |
} |
|
267 |
||
268 |
// |
|
269 |
// Convenience methods for creating AttributedCharacterIterators from |
|
270 |
// different parameters. |
|
271 |
// |
|
272 |
||
273 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
274 |
* Creates an {@code AttributedCharacterIterator} for the String |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
275 |
* {@code s}. |
2 | 276 |
* |
277 |
* @param s String to create AttributedCharacterIterator from |
|
278 |
* @return AttributedCharacterIterator wrapping s |
|
279 |
*/ |
|
280 |
AttributedCharacterIterator createAttributedCharacterIterator(String s) { |
|
281 |
AttributedString as = new AttributedString(s); |
|
282 |
||
283 |
return as.getIterator(); |
|
284 |
} |
|
285 |
||
286 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
287 |
* Creates an {@code AttributedCharacterIterator} containing the |
2 | 288 |
* concatenated contents of the passed in |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
289 |
* {@code AttributedCharacterIterator}s. |
2 | 290 |
* |
291 |
* @param iterators AttributedCharacterIterators used to create resulting |
|
292 |
* AttributedCharacterIterators |
|
293 |
* @return AttributedCharacterIterator wrapping passed in |
|
294 |
* AttributedCharacterIterators |
|
295 |
*/ |
|
296 |
AttributedCharacterIterator createAttributedCharacterIterator( |
|
297 |
AttributedCharacterIterator[] iterators) { |
|
298 |
AttributedString as = new AttributedString(iterators); |
|
299 |
||
300 |
return as.getIterator(); |
|
301 |
} |
|
302 |
||
303 |
/** |
|
304 |
* Returns an AttributedCharacterIterator with the String |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
305 |
* {@code string} and additional key/value pair {@code key}, |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
306 |
* {@code value}. |
2 | 307 |
* |
308 |
* @param string String to create AttributedCharacterIterator from |
|
309 |
* @param key Key for AttributedCharacterIterator |
|
310 |
* @param value Value associated with key in AttributedCharacterIterator |
|
311 |
* @return AttributedCharacterIterator wrapping args |
|
312 |
*/ |
|
313 |
AttributedCharacterIterator createAttributedCharacterIterator( |
|
314 |
String string, AttributedCharacterIterator.Attribute key, |
|
315 |
Object value) { |
|
316 |
AttributedString as = new AttributedString(string); |
|
317 |
||
318 |
as.addAttribute(key, value); |
|
319 |
return as.getIterator(); |
|
320 |
} |
|
321 |
||
322 |
/** |
|
323 |
* Creates an AttributedCharacterIterator with the contents of |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
324 |
* {@code iterator} and the additional attribute {@code key} |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
325 |
* {@code value}. |
2 | 326 |
* |
327 |
* @param iterator Initial AttributedCharacterIterator to add arg to |
|
328 |
* @param key Key for AttributedCharacterIterator |
|
329 |
* @param value Value associated with key in AttributedCharacterIterator |
|
330 |
* @return AttributedCharacterIterator wrapping args |
|
331 |
*/ |
|
332 |
AttributedCharacterIterator createAttributedCharacterIterator( |
|
333 |
AttributedCharacterIterator iterator, |
|
334 |
AttributedCharacterIterator.Attribute key, Object value) { |
|
335 |
AttributedString as = new AttributedString(iterator); |
|
336 |
||
337 |
as.addAttribute(key, value); |
|
338 |
return as.getIterator(); |
|
339 |
} |
|
340 |
||
341 |
||
342 |
/** |
|
343 |
* Defines constants that are used as attribute keys in the |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
344 |
* {@code AttributedCharacterIterator} returned |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
345 |
* from {@code Format.formatToCharacterIterator} and as |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
346 |
* field identifiers in {@code FieldPosition}. |
2 | 347 |
* |
348 |
* @since 1.4 |
|
349 |
*/ |
|
350 |
public static class Field extends AttributedCharacterIterator.Attribute { |
|
351 |
||
352 |
// Proclaim serial compatibility with 1.4 FCS |
|
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
54206
diff
changeset
|
353 |
@java.io.Serial |
2 | 354 |
private static final long serialVersionUID = 276966692217360283L; |
355 |
||
356 |
/** |
|
357 |
* Creates a Field with the specified name. |
|
358 |
* |
|
359 |
* @param name Name of the attribute |
|
360 |
*/ |
|
361 |
protected Field(String name) { |
|
362 |
super(name); |
|
363 |
} |
|
364 |
} |
|
365 |
||
366 |
||
367 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
368 |
* FieldDelegate is notified by the various {@code Format} |
2 | 369 |
* implementations as they are formatting the Objects. This allows for |
370 |
* storage of the individual sections of the formatted String for |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
371 |
* later use, such as in a {@code FieldPosition} or for an |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
372 |
* {@code AttributedCharacterIterator}. |
2 | 373 |
* <p> |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
374 |
* Delegates should NOT assume that the {@code Format} will notify |
2 | 375 |
* the delegate of fields in any particular order. |
376 |
* |
|
14014 | 377 |
* @see FieldPosition#getFieldDelegate |
2 | 378 |
* @see CharacterIteratorFieldDelegate |
379 |
*/ |
|
380 |
interface FieldDelegate { |
|
381 |
/** |
|
382 |
* Notified when a particular region of the String is formatted. This |
|
383 |
* method will be invoked if there is no corresponding integer field id |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
384 |
* matching {@code attr}. |
2 | 385 |
* |
386 |
* @param attr Identifies the field matched |
|
387 |
* @param value Value associated with the field |
|
388 |
* @param start Beginning location of the field, will be >= 0 |
|
389 |
* @param end End of the field, will be >= start and <= buffer.length() |
|
390 |
* @param buffer Contains current formatted value, receiver should |
|
391 |
* NOT modify it. |
|
392 |
*/ |
|
393 |
public void formatted(Format.Field attr, Object value, int start, |
|
394 |
int end, StringBuffer buffer); |
|
395 |
||
396 |
/** |
|
397 |
* Notified when a particular region of the String is formatted. |
|
398 |
* |
|
399 |
* @param fieldID Identifies the field by integer |
|
400 |
* @param attr Identifies the field matched |
|
401 |
* @param value Value associated with the field |
|
402 |
* @param start Beginning location of the field, will be >= 0 |
|
403 |
* @param end End of the field, will be >= start and <= buffer.length() |
|
404 |
* @param buffer Contains current formatted value, receiver should |
|
405 |
* NOT modify it. |
|
406 |
*/ |
|
407 |
public void formatted(int fieldID, Format.Field attr, Object value, |
|
408 |
int start, int end, StringBuffer buffer); |
|
409 |
} |
|
410 |
} |