rt/emul/compact/src/main/java/java/text/AttributedCharacterIterator.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 03 Oct 2013 15:40:35 +0200
branchjdk7-b147
changeset 1334 588d5bf7a560
permissions -rw-r--r--
Set of JDK classes needed to run javac
     1 /*
     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.
     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 instanceMap = new HashMap(7);
   101 
   102         /**
   103          * Constructs an {@code Attribute} with the given name.
   104          */
   105         protected Attribute(String name) {
   106             this.name = name;
   107             if (this.getClass() == Attribute.class) {
   108                 instanceMap.put(name, this);
   109             }
   110         }
   111 
   112         /**
   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.
   116          */
   117         public final boolean equals(Object obj) {
   118             return super.equals(obj);
   119         }
   120 
   121         /**
   122          * Returns a hash code value for the object. This version is identical to
   123          * the one in {@code Object}, but is also final.
   124          */
   125         public final int hashCode() {
   126             return super.hashCode();
   127         }
   128 
   129         /**
   130          * Returns a string representation of the object. This version returns the
   131          * concatenation of class name, {@code "("}, a name identifying the attribute
   132          * and {@code ")"}.
   133          */
   134         public String toString() {
   135             return getClass().getName() + "(" + name + ")";
   136         }
   137 
   138         /**
   139          * Returns the name of the attribute.
   140          */
   141         protected String getName() {
   142             return name;
   143         }
   144 
   145         /**
   146          * Resolves instances being deserialized to the predefined constants.
   147          */
   148         protected Object readResolve() throws InvalidObjectException {
   149             if (this.getClass() != Attribute.class) {
   150                 throw new InvalidObjectException("subclass didn't correctly implement readResolve");
   151             }
   152 
   153             Attribute instance = (Attribute) instanceMap.get(getName());
   154             if (instance != null) {
   155                 return instance;
   156             } else {
   157                 throw new InvalidObjectException("unknown attribute name");
   158             }
   159         }
   160 
   161         /**
   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
   165          */
   166         public static final Attribute LANGUAGE = new Attribute("language");
   167 
   168         /**
   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
   172          * written form.
   173          * <p>Values are instances of {@link Annotation} holding instances of {@link String}.
   174          * @see Annotation
   175          * @see java.lang.String
   176          */
   177         public static final Attribute READING = new Attribute("reading");
   178 
   179         /**
   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.
   183          * @see Annotation
   184          */
   185         public static final Attribute INPUT_METHOD_SEGMENT = new Attribute("input_method_segment");
   186 
   187         // make sure the serial version doesn't change between compiler versions
   188         private static final long serialVersionUID = -9142742483513960612L;
   189 
   190     };
   191 
   192     /**
   193      * Returns the index of the first character of the run
   194      * with respect to all attributes containing the current character.
   195      *
   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.
   199      */
   200     public int getRunStart();
   201 
   202     /**
   203      * Returns the index of the first character of the run
   204      * with respect to the given {@code attribute} containing the current character.
   205      */
   206     public int getRunStart(Attribute attribute);
   207 
   208     /**
   209      * Returns the index of the first character of the run
   210      * with respect to the given {@code attributes} containing the current character.
   211      */
   212     public int getRunStart(Set<? extends Attribute> attributes);
   213 
   214     /**
   215      * Returns the index of the first character following the run
   216      * with respect to all attributes containing the current character.
   217      *
   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.
   221      */
   222     public int getRunLimit();
   223 
   224     /**
   225      * Returns the index of the first character following the run
   226      * with respect to the given {@code attribute} containing the current character.
   227      */
   228     public int getRunLimit(Attribute attribute);
   229 
   230     /**
   231      * Returns the index of the first character following the run
   232      * with respect to the given {@code attributes} containing the current character.
   233      */
   234     public int getRunLimit(Set<? extends Attribute> attributes);
   235 
   236     /**
   237      * Returns a map with the attributes defined on the current
   238      * character.
   239      */
   240     public Map<Attribute,Object> getAttributes();
   241 
   242     /**
   243      * Returns the value of the named {@code attribute} for the current character.
   244      * Returns {@code null} if the {@code attribute} is not defined.
   245      */
   246     public Object getAttribute(Attribute attribute);
   247 
   248     /**
   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.
   252      */
   253     public Set<Attribute> getAllAttributeKeys();
   254 };