Path: blob/master/src/java.base/share/classes/java/text/AttributedCharacterIterator.java
41152 views
/*1* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.text;2627import java.io.InvalidObjectException;28import java.io.Serializable;29import java.util.HashMap;30import java.util.Map;31import java.util.Set;3233/**34* An {@code AttributedCharacterIterator} allows iteration through both text and35* related attribute information.36*37* <p>38* An attribute is a key/value pair, identified by the key. No two39* attributes on a given character can have the same key.40*41* <p>The values for an attribute are immutable, or must not be mutated42* by clients or storage. They are always passed by reference, and not43* cloned.44*45* <p>A <em>run with respect to an attribute</em> is a maximum text range for46* which:47* <ul>48* <li>the attribute is undefined or {@code null} for the entire range, or49* <li>the attribute value is defined and has the same non-{@code null} value for the50* entire range.51* </ul>52*53* <p>A <em>run with respect to a set of attributes</em> is a maximum text range for54* which this condition is met for each member attribute.55*56* <p>When getting a run with no explicit attributes specified (i.e.,57* calling {@link #getRunStart()} and {@link #getRunLimit()}), any58* contiguous text segments having the same attributes (the same set59* of attribute/value pairs) are treated as separate runs if the60* attributes have been given to those text segments separately.61*62* <p>The returned indexes are limited to the range of the iterator.63*64* <p>The returned attribute information is limited to runs that contain65* the current character.66*67* <p>68* Attribute keys are instances of {@link AttributedCharacterIterator.Attribute} and its69* subclasses, such as {@link java.awt.font.TextAttribute}.70*71* @see AttributedCharacterIterator.Attribute72* @see java.awt.font.TextAttribute73* @see AttributedString74* @see Annotation75* @since 1.276*/7778public interface AttributedCharacterIterator extends CharacterIterator {7980/**81* Defines attribute keys that are used to identify text attributes. These82* keys are used in {@code AttributedCharacterIterator} and {@code AttributedString}.83* @see AttributedCharacterIterator84* @see AttributedString85* @since 1.286*/8788public static class Attribute implements Serializable {8990/**91* The name of this {@code Attribute}. The name is used primarily by {@code readResolve}92* to look up the corresponding predefined instance when deserializing93* an instance.94* @serial95*/96private String name;9798// table of all instances in this class, used by readResolve99private static final Map<String, Attribute> instanceMap = new HashMap<>(7);100101/**102* Constructs an {@code Attribute} with the given name.103*104* @param name the name of {@code Attribute}105*/106protected Attribute(String name) {107this.name = name;108if (this.getClass() == Attribute.class) {109instanceMap.put(name, this);110}111}112113/**114* Compares two objects for equality. This version only returns true115* for {@code x.equals(y)} if {@code x} and {@code y} refer116* to the same object, and guarantees this for all subclasses.117*/118public final boolean equals(Object obj) {119return super.equals(obj);120}121122/**123* Returns a hash code value for the object. This version is identical to124* the one in {@code Object}, but is also final.125*/126public final int hashCode() {127return super.hashCode();128}129130/**131* Returns a string representation of the object. This version returns the132* concatenation of class name, {@code "("}, a name identifying the attribute133* and {@code ")"}.134*/135public String toString() {136return getClass().getName() + "(" + name + ")";137}138139/**140* Returns the name of the attribute.141*142* @return the name of {@code Attribute}143*/144protected String getName() {145return name;146}147148/**149* Resolves instances being deserialized to the predefined constants.150*151* @return the resolved {@code Attribute} object152* @throws InvalidObjectException if the object to resolve is not153* an instance of {@code Attribute}154*/155@java.io.Serial156protected Object readResolve() throws InvalidObjectException {157if (this.getClass() != Attribute.class) {158throw new InvalidObjectException("subclass didn't correctly implement readResolve");159}160161Attribute instance = instanceMap.get(getName());162if (instance != null) {163return instance;164} else {165throw new InvalidObjectException("unknown attribute name");166}167}168169/**170* Attribute key for the language of some text.171* <p> Values are instances of {@link java.util.Locale Locale}.172* @see java.util.Locale173*/174public static final Attribute LANGUAGE = new Attribute("language");175176/**177* Attribute key for the reading of some text. In languages where the written form178* and the pronunciation of a word are only loosely related (such as Japanese),179* it is often necessary to store the reading (pronunciation) along with the180* written form.181* <p>Values are instances of {@link Annotation} holding instances of {@link String}.182*183* @see Annotation184* @see java.lang.String185*/186public static final Attribute READING = new Attribute("reading");187188/**189* Attribute key for input method segments. Input methods often break190* up text into segments, which usually correspond to words.191* <p>Values are instances of {@link Annotation} holding a {@code null} reference.192* @see Annotation193*/194public static final Attribute INPUT_METHOD_SEGMENT = new Attribute("input_method_segment");195196// make sure the serial version doesn't change between compiler versions197@java.io.Serial198private static final long serialVersionUID = -9142742483513960612L;199200};201202/**203* Returns the index of the first character of the run204* with respect to all attributes containing the current character.205*206* <p>Any contiguous text segments having the same attributes (the207* same set of attribute/value pairs) are treated as separate runs208* if the attributes have been given to those text segments separately.209*210* @return the index of the first character of the run211*/212public int getRunStart();213214/**215* Returns the index of the first character of the run216* with respect to the given {@code attribute} containing the current character.217*218* @param attribute the desired attribute.219* @return the index of the first character of the run220*/221public int getRunStart(Attribute attribute);222223/**224* Returns the index of the first character of the run225* with respect to the given {@code attributes} containing the current character.226*227* @param attributes a set of the desired attributes.228* @return the index of the first character of the run229*/230public int getRunStart(Set<? extends Attribute> attributes);231232/**233* Returns the index of the first character following the run234* with respect to all attributes containing the current character.235*236* <p>Any contiguous text segments having the same attributes (the237* same set of attribute/value pairs) are treated as separate runs238* if the attributes have been given to those text segments separately.239*240* @return the index of the first character following the run241*/242public int getRunLimit();243244/**245* Returns the index of the first character following the run246* with respect to the given {@code attribute} containing the current character.247*248* @param attribute the desired attribute249* @return the index of the first character following the run250*/251public int getRunLimit(Attribute attribute);252253/**254* Returns the index of the first character following the run255* with respect to the given {@code attributes} containing the current character.256*257* @param attributes a set of the desired attributes258* @return the index of the first character following the run259*/260public int getRunLimit(Set<? extends Attribute> attributes);261262/**263* Returns a map with the attributes defined on the current264* character.265*266* @return a map with the attributes defined on the current character267*/268public Map<Attribute,Object> getAttributes();269270/**271* Returns the value of the named {@code attribute} for the current character.272* Returns {@code null} if the {@code attribute} is not defined.273*274* @param attribute the desired attribute275* @return the value of the named {@code attribute} or {@code null}276*/277public Object getAttribute(Attribute attribute);278279/**280* Returns the keys of all attributes defined on the281* iterator's text range. The set is empty if no282* attributes are defined.283*284* @return the keys of all attributes285*/286public Set<Attribute> getAllAttributeKeys();287};288289290