Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/text/AttributedCharacterIterator.java
41152 views
1
/*
2
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
24
*/
25
26
package java.text;
27
28
import java.io.InvalidObjectException;
29
import java.io.Serializable;
30
import java.util.HashMap;
31
import java.util.Map;
32
import java.util.Set;
33
34
/**
35
* An {@code AttributedCharacterIterator} allows iteration through both text and
36
* related attribute information.
37
*
38
* <p>
39
* An attribute is a key/value pair, identified by the key. No two
40
* attributes on a given character can have the same key.
41
*
42
* <p>The values for an attribute are immutable, or must not be mutated
43
* by clients or storage. They are always passed by reference, and not
44
* cloned.
45
*
46
* <p>A <em>run with respect to an attribute</em> is a maximum text range for
47
* which:
48
* <ul>
49
* <li>the attribute is undefined or {@code null} for the entire range, or
50
* <li>the attribute value is defined and has the same non-{@code null} value for the
51
* entire range.
52
* </ul>
53
*
54
* <p>A <em>run with respect to a set of attributes</em> is a maximum text range for
55
* which this condition is met for each member attribute.
56
*
57
* <p>When getting a run with no explicit attributes specified (i.e.,
58
* calling {@link #getRunStart()} and {@link #getRunLimit()}), any
59
* contiguous text segments having the same attributes (the same set
60
* of attribute/value pairs) are treated as separate runs if the
61
* attributes have been given to those text segments separately.
62
*
63
* <p>The returned indexes are limited to the range of the iterator.
64
*
65
* <p>The returned attribute information is limited to runs that contain
66
* the current character.
67
*
68
* <p>
69
* Attribute keys are instances of {@link AttributedCharacterIterator.Attribute} and its
70
* subclasses, such as {@link java.awt.font.TextAttribute}.
71
*
72
* @see AttributedCharacterIterator.Attribute
73
* @see java.awt.font.TextAttribute
74
* @see AttributedString
75
* @see Annotation
76
* @since 1.2
77
*/
78
79
public interface AttributedCharacterIterator extends CharacterIterator {
80
81
/**
82
* Defines attribute keys that are used to identify text attributes. These
83
* keys are used in {@code AttributedCharacterIterator} and {@code AttributedString}.
84
* @see AttributedCharacterIterator
85
* @see AttributedString
86
* @since 1.2
87
*/
88
89
public static class Attribute implements Serializable {
90
91
/**
92
* The name of this {@code Attribute}. The name is used primarily by {@code readResolve}
93
* to look up the corresponding predefined instance when deserializing
94
* an instance.
95
* @serial
96
*/
97
private String name;
98
99
// table of all instances in this class, used by readResolve
100
private static final Map<String, Attribute> instanceMap = new HashMap<>(7);
101
102
/**
103
* Constructs an {@code Attribute} with the given name.
104
*
105
* @param name the name of {@code Attribute}
106
*/
107
protected Attribute(String name) {
108
this.name = name;
109
if (this.getClass() == Attribute.class) {
110
instanceMap.put(name, this);
111
}
112
}
113
114
/**
115
* Compares two objects for equality. This version only returns true
116
* for {@code x.equals(y)} if {@code x} and {@code y} refer
117
* to the same object, and guarantees this for all subclasses.
118
*/
119
public final boolean equals(Object obj) {
120
return super.equals(obj);
121
}
122
123
/**
124
* Returns a hash code value for the object. This version is identical to
125
* the one in {@code Object}, but is also final.
126
*/
127
public final int hashCode() {
128
return super.hashCode();
129
}
130
131
/**
132
* Returns a string representation of the object. This version returns the
133
* concatenation of class name, {@code "("}, a name identifying the attribute
134
* and {@code ")"}.
135
*/
136
public String toString() {
137
return getClass().getName() + "(" + name + ")";
138
}
139
140
/**
141
* Returns the name of the attribute.
142
*
143
* @return the name of {@code Attribute}
144
*/
145
protected String getName() {
146
return name;
147
}
148
149
/**
150
* Resolves instances being deserialized to the predefined constants.
151
*
152
* @return the resolved {@code Attribute} object
153
* @throws InvalidObjectException if the object to resolve is not
154
* an instance of {@code Attribute}
155
*/
156
@java.io.Serial
157
protected Object readResolve() throws InvalidObjectException {
158
if (this.getClass() != Attribute.class) {
159
throw new InvalidObjectException("subclass didn't correctly implement readResolve");
160
}
161
162
Attribute instance = instanceMap.get(getName());
163
if (instance != null) {
164
return instance;
165
} else {
166
throw new InvalidObjectException("unknown attribute name");
167
}
168
}
169
170
/**
171
* Attribute key for the language of some text.
172
* <p> Values are instances of {@link java.util.Locale Locale}.
173
* @see java.util.Locale
174
*/
175
public static final Attribute LANGUAGE = new Attribute("language");
176
177
/**
178
* Attribute key for the reading of some text. In languages where the written form
179
* and the pronunciation of a word are only loosely related (such as Japanese),
180
* it is often necessary to store the reading (pronunciation) along with the
181
* written form.
182
* <p>Values are instances of {@link Annotation} holding instances of {@link String}.
183
*
184
* @see Annotation
185
* @see java.lang.String
186
*/
187
public static final Attribute READING = new Attribute("reading");
188
189
/**
190
* Attribute key for input method segments. Input methods often break
191
* up text into segments, which usually correspond to words.
192
* <p>Values are instances of {@link Annotation} holding a {@code null} reference.
193
* @see Annotation
194
*/
195
public static final Attribute INPUT_METHOD_SEGMENT = new Attribute("input_method_segment");
196
197
// make sure the serial version doesn't change between compiler versions
198
@java.io.Serial
199
private static final long serialVersionUID = -9142742483513960612L;
200
201
};
202
203
/**
204
* Returns the index of the first character of the run
205
* with respect to all attributes containing the current character.
206
*
207
* <p>Any contiguous text segments having the same attributes (the
208
* same set of attribute/value pairs) are treated as separate runs
209
* if the attributes have been given to those text segments separately.
210
*
211
* @return the index of the first character of the run
212
*/
213
public int getRunStart();
214
215
/**
216
* Returns the index of the first character of the run
217
* with respect to the given {@code attribute} containing the current character.
218
*
219
* @param attribute the desired attribute.
220
* @return the index of the first character of the run
221
*/
222
public int getRunStart(Attribute attribute);
223
224
/**
225
* Returns the index of the first character of the run
226
* with respect to the given {@code attributes} containing the current character.
227
*
228
* @param attributes a set of the desired attributes.
229
* @return the index of the first character of the run
230
*/
231
public int getRunStart(Set<? extends Attribute> attributes);
232
233
/**
234
* Returns the index of the first character following the run
235
* with respect to all attributes containing the current character.
236
*
237
* <p>Any contiguous text segments having the same attributes (the
238
* same set of attribute/value pairs) are treated as separate runs
239
* if the attributes have been given to those text segments separately.
240
*
241
* @return the index of the first character following the run
242
*/
243
public int getRunLimit();
244
245
/**
246
* Returns the index of the first character following the run
247
* with respect to the given {@code attribute} containing the current character.
248
*
249
* @param attribute the desired attribute
250
* @return the index of the first character following the run
251
*/
252
public int getRunLimit(Attribute attribute);
253
254
/**
255
* Returns the index of the first character following the run
256
* with respect to the given {@code attributes} containing the current character.
257
*
258
* @param attributes a set of the desired attributes
259
* @return the index of the first character following the run
260
*/
261
public int getRunLimit(Set<? extends Attribute> attributes);
262
263
/**
264
* Returns a map with the attributes defined on the current
265
* character.
266
*
267
* @return a map with the attributes defined on the current character
268
*/
269
public Map<Attribute,Object> getAttributes();
270
271
/**
272
* Returns the value of the named {@code attribute} for the current character.
273
* Returns {@code null} if the {@code attribute} is not defined.
274
*
275
* @param attribute the desired attribute
276
* @return the value of the named {@code attribute} or {@code null}
277
*/
278
public Object getAttribute(Attribute attribute);
279
280
/**
281
* Returns the keys of all attributes defined on the
282
* iterator's text range. The set is empty if no
283
* attributes are defined.
284
*
285
* @return the keys of all attributes
286
*/
287
public Set<Attribute> getAllAttributeKeys();
288
};
289
290