jtulach@1334
|
1 |
/*
|
jtulach@1334
|
2 |
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
|
jtulach@1334
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jtulach@1334
|
4 |
*
|
jtulach@1334
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jtulach@1334
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jtulach@1334
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jtulach@1334
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jtulach@1334
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jtulach@1334
|
10 |
*
|
jtulach@1334
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jtulach@1334
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jtulach@1334
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jtulach@1334
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jtulach@1334
|
15 |
* accompanied this code).
|
jtulach@1334
|
16 |
*
|
jtulach@1334
|
17 |
* You should have received a copy of the GNU General Public License version
|
jtulach@1334
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jtulach@1334
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jtulach@1334
|
20 |
*
|
jtulach@1334
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jtulach@1334
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jtulach@1334
|
23 |
* questions.
|
jtulach@1334
|
24 |
*/
|
jtulach@1334
|
25 |
|
jtulach@1334
|
26 |
package java.text;
|
jtulach@1334
|
27 |
|
jtulach@1334
|
28 |
import java.io.InvalidObjectException;
|
jtulach@1334
|
29 |
import java.io.Serializable;
|
jtulach@1334
|
30 |
import java.util.HashMap;
|
jtulach@1334
|
31 |
import java.util.Map;
|
jtulach@1334
|
32 |
import java.util.Set;
|
jtulach@1334
|
33 |
|
jtulach@1334
|
34 |
/**
|
jtulach@1334
|
35 |
* An {@code AttributedCharacterIterator} allows iteration through both text and
|
jtulach@1334
|
36 |
* related attribute information.
|
jtulach@1334
|
37 |
*
|
jtulach@1334
|
38 |
* <p>
|
jtulach@1334
|
39 |
* An attribute is a key/value pair, identified by the key. No two
|
jtulach@1334
|
40 |
* attributes on a given character can have the same key.
|
jtulach@1334
|
41 |
*
|
jtulach@1334
|
42 |
* <p>The values for an attribute are immutable, or must not be mutated
|
jtulach@1334
|
43 |
* by clients or storage. They are always passed by reference, and not
|
jtulach@1334
|
44 |
* cloned.
|
jtulach@1334
|
45 |
*
|
jtulach@1334
|
46 |
* <p>A <em>run with respect to an attribute</em> is a maximum text range for
|
jtulach@1334
|
47 |
* which:
|
jtulach@1334
|
48 |
* <ul>
|
jtulach@1334
|
49 |
* <li>the attribute is undefined or {@code null} for the entire range, or
|
jtulach@1334
|
50 |
* <li>the attribute value is defined and has the same non-{@code null} value for the
|
jtulach@1334
|
51 |
* entire range.
|
jtulach@1334
|
52 |
* </ul>
|
jtulach@1334
|
53 |
*
|
jtulach@1334
|
54 |
* <p>A <em>run with respect to a set of attributes</em> is a maximum text range for
|
jtulach@1334
|
55 |
* which this condition is met for each member attribute.
|
jtulach@1334
|
56 |
*
|
jtulach@1334
|
57 |
* <p>When getting a run with no explicit attributes specified (i.e.,
|
jtulach@1334
|
58 |
* calling {@link #getRunStart()} and {@link #getRunLimit()}), any
|
jtulach@1334
|
59 |
* contiguous text segments having the same attributes (the same set
|
jtulach@1334
|
60 |
* of attribute/value pairs) are treated as separate runs if the
|
jtulach@1334
|
61 |
* attributes have been given to those text segments separately.
|
jtulach@1334
|
62 |
*
|
jtulach@1334
|
63 |
* <p>The returned indexes are limited to the range of the iterator.
|
jtulach@1334
|
64 |
*
|
jtulach@1334
|
65 |
* <p>The returned attribute information is limited to runs that contain
|
jtulach@1334
|
66 |
* the current character.
|
jtulach@1334
|
67 |
*
|
jtulach@1334
|
68 |
* <p>
|
jtulach@1334
|
69 |
* Attribute keys are instances of {@link AttributedCharacterIterator.Attribute} and its
|
jtulach@1334
|
70 |
* subclasses, such as {@link java.awt.font.TextAttribute}.
|
jtulach@1334
|
71 |
*
|
jtulach@1334
|
72 |
* @see AttributedCharacterIterator.Attribute
|
jtulach@1334
|
73 |
* @see java.awt.font.TextAttribute
|
jtulach@1334
|
74 |
* @see AttributedString
|
jtulach@1334
|
75 |
* @see Annotation
|
jtulach@1334
|
76 |
* @since 1.2
|
jtulach@1334
|
77 |
*/
|
jtulach@1334
|
78 |
|
jtulach@1334
|
79 |
public interface AttributedCharacterIterator extends CharacterIterator {
|
jtulach@1334
|
80 |
|
jtulach@1334
|
81 |
/**
|
jtulach@1334
|
82 |
* Defines attribute keys that are used to identify text attributes. These
|
jtulach@1334
|
83 |
* keys are used in {@code AttributedCharacterIterator} and {@code AttributedString}.
|
jtulach@1334
|
84 |
* @see AttributedCharacterIterator
|
jtulach@1334
|
85 |
* @see AttributedString
|
jtulach@1334
|
86 |
* @since 1.2
|
jtulach@1334
|
87 |
*/
|
jtulach@1334
|
88 |
|
jtulach@1334
|
89 |
public static class Attribute implements Serializable {
|
jtulach@1334
|
90 |
|
jtulach@1334
|
91 |
/**
|
jtulach@1334
|
92 |
* The name of this {@code Attribute}. The name is used primarily by {@code readResolve}
|
jtulach@1334
|
93 |
* to look up the corresponding predefined instance when deserializing
|
jtulach@1334
|
94 |
* an instance.
|
jtulach@1334
|
95 |
* @serial
|
jtulach@1334
|
96 |
*/
|
jtulach@1334
|
97 |
private String name;
|
jtulach@1334
|
98 |
|
jtulach@1334
|
99 |
// table of all instances in this class, used by readResolve
|
jtulach@1334
|
100 |
private static final Map instanceMap = new HashMap(7);
|
jtulach@1334
|
101 |
|
jtulach@1334
|
102 |
/**
|
jtulach@1334
|
103 |
* Constructs an {@code Attribute} with the given name.
|
jtulach@1334
|
104 |
*/
|
jtulach@1334
|
105 |
protected Attribute(String name) {
|
jtulach@1334
|
106 |
this.name = name;
|
jtulach@1334
|
107 |
if (this.getClass() == Attribute.class) {
|
jtulach@1334
|
108 |
instanceMap.put(name, this);
|
jtulach@1334
|
109 |
}
|
jtulach@1334
|
110 |
}
|
jtulach@1334
|
111 |
|
jtulach@1334
|
112 |
/**
|
jtulach@1334
|
113 |
* Compares two objects for equality. This version only returns true
|
jtulach@1334
|
114 |
* for <code>x.equals(y)</code> if <code>x</code> and <code>y</code> refer
|
jtulach@1334
|
115 |
* to the same object, and guarantees this for all subclasses.
|
jtulach@1334
|
116 |
*/
|
jtulach@1334
|
117 |
public final boolean equals(Object obj) {
|
jtulach@1334
|
118 |
return super.equals(obj);
|
jtulach@1334
|
119 |
}
|
jtulach@1334
|
120 |
|
jtulach@1334
|
121 |
/**
|
jtulach@1334
|
122 |
* Returns a hash code value for the object. This version is identical to
|
jtulach@1334
|
123 |
* the one in {@code Object}, but is also final.
|
jtulach@1334
|
124 |
*/
|
jtulach@1334
|
125 |
public final int hashCode() {
|
jtulach@1334
|
126 |
return super.hashCode();
|
jtulach@1334
|
127 |
}
|
jtulach@1334
|
128 |
|
jtulach@1334
|
129 |
/**
|
jtulach@1334
|
130 |
* Returns a string representation of the object. This version returns the
|
jtulach@1334
|
131 |
* concatenation of class name, {@code "("}, a name identifying the attribute
|
jtulach@1334
|
132 |
* and {@code ")"}.
|
jtulach@1334
|
133 |
*/
|
jtulach@1334
|
134 |
public String toString() {
|
jtulach@1334
|
135 |
return getClass().getName() + "(" + name + ")";
|
jtulach@1334
|
136 |
}
|
jtulach@1334
|
137 |
|
jtulach@1334
|
138 |
/**
|
jtulach@1334
|
139 |
* Returns the name of the attribute.
|
jtulach@1334
|
140 |
*/
|
jtulach@1334
|
141 |
protected String getName() {
|
jtulach@1334
|
142 |
return name;
|
jtulach@1334
|
143 |
}
|
jtulach@1334
|
144 |
|
jtulach@1334
|
145 |
/**
|
jtulach@1334
|
146 |
* Resolves instances being deserialized to the predefined constants.
|
jtulach@1334
|
147 |
*/
|
jtulach@1334
|
148 |
protected Object readResolve() throws InvalidObjectException {
|
jtulach@1334
|
149 |
if (this.getClass() != Attribute.class) {
|
jtulach@1334
|
150 |
throw new InvalidObjectException("subclass didn't correctly implement readResolve");
|
jtulach@1334
|
151 |
}
|
jtulach@1334
|
152 |
|
jtulach@1334
|
153 |
Attribute instance = (Attribute) instanceMap.get(getName());
|
jtulach@1334
|
154 |
if (instance != null) {
|
jtulach@1334
|
155 |
return instance;
|
jtulach@1334
|
156 |
} else {
|
jtulach@1334
|
157 |
throw new InvalidObjectException("unknown attribute name");
|
jtulach@1334
|
158 |
}
|
jtulach@1334
|
159 |
}
|
jtulach@1334
|
160 |
|
jtulach@1334
|
161 |
/**
|
jtulach@1334
|
162 |
* Attribute key for the language of some text.
|
jtulach@1334
|
163 |
* <p> Values are instances of {@link java.util.Locale Locale}.
|
jtulach@1334
|
164 |
* @see java.util.Locale
|
jtulach@1334
|
165 |
*/
|
jtulach@1334
|
166 |
public static final Attribute LANGUAGE = new Attribute("language");
|
jtulach@1334
|
167 |
|
jtulach@1334
|
168 |
/**
|
jtulach@1334
|
169 |
* Attribute key for the reading of some text. In languages where the written form
|
jtulach@1334
|
170 |
* and the pronunciation of a word are only loosely related (such as Japanese),
|
jtulach@1334
|
171 |
* it is often necessary to store the reading (pronunciation) along with the
|
jtulach@1334
|
172 |
* written form.
|
jtulach@1334
|
173 |
* <p>Values are instances of {@link Annotation} holding instances of {@link String}.
|
jtulach@1334
|
174 |
* @see Annotation
|
jtulach@1334
|
175 |
* @see java.lang.String
|
jtulach@1334
|
176 |
*/
|
jtulach@1334
|
177 |
public static final Attribute READING = new Attribute("reading");
|
jtulach@1334
|
178 |
|
jtulach@1334
|
179 |
/**
|
jtulach@1334
|
180 |
* Attribute key for input method segments. Input methods often break
|
jtulach@1334
|
181 |
* up text into segments, which usually correspond to words.
|
jtulach@1334
|
182 |
* <p>Values are instances of {@link Annotation} holding a {@code null} reference.
|
jtulach@1334
|
183 |
* @see Annotation
|
jtulach@1334
|
184 |
*/
|
jtulach@1334
|
185 |
public static final Attribute INPUT_METHOD_SEGMENT = new Attribute("input_method_segment");
|
jtulach@1334
|
186 |
|
jtulach@1334
|
187 |
// make sure the serial version doesn't change between compiler versions
|
jtulach@1334
|
188 |
private static final long serialVersionUID = -9142742483513960612L;
|
jtulach@1334
|
189 |
|
jtulach@1334
|
190 |
};
|
jtulach@1334
|
191 |
|
jtulach@1334
|
192 |
/**
|
jtulach@1334
|
193 |
* Returns the index of the first character of the run
|
jtulach@1334
|
194 |
* with respect to all attributes containing the current character.
|
jtulach@1334
|
195 |
*
|
jtulach@1334
|
196 |
* <p>Any contiguous text segments having the same attributes (the
|
jtulach@1334
|
197 |
* same set of attribute/value pairs) are treated as separate runs
|
jtulach@1334
|
198 |
* if the attributes have been given to those text segments separately.
|
jtulach@1334
|
199 |
*/
|
jtulach@1334
|
200 |
public int getRunStart();
|
jtulach@1334
|
201 |
|
jtulach@1334
|
202 |
/**
|
jtulach@1334
|
203 |
* Returns the index of the first character of the run
|
jtulach@1334
|
204 |
* with respect to the given {@code attribute} containing the current character.
|
jtulach@1334
|
205 |
*/
|
jtulach@1334
|
206 |
public int getRunStart(Attribute attribute);
|
jtulach@1334
|
207 |
|
jtulach@1334
|
208 |
/**
|
jtulach@1334
|
209 |
* Returns the index of the first character of the run
|
jtulach@1334
|
210 |
* with respect to the given {@code attributes} containing the current character.
|
jtulach@1334
|
211 |
*/
|
jtulach@1334
|
212 |
public int getRunStart(Set<? extends Attribute> attributes);
|
jtulach@1334
|
213 |
|
jtulach@1334
|
214 |
/**
|
jtulach@1334
|
215 |
* Returns the index of the first character following the run
|
jtulach@1334
|
216 |
* with respect to all attributes containing the current character.
|
jtulach@1334
|
217 |
*
|
jtulach@1334
|
218 |
* <p>Any contiguous text segments having the same attributes (the
|
jtulach@1334
|
219 |
* same set of attribute/value pairs) are treated as separate runs
|
jtulach@1334
|
220 |
* if the attributes have been given to those text segments separately.
|
jtulach@1334
|
221 |
*/
|
jtulach@1334
|
222 |
public int getRunLimit();
|
jtulach@1334
|
223 |
|
jtulach@1334
|
224 |
/**
|
jtulach@1334
|
225 |
* Returns the index of the first character following the run
|
jtulach@1334
|
226 |
* with respect to the given {@code attribute} containing the current character.
|
jtulach@1334
|
227 |
*/
|
jtulach@1334
|
228 |
public int getRunLimit(Attribute attribute);
|
jtulach@1334
|
229 |
|
jtulach@1334
|
230 |
/**
|
jtulach@1334
|
231 |
* Returns the index of the first character following the run
|
jtulach@1334
|
232 |
* with respect to the given {@code attributes} containing the current character.
|
jtulach@1334
|
233 |
*/
|
jtulach@1334
|
234 |
public int getRunLimit(Set<? extends Attribute> attributes);
|
jtulach@1334
|
235 |
|
jtulach@1334
|
236 |
/**
|
jtulach@1334
|
237 |
* Returns a map with the attributes defined on the current
|
jtulach@1334
|
238 |
* character.
|
jtulach@1334
|
239 |
*/
|
jtulach@1334
|
240 |
public Map<Attribute,Object> getAttributes();
|
jtulach@1334
|
241 |
|
jtulach@1334
|
242 |
/**
|
jtulach@1334
|
243 |
* Returns the value of the named {@code attribute} for the current character.
|
jtulach@1334
|
244 |
* Returns {@code null} if the {@code attribute} is not defined.
|
jtulach@1334
|
245 |
*/
|
jtulach@1334
|
246 |
public Object getAttribute(Attribute attribute);
|
jtulach@1334
|
247 |
|
jtulach@1334
|
248 |
/**
|
jtulach@1334
|
249 |
* Returns the keys of all attributes defined on the
|
jtulach@1334
|
250 |
* iterator's text range. The set is empty if no
|
jtulach@1334
|
251 |
* attributes are defined.
|
jtulach@1334
|
252 |
*/
|
jtulach@1334
|
253 |
public Set<Attribute> getAllAttributeKeys();
|
jtulach@1334
|
254 |
};
|