2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
28 import java.io.InvalidObjectException;
29 import java.io.Serializable;
30 import java.util.HashMap;
35 * An {@code AttributedCharacterIterator} allows iteration through both text and
36 * related attribute information.
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.
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
46 * <p>A <em>run with respect to an attribute</em> is a maximum text range for
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
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.
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.
63 * <p>The returned indexes are limited to the range of the iterator.
65 * <p>The returned attribute information is limited to runs that contain
66 * the current character.
69 * Attribute keys are instances of {@link AttributedCharacterIterator.Attribute} and its
70 * subclasses, such as {@link java.awt.font.TextAttribute}.
72 * @see AttributedCharacterIterator.Attribute
73 * @see java.awt.font.TextAttribute
74 * @see AttributedString
79 public interface AttributedCharacterIterator extends CharacterIterator {
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
89 public static class Attribute implements Serializable {
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
99 // table of all instances in this class, used by readResolve
100 private static final Map instanceMap = new HashMap(7);
103 * Constructs an {@code Attribute} with the given name.
105 protected Attribute(String name) {
107 if (this.getClass() == Attribute.class) {
108 instanceMap.put(name, this);
113 * Compares two objects for equality. This version only returns true
114 * for <code>x.equals(y)</code> if <code>x</code> and <code>y</code> refer
115 * to the same object, and guarantees this for all subclasses.
117 public final boolean equals(Object obj) {
118 return super.equals(obj);
122 * Returns a hash code value for the object. This version is identical to
123 * the one in {@code Object}, but is also final.
125 public final int hashCode() {
126 return super.hashCode();
130 * Returns a string representation of the object. This version returns the
131 * concatenation of class name, {@code "("}, a name identifying the attribute
134 public String toString() {
135 return getClass().getName() + "(" + name + ")";
139 * Returns the name of the attribute.
141 protected String getName() {
146 * Resolves instances being deserialized to the predefined constants.
148 protected Object readResolve() throws InvalidObjectException {
149 if (this.getClass() != Attribute.class) {
150 throw new InvalidObjectException("subclass didn't correctly implement readResolve");
153 Attribute instance = (Attribute) instanceMap.get(getName());
154 if (instance != null) {
157 throw new InvalidObjectException("unknown attribute name");
162 * Attribute key for the language of some text.
163 * <p> Values are instances of {@link java.util.Locale Locale}.
164 * @see java.util.Locale
166 public static final Attribute LANGUAGE = new Attribute("language");
169 * Attribute key for the reading of some text. In languages where the written form
170 * and the pronunciation of a word are only loosely related (such as Japanese),
171 * it is often necessary to store the reading (pronunciation) along with the
173 * <p>Values are instances of {@link Annotation} holding instances of {@link String}.
175 * @see java.lang.String
177 public static final Attribute READING = new Attribute("reading");
180 * Attribute key for input method segments. Input methods often break
181 * up text into segments, which usually correspond to words.
182 * <p>Values are instances of {@link Annotation} holding a {@code null} reference.
185 public static final Attribute INPUT_METHOD_SEGMENT = new Attribute("input_method_segment");
187 // make sure the serial version doesn't change between compiler versions
188 private static final long serialVersionUID = -9142742483513960612L;
193 * Returns the index of the first character of the run
194 * with respect to all attributes containing the current character.
196 * <p>Any contiguous text segments having the same attributes (the
197 * same set of attribute/value pairs) are treated as separate runs
198 * if the attributes have been given to those text segments separately.
200 public int getRunStart();
203 * Returns the index of the first character of the run
204 * with respect to the given {@code attribute} containing the current character.
206 public int getRunStart(Attribute attribute);
209 * Returns the index of the first character of the run
210 * with respect to the given {@code attributes} containing the current character.
212 public int getRunStart(Set<? extends Attribute> attributes);
215 * Returns the index of the first character following the run
216 * with respect to all attributes containing the current character.
218 * <p>Any contiguous text segments having the same attributes (the
219 * same set of attribute/value pairs) are treated as separate runs
220 * if the attributes have been given to those text segments separately.
222 public int getRunLimit();
225 * Returns the index of the first character following the run
226 * with respect to the given {@code attribute} containing the current character.
228 public int getRunLimit(Attribute attribute);
231 * Returns the index of the first character following the run
232 * with respect to the given {@code attributes} containing the current character.
234 public int getRunLimit(Set<? extends Attribute> attributes);
237 * Returns a map with the attributes defined on the current
240 public Map<Attribute,Object> getAttributes();
243 * Returns the value of the named {@code attribute} for the current character.
244 * Returns {@code null} if the {@code attribute} is not defined.
246 public Object getAttribute(Attribute attribute);
249 * Returns the keys of all attributes defined on the
250 * iterator's text range. The set is empty if no
251 * attributes are defined.
253 public Set<Attribute> getAllAttributeKeys();